
% Instantiate remaining embeddings to replication.

% ---------------------------------------------------------------------
 
% $Id$
 
% $Log$

% ---------------------------------------------------------------------

% ``meta-syntax'' is
%
%  <expression>  ::=
%    expRef(<exp_handle>)                                            |
%    idxRef(<exp_handle>, <idx_handle>)                              |
%    varRef(<var_handle>)                                            |
%    funRef(<fun_handle>, <tag>, List(<exp_handle>))                 |
%    subscript(<exp_handle>, List(<exp_handle>))                     |
%    arrall(List(<idx_handle>), List(<exp_handle>), <exp_handle>)    |
%    forall(List(<idx_handle>), List(<exp_handle>), List(<exp_handle>),
%           List(<exp_handle>))                                      |
%    size(<literal>, <exp_handle>)                                   |
%    literal(<literal>)                                              |
%    true                                                            |
%    false                                                           |
%    any
%
%  <index_declaration> ::= index(<range>, <idx_attr>)
%
%  <tag>         ::= <predef> | user
%
%  <predef>      ::= ...

% Attribute meta-syntax...
%
%  <fun_attr> ::= syms         :: <sym_tab>               #
%                 declarations :: List(<var_handle> |
%                                      <fun_handle> |
%                                      <exp_handle>)      #
%                 statements   :: List(<exp_handle>)      #
%                 dummies      :: List(<var_handle>)      #
%                 result       :: <var_handle>            #
%                 class        :: elemental | array       #
%                 dumShps      :: List(<extern_shape>)    #
%                 resShp       :: <extern_shape>          #
%                 dumMaps      :: List(<extern_mapping>)  #
%                 resMap       :: <extern_mapping>        #
%                 dumAlnGrps   :: List(List(<literal>))   #
%                 resAlnGrp    :: List(<literal>)
%
%  <var_attr> ::= shape    :: List(<exp_handle>)         #
%                 ishape   :: List(<exp_handle>)         #
%                 mapDesc  :: <var_mapping_desc>         #
%                 imapDesc :: <var_mapping_desc>
%
%  <exp_attr> ::= shape :: List(<range>)

% Syntax of mapping descriptors
%
%
%  <exp_mapping_desc>  ::=  align(<template>,
%                                 List(<arr_idx_mapping_desc),
%                                 List(<idx_mapping_desc),
%                                 List(<embedding_desc>))
%
%  <arr_idx_mapping_desc>  ::=  <map> :: <dimension>
%
%  <template>          ::= def | <var_handle>
%
%  <idx_mapping_desc>  ::= <map> :: <idx_handle> -> <dimension>
%
%  <map>               ::= map(<exp_handle>, <exp_handle>)  |
%                          id                               |
%                          any
%
%  <embedding_desc>    ::=  emb(<exp_handle>) :: <dimension>  |
%                           rep :: <dimension>


replicateExpressionHandleList([], _).
replicateExpressionHandleList([EH | EHs], Syms) :-
    replicateExpressionHandle(EH, Syms),
    replicateExpressionHandleList(EHs, Syms).

replicateExpressionHandle(EH, Syms) :-
    symLookUp(EH, expression(E, Atr), Syms),
    replicateExpression(E, Atr, Syms).

replicateExpression(varRef(_), Atr, Syms) :-
    expAtr(Atr),
      CtxMap    := Atr -> ctxMap,  LocMap := Atr -> locMap,
    replicate(LocMap),
    replicate(CtxMap),
    embedProp(CtxMap, LocMap, UnifiedIM).
replicateExpression(funRef(_, _, _), Atr, Syms) :-
    expAtr(Atr),
      EHs       := Atr -> mapDaughts,
      CtxMap    := Atr -> ctxMap,  LocMap := Atr -> locMap,
    replicate(LocMap),
    replicate(CtxMap),
    replicateExpressionHandleList(EHs, Syms).
replicateExpression(subscript(_, _), Atr, Syms) :-
    expAtr(Atr),
      EHs    := Atr -> mapDaughts,
      CtxMap := Atr -> ctxMap,      LocMap := Atr -> locMap,
    replicate(LocMap),
    replicate(CtxMap),
    replicateExpressionHandleList(EHs, Syms).
replicateExpression(subscript(_, _), Atr, Syms) :-
    expAtr(Atr),
      EHs    := Atr -> mapDaughts,
      CtxMap := Atr -> ctxMap,      LocMap := Atr -> locMap,
    replicate(LocMap),
    replicate(CtxMap),
    replicateExpressionHandleList(EHs, Syms).
replicateExpression(forall(_, Shape, _, EHs), _, Syms) :-
    replicateExpressionHandleList(Shape, Syms),
    replicateExpressionHandleList(EH, Syms).
replicateExpression(arrall(_, Shape, EH), _, Syms) :-
    replicateExpressionHandleList(Shape, Syms),
    replicateExpressionHandleList(EH, Syms).
replicateExpression(idxRef(SizeH, _), Atr, Syms) :-
    replicateExpressionHandle(SizeH, Syms).
replicateExpression(size(_, EH), Atr, Syms) :-
    expAtr(Atr),
      CtxMap    := Atr -> ctxMap,      LocMap := Atr -> locMap,
    replicate(LocMap),
    replicate(CtxMap),
    replicateExpressionHandle(EH, Syms).
replicateExpression(expRef(_), _, _).
replicateExpression(E, _, _) :- isLiteral(E).


% ----- Embed linear subscript expressions -----


replicate(Map) :-
    getEmbedding(Embed, Map),
    replicateEmbeddings(Embed).


replicateEmbeddings([]).
replicateEmbeddings([Embedding | Embeddings]) :-
    replicateEmbedding(Embedding),
    replicateEmbeddings(Embeddings).

replicateEmbedding(rep :: _) :- !.
replicateEmbedding(_).

