
% Synthesise template dimensions where necessary.

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

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

% !!!! Record relevant syntax of symbol table !!!!

% ``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(<idx_mapping_desc>),
%                                 List(<idx_mapping_desc>),
%                                 List(<embedding_desc>))
%
%  <template>          ::= def | <var_handle>
%
%  <idx_mapping_desc>  ::= <map> :: <idx_domain> -> <dimension>
%
%  <idx_domain>        ::= <idx_handle> : <exp_handle>  |
%                          <exp_handle>
%
%  <map>               ::= map(<exp_handle>, <exp_handle>)  |
%                          id                               |
%                          any
%
%  <embedding_desc>    ::=  emb(<exp_handle>) :: <dimension>  |
%                           rep :: <dimension>


idxMapSynthDimsExpressionHandleList([], _).
idxMapSynthDimsExpressionHandleList([EH | EHs], Syms) :-
    idxMapSynthDimsExpressionHandle(EH, Syms),
    idxMapSynthDimsExpressionHandleList(EHs, Syms).

idxMapSynthDimsExpressionHandle(EH, Syms) :-
    symLookUp(EH, expression(E, Atr), Syms),
    idxMapSynthDimsExpression(E, Atr, Syms).

idxMapSynthDimsExpression(varRef(_), Atr, Syms) :-
    expAtr(Atr),
      CtxMap := Atr -> ctxMap,  LocMap := Atr -> locMap,
    idxMapSynthDims(CtxMap, Syms),
    idxMapSynthDims(LocMap, Syms).
idxMapSynthDimsExpression(funRef(_, _, _), Atr, Syms) :-
    expAtr(Atr),
      EHs    := Atr -> mapDaughts,
      CtxMap := Atr -> ctxMap,  LocMap := Atr -> locMap,
    idxMapSynthDims(CtxMap, Syms),
    idxMapSynthDims(LocMap, Syms),
    idxMapSynthDimsExpressionHandleList(EHs, Syms).
idxMapSynthDimsExpression(subscript(_, _), Atr, Syms) :-
    expAtr(Atr),
      EHs    := Atr -> mapDaughts,
      CtxMap := Atr -> ctxMap,      LocMap := Atr -> locMap,
    idxMapSynthDims(CtxMap, Syms),
    idxMapSynthDims(LocMap, Syms),
    idxMapSynthDimsExpressionHandleList(EHs, Syms).
idxMapSynthDimsExpression(forall(_, Shape, _, EHs), _, Syms) :-
    idxMapSynthDimsExpressionHandleList(Shape, Syms),
    idxMapSynthDimsExpressionHandleList(EH, Syms).
idxMapSynthDimsExpression(arrall(_, Shape, EH), _, Syms) :-
    idxMapSynthDimsExpressionHandleList(Shape, Syms),
    idxMapSynthDimsExpressionHandleList(EH, Syms).
idxMapSynthDimsExpression(idxRef(SizeH, IH), Atr, Syms) :-
    idxMapSynthDimsExpressionHandle(SizeH, Syms).
idxMapSynthDimsExpression(size(_, EH), Atr, Syms) :-
    expAtr(Atr),
      CtxMap := Atr -> ctxMap,      LocMap := Atr -> locMap,
    idxMapSynthDims(CtxMap, Syms),
    idxMapSynthDims(LocMap, Syms),
    idxMapSynthDimsExpressionHandle(SizeH, Syms).
idxMapSynthDimsExpression(expRef(_), _, _).
idxMapSynthDimsExpression(E, _, _) :- isLiteral(E).


% ----- `idxMapSynthDims' -----

idxMapSynthDims(Map, Syms) :-
    getAIMap(AIMap, Map),
    getIMap(IdxMap, Map),
    doSynthDims(AIMap, Syms),
    doSynthDims(IMap, Syms).

doSynthDims([], _).
doSynthDims([IdxMapping | IdxMappings], Syms) :-
    doSynthDim(IdxMapping, Syms),
    doSynthDims(IdxMappings, Syms).

doSynthDim(M :: IdxDom -> D, Syms) :-
    var(M),
    !,
    sizeIdxDom(SizeH, IdxDom),
    symInstall(DH, synth(SizeH), Syms),
    M = synth(DH).
doSynthDim(_, _).

