Changeset 159

Show
Ignore:
Timestamp:
04/11/07 04:17:09 (2 years ago)
Author:
ant_39
Message:
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/gcc/gcc/algol60/Make-lang.in

    r154 r159  
    3232                algol60/symbol.o algol60/type.o \ 
    3333                algol60/for-elmt.o algol60/desig-expr.o \ 
    34                 algol60/statement.o algol60/expression.o 
     34                algol60/statement.o algol60/expression.o \ 
     35                algol60/a60_symtab.o 
    3536 
    3637.PHONY: algol60 
     
    144145algol60/symbol.o: algol60/symbol.c algol60/symbol.h algol60/symbol.i\ 
    145146                algol60/label.i algol60/type.i algol60/statement.i algol60/estring.i\ 
    146                 algol60/cursor.i
     147                algol60/cursor.i algol60/logger.i
    147148                algol60/label.h algol60/type.h algol60/statement.h algol60/estring.h\ 
    148                 algol60/cursor.h
     149                algol60/cursor.h algol60/logger.h
    149150                algol60/visitor.i algol60/visitor.h algol60/visitor-impl.h\ 
     151                algol60/meta.h algol60/pd.h algol60/gcc-stuff 
     152 
     153algol60/a60_symtab.o: algol60/a60_symtab.c algol60/a60_symtab.h algol60/a60_symtab.i\ 
     154                algol60/symbol.i algol60/label.i algol60/type.i algol60/statement.i\ 
     155                algol60/estring.i algol60/logger.i algol60/cursor.i algol60/visitor.i\ 
     156                algol60/slist.h algol60/symbol.h algol60/label.h algol60/type.h\ 
     157                algol60/logger.h algol60/estring.h\ 
    150158                algol60/meta.h algol60/pd.h algol60/gcc-stuff 
    151159 
     
    163171                algol60/cursor.i algol60/slist.i algol60/expression.i\ 
    164172                algol60/logger.i algol60/symbol.i algol60/label.i algol60/type.i\ 
    165                 algol60/estring.i
     173                algol60/estring.i algol60/a60_symtab.i
    166174                algol60/cursor.h algol60/slist.h algol60/expression.h\ 
    167175                algol60/logger.h algol60/symbol.h algol60/label.h algol60/type.h\ 
    168176                algol60/estring.h algol60/boundspair.h algol60/for-elmt.h\ 
     177                algol60/a60_symtab.h\ 
    169178                algol60/visitor-impl.h algol60/visitor.h algol60/visitor.i\ 
    170179                algol60/meta.h algol60/pd.h algol60/gcc-stuff 
  • trunk/gcc/gcc/algol60/al60l-bind.c

    r157 r159  
    4040#include "for-elmt.h" 
    4141#include "visitor.h" 
     42#include "a60_symtab.h" 
    4243 
    4344struct struct_al60l_bind_state_t 
     
    730731} 
    731732 
     733static void * 
     734private_stmt_container_build_decl_for_symbol (symbol_t * sym, void * _state) 
     735{ 
     736  // This is called from stmt_container_build_generic for each symbol 
     737  // in symbol table. 
     738  al60l_bind_state_t * state = _state; 
     739  tree decl = symbol_decl_for_type (sym, symbol_type (sym), state); 
     740  symbol_set_extra (sym, decl); 
     741  bind_state_add_decl (state, decl); 
     742  return NULL; 
     743} 
     744 
     745static void * 
     746private_stmt_container_build_init_for_symbol (symbol_t * sym, void * _state) 
     747{ 
     748  // This is called from stmt_container_build_generic for each symbol 
     749  // in symbol table. 
     750  al60l_bind_state_t * state = _state; 
     751  tree init = symbol_init_for_type (sym, symbol_type (sym), state); 
     752  bind_state_add_stmt (state, init); 
     753  return NULL; 
     754} 
     755 
    732756void * 
    733757stmt_container_build_generic (container_t * self, void * _state) 
     
    740764  bind_state_push_block (state); 
    741765 
    742   it = slist_iter (container_symtab (self)); 
    743   for (; slist_it_has (it); slist_it_next (it)) 
    744     { 
    745       symbol_t * sym = slist_it_get (it); 
    746       tree decl = symbol_decl_for_type (sym, symbol_type (sym), _state); 
    747       symbol_set_extra (sym, decl); 
    748  
    749       bind_state_add_decl (state, decl); 
    750     } 
    751  
    752   slist_it_reset (it, container_symtab (self)); 
    753  
    754   for (; slist_it_has (it); slist_it_next (it)) 
    755     { 
    756       symbol_t * sym = slist_it_get (it); 
    757       tree init = symbol_init_for_type (sym, symbol_type (sym), _state); 
    758       bind_state_add_stmt (state, init); 
    759     } 
    760  
    761   slist_it_reset (it, container_stmts (self)); 
    762  
     766  a60_symtab_each (container_symtab (self), 
     767                   a60_symbol_callback (private_stmt_container_build_decl_for_symbol), 
     768                   _state); 
     769 
     770  a60_symtab_each (container_symtab (self), 
     771                   a60_symbol_callback (private_stmt_container_build_init_for_symbol), 
     772                   _state); 
     773 
     774  it = slist_iter (container_stmts (self)); 
    763775  for (; slist_it_has (it); slist_it_next (it)) 
    764776    { 
     
    782794} 
    783795 
     796static void * 
     797private_stmt_toplev_build_decl_for_symbol (symbol_t * sym, void * _state) 
     798{ 
     799  // This is called from stmt_toplev_build_generic for each symbol 
     800  // in symbol table. 
     801  al60l_bind_state_t * state = _state; 
     802  symbol_set_extra (sym, builtin_decl_get_generic (sym, state)); 
     803  return NULL; 
     804} 
     805 
    784806void * 
    785807stmt_toplev_build_generic (container_t * self, void * _state) 
    786808{ 
    787   al60l_bind_state_t * state = _state; 
    788  
    789809  // Process builtins first. 
    790   slist_it_t * it = slist_iter (container_symtab (self)); 
    791   for (; slist_it_has (it); slist_it_next (it)) 
    792     { 
    793       symbol_t * sym = a60_as_symbol (slist_it_get (it)); 
    794       symbol_set_extra (sym, builtin_decl_get_generic (sym, state)); 
    795     } 
    796   delete_slist_it (it); 
     810  a60_symtab_each (container_symtab (self), 
     811                   a60_symbol_callback (private_stmt_toplev_build_decl_for_symbol), 
     812                   _state); 
    797813 
    798814  // The toplev block should contain only one node: the actual program 
     
    961977  // typechecking, and subexpression resolving has already been done 
    962978  // by this point. 
    963   symbol_t * sym = new_symbol (l); 
     979  symbol_t * sym = new_symbol_var (l); 
    964980  symbol_set_type (sym, types[typeidx]); // function type 
    965981  symbol_set_hidden (sym, 1); 
  • trunk/gcc/gcc/algol60/desig-expr.c

    r151 r159  
    228228  assert (self != NULL); 
    229229  assert (log != NULL); 
     230  a60_symtab_t * symtab = container_symtab (context); 
    230231 
    231232  switch (self->base.kind) 
     
    234235      { 
    235236        self->elbl.sym 
    236           = container_find_name_rec_add_undefined (context, self->elbl.lbl, 
     237          = a60_symtab_find_name_rec_add_undefined (symtab, self->elbl.lbl, 
    237238                                                   type_label (), 
    238239                                                   log, self->cursor); 
     
    266267      { 
    267268        self->eswitch.sym 
    268           = container_find_name_rec_add_undefined (context, self->elbl.lbl, 
     269          = a60_symtab_find_name_rec_add_undefined (symtab, self->elbl.lbl, 
    269270                                                   type_switch_any (), 
    270271                                                   log, self->cursor); 
  • trunk/gcc/gcc/algol60/expression.c

    r151 r159  
    617617{ 
    618618  type_t * match_type = new_t_proc (type_any (), new_slist ()); 
    619   symbol_t * found = container_find_name_rec (block, self->idref.lbl, match_type); 
     619  a60_symtab_t * symtab = container_symtab (block); 
     620  symbol_t * found = a60_symtab_find_name_rec (symtab, self->idref.lbl, match_type); 
    620621  if (found == NULL) 
    621     found = container_find_name_rec_add_undefined (block, self->idref.lbl, type_any (), 
     622    found = a60_symtab_find_name_rec_add_undefined (symtab, self->idref.lbl, 
     623                                                    type_any (), 
    622624                                                   log, self->cursor); 
    623625  self->idref.sym = found; 
     
    727729 
    728730  type_t * match_type = new_t_proc (type_any (), argtypes); 
     731  a60_symtab_t * symtab = container_symtab (block); 
    729732  self->call.sym = 
    730     container_find_name_rec_add_undefined (block, self->call.lbl, 
     733    a60_symtab_find_name_rec_add_undefined (symtab, self->call.lbl, 
    731734                                           match_type, log, 
    732735                                           self->cursor); 
     
    746749  delete_slist_it (it); 
    747750 
     751  a60_symtab_t * symtab = container_symtab (block); 
    748752  self->subscript.sym = 
    749     container_find_name_rec_add_undefined (block, self->subscript.lbl, 
     753    a60_symtab_find_name_rec_add_undefined (symtab, self->subscript.lbl, 
    750754                                           type_array_any (), log, 
    751755                                           self->cursor); 
  • trunk/gcc/gcc/algol60/parser.y

    r154 r159  
    1414#include "desig-expr.h" 
    1515#include "for-elmt.h" 
     16#include "a60_symtab.h"//! 
    1617#include "meta.h" 
    1718#include "visitor-impl.h" 
     
    196197//%type <sym> DeclaredIdentifier 
    197198%type <stmt> Block 
     199%type <type> Specifier 
     200%type <lst> SpecificationPart 
     201%type <lst> ValuePart 
     202%type <lst> FormalParameterList 
     203%type <lst> FormalParameterPart 
    198204//%type <> BlockDeclarationsList 
    199205//%type <> BlockDeclarations 
    200206%type <type> Type 
     207%type <type> OptType 
    201208%type <flag> OptOwn 
    202209%type <type> OptIntrinsicType 
     
    228235%type <lst> ForList 
    229236%type <lelm> ForListElmt 
    230  
    231237%% 
    232238 
     
    241247    container_t * c = new_stmt_toplev (NULL); 
    242248    container_set_parent (c, dummy); 
    243     stmt_toplev_define_internals (c); 
     249    a60_symtab_t * symtab = container_symtab (c); 
     250    a60_symtab_toplev_define_internals (symtab); 
    244251    private_open_block (parser, c); 
    245252  } 
     
    278285 
    279286LabelIdentifier: 
    280   IDENTIFIER 
    281     { 
    282       log_printf (parser->log, ll_debug, "LabelIdentifier -> IDENTIFIER"); 
    283       estring_t * lit = $1; 
    284       $$ = new_label (lit); 
    285       label_set_cursor ($$, cr_csr (parser, &@1)); 
    286     } 
     287  Identifier 
    287288  | 
    288289  LITINTEGER 
     
    404405      private_setup_and_add_symbol (parser, $2, t); 
    405406    } 
     407  | 
     408  OptType KWPROCEDURE Identifier FormalParameterPart SEPSEMICOLON ValuePart SpecificationPart Statement 
     409    { 
     410      log_printf (parser->log, ll_debug, 
     411                  "BlockDeclarations -> OptType KWPROCEDURE Identifier " 
     412                  "FormalParameterPart SEPSEMICOLON ValuePart " 
     413                  "SpecificationPart Statement"); 
     414      /* 
     415      slist_t * formal_params = $4; 
     416      slist_t * value_params = $6; 
     417      slist_t * param_types = $7; 
     418      slist_it_t * it = slist_iter (formal_params); 
     419      for (; slist_it_has (it); slist_it_next (it)) 
     420        { 
     421          //@TODO 
     422        } 
     423      delete_slist_it (it); 
     424      //type_t * t = new_t_switch ($4); 
     425      */ 
     426    } 
     427 
     428FormalParameterPart: 
     429  /*epsilon*/ { $$ = new_slist (); } 
     430  | 
     431  SEPLPAREN FormalParameterList SEPRPAREN { $$ = $2; } 
     432 
     433FormalParameterList: 
     434  Identifier 
     435    { 
     436      $$ = new_slist (); 
     437      slist_pushfront ($$, $1); 
     438      @$ = @1; 
     439    } 
     440  | 
     441  FormalParameterList ParameterDelimiter Identifier 
     442    { 
     443      slist_pushfront ($$, $3); 
     444      @$ = @1; 
     445    } 
     446 
     447ValuePart: 
     448  /*epsilon*/ { $$ = new_slist (); } 
     449  | 
     450  KWVALUE IdentifierList SEPSEMICOLON { $$ = $2; } 
     451 
     452SpecificationPart: 
     453  /*epsilon*/ 
     454    { $$ = new_slist (); } 
     455  | 
     456  SpecificationPart Specifier IdentifierList SEPSEMICOLON 
     457    { 
     458      slist_pushfront ($$, $3); 
     459      slist_pushfront ($$, $2); 
     460      @$ = @1; 
     461    } 
     462 
     463Specifier: 
     464  IntrinsicType 
     465  | 
     466  IntrinsicType KWARRAY { $$ = new_t_array ($1); } 
     467  | 
     468  KWARRAY { $$ = type_array_real (); } 
     469  | 
     470  KWLABEL 
     471    { 
     472      log_printf (parser->log, ll_debug, "Specifier -> KWLABEL"); 
     473      $$ = type_label (); 
     474      @$ = @1; 
     475    } 
     476  | 
     477  KWSWITCH 
     478    { 
     479      log_printf (parser->log, ll_debug, "Specifier -> KWSWITCH"); 
     480      $$ = type_switch_any (); 
     481      @$ = @1; 
     482    } 
     483  | 
     484  OptIntrinsicType KWPROCEDURE 
     485    { 
     486      log_printf (parser->log, ll_debug, "Specifier -> OptType KWPROCEDURE"); 
     487      $$ = new_t_proc_stub ($1 ? $1 : type_any ()); 
     488      @$ = @1; 
     489    } 
    406490 
    407491Type: 
     
    438522    } 
    439523 
     524OptType: 
     525  /*epsilon*/ { $$ = 0; } 
     526  | 
     527  Type 
     528 
    440529OptOwn: 
    441530  /*epsilon*/ { $$ = 0; } 
     
    11781267 
    11791268  while (context != parser->program_block 
    1180          && slist_empty (container_symtab (context))) 
     1269         && a60_symtab_empty (container_symtab (context))) 
    11811270    context = container_parent (context); 
    11821271 
     
    11851274    { 
    11861275      label_t * lbl = slist_it_get (it); 
    1187       symbol_t * sym = new_symbol (lbl); 
    1188       if (container_add_symbol (context, sym, sek_ordinary) != 0) 
     1276      symbol_t * sym = new_symbol_var (lbl); 
     1277      a60_symtab_t * symtab = container_symtab (context); 
     1278      if (a60_symtab_add_symbol (symtab, sym, sek_ordinary) != 0) 
    11891279        { 
    11901280          cursor_t * csr = stmt_cursor (target); 
     
    12071297{ 
    12081298  // Setup symbol and add to table. 
    1209   symbol_t * sym = new_symbol (lbl); 
     1299  symbol_t * sym = new_symbol_var (lbl); 
    12101300  symbol_set_type (sym, qt); 
    1211   int conflict = container_add_symbol (parser->block, sym, sek_ordinary); 
     1301  a60_symtab_t * symtab = container_symtab (parser->block); 
     1302  int conflict = a60_symtab_add_symbol (symtab, sym, sek_ordinary); 
    12121303  if (conflict) 
    12131304    { 
  • trunk/gcc/gcc/algol60/statement.c

    r154 r159  
    1616#include "for-elmt.h" 
    1717#include "meta.h" 
     18#include "a60_symtab.h" 
    1819#include "visitor-impl.h" 
    1920 
     
    6465{ 
    6566  slist_t * statements; 
    66   slist_t * symtab; 
     67  a60_symtab_t * symtab; 
    6768} 
    6869container_rep_t; 
     
    7778  sk_goto, 
    7879  sk_block, 
    79   sk_toplev 
     80  sk_toplev, 
    8081} 
    8182stmt_kind_t; 
     
    205206  statement_t * ret = private_new_statement (kind, cursor, NULL); 
    206207  ret->block.statements = new_slist_typed (adapt_test, a60_as_statement); 
    207   ret->block.symtab = new_slist_typed (adapt_test, a60_as_symbol); 
     208  ret->block.symtab = a60_new_symtab (); 
    208209  return (container_t *)ret; 
    209210} 
     
    219220{ 
    220221  return private_new_container (sk_toplev, cursor); 
    221 } 
    222  
    223 statement_t * 
    224 clone_statement (statement_t const * self) 
    225 { 
    226   assert (self != NULL); 
    227  
    228   statement_t * ret = private_new_statement (self->base.kind, self->cursor, NULL); 
    229  
    230   switch (self->base.kind) 
    231     { 
    232     case sk_dummy: 
    233       break; 
    234  
    235     case sk_assign: 
    236       ret->assign.lhss = clone_slist (self->assign.lhss); 
    237       slist_map (ret->assign.lhss, adapt_test, clone_expression); 
    238       ret->assign.rhs = clone_expression (self->assign.rhs); 
    239       break; 
    240  
    241     case sk_call: 
    242       ret->call.call = clone_expression (self->call.call); 
    243       break; 
    244  
    245     case sk_cond: 
    246       ret->cond.cond = clone_expression (self->cond.cond); 
    247       ret->cond.ifclause = clone_statement (self->cond.ifclause); 
    248       ret->cond.elseclause 
    249         = ret->cond.elseclause ? clone_statement (self->cond.elseclause) : NULL; 
    250       break; 
    251  
    252     case sk_for: 
    253       ret->afor.variable = clone_expression (self->afor.variable); 
    254       ret->afor.elmts = clone_slist (self->afor.elmts); 
    255       slist_map (ret->afor.elmts, adapt_test, clone_for_elmt); 
    256       ret->afor.body = clone_statement (self->afor.body); 
    257       break; 
    258  
    259     case sk_goto: 
    260       ret->agoto.target = clone_desig_expr (self->agoto.target); 
    261       break; 
    262  
    263     case sk_block: 
    264     case sk_toplev: 
    265       { 
    266         ret->block.symtab = clone_slist (self->block.symtab); 
    267         slist_map (ret->block.symtab, adapt_test, clone_symbol); 
    268         ret->block.statements = new_slist_typed (adapt_test, a60_as_statement); 
    269  
    270         // extract labels from the list 
    271         slist_t * labels = new_slist (); 
    272         slist_it_t * jt = slist_iter (ret->block.symtab); 
    273         for (; slist_it_has (jt); slist_it_next (jt)) 
    274           { 
    275             symbol_t * sym = slist_it_get (jt); 
    276             if (types_same (symbol_type (sym), type_label ())) 
    277               slist_pushback (labels, sym); 
    278           } 
    279  
    280         // clone statement list and retarget labels in one pass 
    281         slist_it_t * it = slist_iter (self->block.statements); 
    282         for (; slist_it_has (it); slist_it_next (it)) 
    283           { 
    284             statement_t * stmt = slist_it_get (it); 
    285             statement_t * clone = clone_statement (stmt); 
    286             container_add_stmt (a60_as_container (ret), clone); 
    287  
    288             slist_it_reset (jt, labels); 
    289             for (; slist_it_has (jt); slist_it_next (jt)) 
    290               { 
    291                 symbol_t * sym = slist_it_get (jt); 
    292                 statement_t * target = symbol_stmt (sym); 
    293                 if (target == stmt) 
    294                   { 
    295                     symbol_set_stmt (sym, clone); 
    296                     stmt_add_label (clone, sym); 
    297                   } 
    298               } 
    299           } 
    300  
    301         delete_slist_it (jt); 
    302         delete_slist_it (it); 
    303         delete_slist (labels); 
    304       } 
    305       break; 
    306     }; 
    307  
    308   return ret; 
    309 } 
    310  
    311 container_t * 
    312 clone_container (container_t const * self) 
    313 { 
    314   return a60_as_container (clone_statement (a60_as_statement ((container_t *)self))); 
    315222} 
    316223 
     
    353260 
    354261static void 
    355 private_dump_container (statement_t const * self, estring_t * buf, int level) 
    356 
     262private_dump_container (statement_t const * self ATTRIBUTE_UNUSED, 
     263                        estring_t * buf ATTRIBUTE_UNUSED, 
     264                        int level ATTRIBUTE_UNUSED) 
     265
     266  /* 
    357267  estring_t * buf0 = new_estring (); 
    358268 
     
    428338 
    429339  delete_estring (buf0); 
     340  */ 
    430341} 
    431342 
     
    605516{ 
    606517  assert (a60_as_container (self)); 
    607   slist_it_t * it; 
    608  
    609   it = slist_iter (self->block.symtab); 
    610   for (; slist_it_has (it); slist_it_next (it)) 
    611     { 
    612       symbol_t * sym = slist_it_get (it); 
    613       type_resolve_symbols (symbol_type (sym), a60_as_container (self), log); 
    614     } 
    615  
    616   slist_it_reset (it, self->block.statements); 
    617  
     518  a60_symtab_resolve_symbols (self->block.symtab, a60_as_container (self), log); 
     519  slist_it_t * it = slist_iter (self->block.statements); 
    618520  for (; slist_it_has (it); slist_it_next (it)) 
    619521    stmt_resolve_symbols (slist_it_get (it), log); 
     
    729631 
    730632  self->parent = parent; 
     633  a60_symtab_set_parent (self->block.symtab, ((statement_t*)parent)->block.symtab); 
    731634} 
    732635 
     
    838741} 
    839742 
    840 slist_t * 
     743a60_symtab_t * 
    841744container_symtab (container_t const * _self) 
    842745{ 
     
    854757  assert (self->base.kind == sk_block || self->base.kind == sk_toplev); 
    855758  return self->block.statements; 
    856 } 
    857  
    858 static void 
    859 private_container_find_it (statement_t * container, label_t const * lbl, type_t const * atype, slist_it_t ** ret_prev, slist_it_t ** ret_it) 
    860 { 
    861   slist_it_t * it = slist_iter (container->block.symtab); 
    862   slist_it_t * prev = NULL; 
    863  
    864   for (; slist_it_has (it); ) 
    865     { 
    866       symbol_t * sym = slist_it_get (it); 
    867       type_t * symtype = symbol_type (sym); 
    868       if (label_eq (symbol_label (sym), lbl) 
    869           && (symtype == NULL || types_match (symtype, atype))) 
    870         break; 
    871  
    872       slist_it_next (it); 
    873       if (ret_prev) 
    874         { 
    875           if (prev) 
    876             slist_it_next (prev); 
    877           else 
    878             prev = slist_iter (container->block.symtab); 
    879         } 
    880     } 
    881  
    882   if (ret_it) 
    883     *ret_it = it; 
    884   else 
    885     delete_slist_it (it); 
    886  
    887   if (ret_prev) 
    888     *ret_prev = prev; 
    889 } 
    890  
    891 int 
    892 container_add_symbol (container_t * _self, symbol_t * sym, 
    893                       symtab_entry_kind_t internal) 
    894 { 
    895   assert (_self != NULL); 
    896   assert (sym != NULL); 
    897   A60_USER_TO_REP (statement, self, *); 
    898   assert (a60_as_container (self)); 
    899  
    900   if (internal == sek_ordinary 
    901       && container_find_name (_self, symbol_label (sym), type_any ()) != NULL) 
    902     return -1; 
    903  
    904   slist_pushback (self->block.symtab, sym); 
    905   return 0; 
    906 } 
    907  
    908 symbol_t * 
    909 container_erase_symbol (container_t * _self, symbol_t * sym) 
    910 { 
    911   assert (_self != NULL); 
    912   assert (sym != NULL); 
    913   A60_USER_TO_REP (statement, self, *); 
    914   assert (a60_as_container (self)); 
    915  
    916   slist_it_t * pr, * it; 
    917   symbol_t * ret; 
    918  
    919   private_container_find_it (self, symbol_label (sym), symbol_type (sym), &pr, &it); 
    920  
    921   if (pr == NULL && it != NULL) 
    922     // found at the beginning 
    923     { 
    924       ret = slist_popfront (self->block.symtab); 
    925       delete_slist_it (it); 
    926     } 
    927   else 
    928     // found somewhere in the middle 
    929     { 
    930       assert (pr != NULL && it != NULL); 
    931       ret = slist_it_erase_after (pr); 
    932       delete_slist_it (pr); 
    933       delete_slist_it (it); 
    934     } 
    935  
    936   return ret; 
    937 } 
    938  
    939 symbol_t * 
    940 container_find_name (container_t * _self, label_t const * lbl, type_t const * atype) 
    941 { 
    942   assert (_self != NULL); 
    943   assert (lbl != NULL); 
    944   A60_USER_TO_REP (statement, self, *); 
    945   assert (a60_as_container (self)); 
    946  
    947   slist_it_t * it; 
    948   private_container_find_it (self, lbl, atype, NULL, &it); 
    949   symbol_t * ret = slist_it_has (it) ? slist_it_get (it) : NULL; 
    950   delete_slist_it (it); 
    951   return ret; 
    952 } 
    953  
    954 symbol_t * 
    955 container_find_name_rec (container_t * _self, label_t const * lbl, type_t const * atype) 
    956 { 
    957   assert (_self != NULL); 
    958   assert (lbl != NULL); 
    959   A60_USER_TO_REP (statement, self, *); 
    960   assert (a60_as_container (self)); 
    961  
    962   symbol_t * sym = container_find_name (_self, lbl, atype); 
    963   if (sym == NULL && self->parent != NULL) 
    964     return container_find_name_rec (self->parent, lbl, atype); 
    965   else 
    966     return sym; 
    967 } 
    968  
    969 symbol_t * 
    970 container_find_name_rec_add_undefined (container_t * self, label_t const * lbl, 
    971                                        type_t * atype, logger_t * log, 
    972                                        cursor_t * cursor) 
    973 { 
    974   assert (self != NULL); 
    975   assert (lbl != NULL); 
    976   assert (atype != NULL); 
    977   assert (log != NULL); 
    978   assert (a60_as_container (self)); 
    979  
    980   symbol_t * found = container_find_name_rec (self, lbl, atype); 
    981   if (found == NULL) 
    982     { 
    983       if (types_same (atype, type_any ())) 
    984         log_printfc (log, ll_error, cursor, 
    985                      "(1) unknown symbol named `%s'", 
    986                      estr_cstr (label_id (lbl))); 
    987       else 
    988         { 
    989           // second chance: look up any symbol of that name 
    990           found = container_find_name_rec (self, lbl, type_any ()); 
    991           if (found == NULL) 
    992             { 
    993               log_printfc (log, ll_error, cursor, 
    994                            "(2) unknown symbol named `%s'", 
    995                            estr_cstr (label_id (lbl))); 
    996             } 
    997           else 
    998             { 
    999               estring_t * t1s = type_to_str (atype, NULL); 
    1000               estring_t * t2s = type_to_str (symbol_type (found), NULL); 
    1001               estring_t * fmt = 
    1002                 new_estring_fmt ("type mismatch for symbol `%s': " 
    1003                                  "requested type `%s', found type `%s'", 
    1004                                  estr_cstr (label_id (lbl)), 
    1005                                  estr_cstr (t1s), estr_cstr (t2s)); 
    1006               log_printfc (log, ll_error, cursor, "%s", estr_cstr (fmt)); 
    1007               delete_estring (fmt); 
    1008               delete_estring (t2s); 
    1009               delete_estring (t1s); 
    1010             } 
    1011         } 
    1012       int was_there = container_add_symbol (self, new_symbol (lbl), sek_ordinary); 
    1013       assert (!was_there); 
    1014       found = container_find_name (self, lbl, atype); 
    1015       // mark a type at new symbol, either fallback type_int, or 
    1016       // atype, if it has suitable type 
    1017       symbol_set_type (found, is_metatype (atype) ? type_int () : atype); 
    1018     } 
    1019   assert (found != NULL); 
    1020  
    1021   return found; 
    1022 } 
    1023  
    1024 void 
    1025 stmt_toplev_define_internals (container_t * _self) 
    1026 { 
    1027   assert (_self != NULL); 
    1028   A60_USER_TO_REP (statement, self, *); 
    1029   assert (a60_as_container (self)); 
    1030   assert (self->base.kind == sk_toplev); 
    1031  
    1032   struct interfun { 
    1033     char const* n; 
    1034     type_t * t; 
    1035   } builtins [] = 
    1036   { 
    1037     // Algol 60 intrinsics 
    1038     {"abs",  type_proc_real_real ()}, 
    1039     {"abs",  type_proc_int_int ()}, 
    1040     {"sign", type_proc_int_real ()}, 
    1041     {"sign", type_proc_int_int ()}, 
    1042     {"sqrt", type_proc_real_real ()}, 
    1043     {"sin",  type_proc_real_real ()}, 
    1044     {"cos",  type_proc_real_real ()}, 
    1045     {"arctan", type_proc_real_real ()}, 
    1046     {"ln",   type_proc_real_real ()}, 
    1047     {"exp",  type_proc_real_real ()}, 
    1048     {"entier", type_proc_int_real ()}, 
    1049     {"entier", type_proc_real_int ()}, 
    1050     // Extensions 
    1051     {"exit", type_proc_void_int ()}, 
    1052     {"puts", type_proc_int_string ()}, 
    1053     // 
    1054     {NULL,   NULL} 
    1055   }; 
    1056  
    1057   struct interfun * ptr = builtins; 
    1058   for (; ptr->n != NULL; ptr++) 
    1059     { 
    1060       symbol_t * s = new_symbol (new_label (new_estring_from (ptr->n))); 
    1061       symbol_set_type (s, ptr->t); 
    1062       symbol_set_hidden (s, 1); 
    1063       int fail = container_add_symbol (_self, s, sek_internal); 
    1064       assert (fail == 0); 
    1065     } 
    1066759} 
    1067760 
  • trunk/gcc/gcc/algol60/statement.h

    r151 r159  
    2323#include "type.i" 
    2424#include "estring.i" 
     25#include "a60_symtab.i" 
    2526#include "visitor.i" 
    2627#include "pd.h" 
     
    7374container_t * new_stmt_toplev (cursor_t * cursor) 
    7475  ATTRIBUTE_MALLOC; 
    75  
    76 /// Create a copy of subtree starting at given statement. 
    77 /// Cursor is shared, and parent is set to NULL, but other components 
    78 /// are recursively cloned. 
    79 statement_t * clone_statement (statement_t const * self) 
    80   ATTRIBUTE_MALLOC 
    81   ATTRIBUTE_NONNULL(1); 
    82  
    83 /// The same as above, just with typing for containers. 
    84 container_t * clone_container (container_t const * self) 
    85   ATTRIBUTE_MALLOC 
    86   ATTRIBUTE_NONNULL(1); 
    8776 
    8877/// Convert void* to statement, if it is statement, or abort. 
     
    187176 
    188177/// Answer the symtab associated with the container. 
    189 slist_t * container_symtab (container_t const * self) 
     178a60_symtab_t * container_symtab (container_t const * self) 
    190179  ATTRIBUTE_NONNULL(1); 
    191180 
    192181/// Answer the statement list associated with the container. 
    193182slist_t * container_stmts (container_t const * self) 
    194   ATTRIBUTE_NONNULL(1); 
    195  
    196 typedef enum enum_symtab_entry_kind_t 
    197 { 
    198   sek_internal, 
    199   sek_ordinary 
    200 } symtab_entry_kind_t; 
    201  
    202 /// Add given symbol to the container and return 0.  It there already 
    203 /// is other symbol with the same name, do nothing and return -1. 
    204 /// `internal' is sek_internal for internal symbols (internal symbols 
    205 /// may be overloaded) and sek_ordinary for ordinary symbols (which 
    206 /// may not). 
    207 int container_add_symbol (container_t * self, symbol_t * sym, symtab_entry_kind_t internal) 
    208   ATTRIBUTE_NONNULL(1) 
    209   ATTRIBUTE_NONNULL(2); 
    210  
    211 /// Remove the symbol from symtab.  The symbol must be present in 
    212 /// symtab. 
    213 symbol_t * container_erase_symbol (container_t * self, symbol_t * sym) 
    214   ATTRIBUTE_NONNULL(1) 
    215   ATTRIBUTE_NONNULL(2); 
    216  
    217 /// Look up given symbol in the container.  Answer first symbol that 
    218 /// matches restriction `atype', or first symbol with matching name, 
    219 /// if its type is NULL (in that case type can't be checked).  Return 
    220 /// NULL if not found. 
    221 symbol_t * container_find_name (container_t * self, label_t const * lbl, type_t const * atype) 
    222   ATTRIBUTE_NONNULL(1) 
    223   ATTRIBUTE_NONNULL(2); 
    224  
    225 /// Lookup given symbol in the container, and if it fails, recursively 
    226 /// in all parental containers. Answer first symbol that matches 
    227 /// restriction `atype', or first symbol with matching name, if its 
    228 /// type is NULL (in that case type can't be checked). Return NULL if 
    229 /// not found. 
    230 symbol_t * container_find_name_rec (container_t * self, label_t const * lbl, type_t const * atype) 
    231   ATTRIBUTE_NONNULL(1) 
    232   ATTRIBUTE_NONNULL(2); 
    233  
    234 /// See if given symbol with requested matching type, is already 
    235 /// defined somewhere in the scope.  If it's not, create new 
    236 /// definition in most enclosing scope.  Answer either found, or newly 
    237 /// created symbol. 
    238 symbol_t * container_find_name_rec_add_undefined (container_t * self, label_t const * lbl, type_t * atype, logger_t * log, cursor_t * cursor) 
    239   ATTRIBUTE_NONNULL(1) 
    240   ATTRIBUTE_NONNULL(2) 
    241   ATTRIBUTE_NONNULL(3) 
    242   ATTRIBUTE_NONNULL(4); 
    243  
    244 /// Populate given container with algol internal functions.  The 
    245 /// container must be `stmt_toplev`. 
    246 void stmt_toplev_define_internals (container_t * self) 
    247183  ATTRIBUTE_NONNULL(1); 
    248184 
  • trunk/gcc/gcc/algol60/symbol.c

    r151 r159  
    99#include "estring.h" 
    1010#include "visitor.h" 
     11#include "logger.h" 
    1112#include "visitor-impl.h" 
    1213 
    1314static char const * private_symbol_signature = "symbol"; 
     15 
     16typedef struct struct_sym_var_t 
     17{ 
     18} 
     19sym_var_t; 
     20 
     21typedef struct struct_sym_func_t 
     22{ 
     23  /// List typed with a60_as_symbol, contains explicit parameters of 
     24  /// the function. 
     25  slist_t * params; 
     26 
     27  /// List typed with a60_as_symbol, contains symbols that are 
     28  /// unresolved in function body, and as such form implicit 
     29  /// parameters of the function. 
     30  slist_t * implied_params; 
     31 
     32  /// Body of this function. 
     33  statement_t * body; 
     34} 
     35sym_func_t; 
     36 
     37typedef enum enum_symbol_kind_t 
     38{ 
     39  sk_var, 
     40  sk_func, 
     41} 
     42symbol_kind_t; 
    1443 
    1544struct struct_symbol_t 
     
    2150  int hidden; 
    2251  void * extra; 
     52 
     53  union { 
     54    sym_var_t sym_var; 
     55    sym_func_t sym_func; 
     56  }; 
    2357}; 
    2458 
    2559static symbol_t * 
    26 private_alloc_symbol (label_t const * label
     60private_alloc_symbol (label_t const * label, symbol_kind_t kind
    2761{ 
    2862  symbol_t * ret = malloc (sizeof (symbol_t)); 
     
    3064  ret->base.signature = private_symbol_signature; 
    3165#endif 
     66  ret->base.kind = kind; 
    3267  ret->label = label; 
    33   return ret; 
    34 } 
    35  
    36 symbol_t * 
    37 new_symbol (label_t const * label) 
    38 { 
    39   assert (label != NULL); 
    40  
    41   symbol_t * ret = private_alloc_symbol (label); 
    4268  ret->stmt = NULL; 
    4369  ret->type = NULL; 
     
    4874 
    4975symbol_t * 
     76new_symbol_var (label_t const * label) 
     77{ 
     78  assert (label != NULL); 
     79 
     80  symbol_t * ret = private_alloc_symbol (label, sk_var); 
     81  return ret; 
     82} 
     83 
     84symbol_t * 
     85new_symbol_func (label_t const * label) 
     86{ 
     87  assert (label != NULL); 
     88 
     89  symbol_t * ret = private_alloc_symbol (label, sk_func); 
     90  return ret; 
     91} 
     92 
     93symbol_t * 
    5094clone_symbol (symbol_t const * self) 
    5195{ 
     
    59103  assert (label != NULL); 
    60104 
    61   symbol_t * ret = private_alloc_symbol (label); 
     105  symbol_t * ret = private_alloc_symbol (label, self->base.kind); 
    62106  ret->stmt = self->stmt; 
    63107  ret->type = self->type; 
    64108  ret->hidden = self->hidden; 
    65109  ret->extra = self->extra; 
     110 
     111  switch (self->base.kind) 
     112    { 
     113    case sk_var: 
     114      break; 
     115 
     116    case sk_func: 
     117      break; 
     118    } 
     119 
    66120  return ret; 
    67121} 
     
    70124delete_symbol (symbol_t * self) 
    71125{ 
    72   free (self); 
     126  if (self != NULL) 
     127    { 
     128      free (self); 
     129 
     130      switch (self->base.kind) 
     131        { 
     132        case sk_var: 
     133          break; 
     134 
     135        case sk_func: 
     136          break; 
     137        } 
     138    } 
    73139} 
    74140 
     
    97163 
    98164void 
     165symbol_resolve_symbols (symbol_t * self, container_t * context, logger_t * log) 
     166{ 
     167  switch (self->base.kind) 
     168    { 
     169    case sk_var: 
     170      type_resolve_symbols (self->type, context, log); 
     171      return; 
     172 
     173    case sk_func: 
     174      // @TODO: Do the right thing :) 
     175      log_printf (log, ll_info, "note: skipping resolve of function symbol."); 
     176      return; 
     177    } 
     178} 
     179 
     180void 
    99181symbol_set_type (symbol_t * self, type_t * type) 
    100182{ 
  • trunk/gcc/gcc/algol60/symbol.h

    <
    r151 r159  
    1717#include "estring.i" 
    1818#include "visitor.i" 
     19#include "logger.i"