Ignore:
Timestamp:
Jan 7, 2015, 6:04:42 PM (10 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
Children:
0b8cd722
Parents:
d9a0e76
Message:

fixed restrict, fixed parameter copy, introduced name table for types, changed variable after to string

File:
1 edited

Legend:

Unmodified
Added
Removed
  • translator/SymTab/Indexer.cc

    rd9a0e76 r17cd4eb  
    1 /*
    2  * This file is part of the Cforall project
    3  *
    4  * $Id: Indexer.cc,v 1.12 2005/08/29 20:14:17 rcbilson Exp $
    5  *
    6  */
    7 
    81#include "SynTree/Declaration.h"
    92#include "SynTree/Type.h"
     
    158#include "utility.h"
    169
    17 #define debugPrint(x) if( doDebug ) { std::cout << x; }
     10#define debugPrint(x) if ( doDebug ) { std::cout << x; }
    1811
    1912namespace SymTab {
    20 
    21 Indexer::Indexer( bool useDebug )
    22   : doDebug( useDebug )
    23 {
    24 }
    25 
    26 Indexer::~Indexer()
    27 {
    28 }
    29 
    30 void
    31 Indexer::visit( ObjectDecl *objectDecl )
    32 {
    33   maybeAccept( objectDecl->get_type(), *this );
    34   maybeAccept( objectDecl->get_init(), *this );
    35   maybeAccept( objectDecl->get_bitfieldWidth(), *this );
    36   if( objectDecl->get_name() != "" ) {
    37     debugPrint( "Adding object " << objectDecl->get_name() << std::endl );
    38     idTable.addDecl( objectDecl );
    39   }
    40 }
    41 
    42 void
    43 Indexer::visit( FunctionDecl *functionDecl )
    44 {
    45   if( functionDecl->get_name() == "" ) return;
    46   debugPrint( "Adding function " << functionDecl->get_name() << std::endl );
    47   idTable.addDecl( functionDecl );
    48   enterScope();
    49   maybeAccept( functionDecl->get_functionType(), *this );
    50   acceptAll( functionDecl->get_oldDecls(), *this );
    51   maybeAccept( functionDecl->get_statements(), *this );
    52   leaveScope();
    53 }
     13    Indexer::Indexer( bool useDebug ) : doDebug( useDebug ) {}
     14
     15    Indexer::~Indexer() {}
     16
     17    void Indexer::visit( ObjectDecl *objectDecl ) {
     18        maybeAccept( objectDecl->get_type(), *this );
     19        maybeAccept( objectDecl->get_init(), *this );
     20        maybeAccept( objectDecl->get_bitfieldWidth(), *this );
     21        if ( objectDecl->get_name() != "" ) {
     22            debugPrint( "Adding object " << objectDecl->get_name() << std::endl );
     23            idTable.addDecl( objectDecl );
     24        }
     25    }
     26
     27    void Indexer::visit( FunctionDecl *functionDecl ) {
     28        if ( functionDecl->get_name() == "" ) return;
     29        debugPrint( "Adding function " << functionDecl->get_name() << std::endl );
     30        idTable.addDecl( functionDecl );
     31        enterScope();
     32        maybeAccept( functionDecl->get_functionType(), *this );
     33        acceptAll( functionDecl->get_oldDecls(), *this );
     34        maybeAccept( functionDecl->get_statements(), *this );
     35        leaveScope();
     36    }
    5437
    5538/********
     
    7255 */
    7356
    74 void
    75 Indexer::visit( TypeDecl *typeDecl )
    76 {
    77   // see A NOTE ON THE ORDER OF TRAVERSAL, above
    78   // note that assertions come after the type is added to the symtab, since they aren't part
    79   // of the type proper and may depend on the type itself
    80   enterScope();
    81   acceptAll( typeDecl->get_parameters(), *this );
    82   maybeAccept( typeDecl->get_base(), *this );
    83   leaveScope();
    84   debugPrint( "Adding type " << typeDecl->get_name() << std::endl );
    85   typeTable.add( typeDecl );
    86   acceptAll( typeDecl->get_assertions(), *this );
    87 }
    88 
    89 void
    90 Indexer::visit( TypedefDecl *typeDecl )
    91 {
    92   enterScope();
    93   acceptAll( typeDecl->get_parameters(), *this );
    94   maybeAccept( typeDecl->get_base(), *this );
    95   leaveScope();
    96   debugPrint( "Adding typedef " << typeDecl->get_name() << std::endl );
    97   typeTable.add( typeDecl );
    98 }
    99 
    100 void
    101 Indexer::visit( StructDecl *aggregateDecl )
    102 {
    103   // make up a forward declaration and add it before processing the members
    104   StructDecl fwdDecl( aggregateDecl->get_name() );
    105   cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() );
    106   debugPrint( "Adding fwd decl for struct " << fwdDecl.get_name() << std::endl );
    107   structTable.add( &fwdDecl );
    108  
    109   enterScope();
    110   acceptAll( aggregateDecl->get_parameters(), *this );
    111   acceptAll( aggregateDecl->get_members(), *this );
    112   leaveScope();
    113  
    114   debugPrint( "Adding struct " << aggregateDecl->get_name() << std::endl );
    115   // this addition replaces the forward declaration
    116   structTable.add( aggregateDecl );
    117 }
    118 
    119 void
    120 Indexer::visit( UnionDecl *aggregateDecl )
    121 {
    122   // make up a forward declaration and add it before processing the members
    123   UnionDecl fwdDecl( aggregateDecl->get_name() );
    124   cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() );
    125   debugPrint( "Adding fwd decl for union " << fwdDecl.get_name() << std::endl );
    126   unionTable.add( &fwdDecl );
    127  
    128   enterScope();
    129   acceptAll( aggregateDecl->get_parameters(), *this );
    130   acceptAll( aggregateDecl->get_members(), *this );
    131   leaveScope();
    132  
    133   debugPrint( "Adding union " << aggregateDecl->get_name() << std::endl );
    134   unionTable.add( aggregateDecl );
    135 }
    136 
    137 void
    138 Indexer::visit( EnumDecl *aggregateDecl )
    139 {
    140   debugPrint( "Adding enum " << aggregateDecl->get_name() << std::endl );
    141   enumTable.add( aggregateDecl );
    142   // unlike structs, contexts, and unions, enums inject their members into the
    143   // global scope
    144   acceptAll( aggregateDecl->get_members(), *this );
    145 }
    146 
    147 void
    148 Indexer::visit( ContextDecl *aggregateDecl )
    149 {
    150   enterScope();
    151   acceptAll( aggregateDecl->get_parameters(), *this );
    152   acceptAll( aggregateDecl->get_members(), *this );
    153   leaveScope();
    154  
    155   debugPrint( "Adding context " << aggregateDecl->get_name() << std::endl );
    156   contextTable.add( aggregateDecl );
    157 }
    158 
    159 void
    160 Indexer::visit( CompoundStmt *compoundStmt )
    161 {
    162   enterScope();
    163   acceptAll( compoundStmt->get_kids(), *this );
    164   leaveScope();
    165 }
    166 
    167 void
    168 Indexer::visit( ContextInstType *contextInst )
    169 {
    170   acceptAll( contextInst->get_parameters(), *this );
    171   acceptAll( contextInst->get_members(), *this );
    172 }
    173 
    174 void
    175 Indexer::visit( StructInstType *structInst )
    176 {
    177   if( !structTable.lookup( structInst->get_name() ) ) {
    178     debugPrint( "Adding struct " << structInst->get_name() << " from implicit forward declaration" << std::endl );
    179     structTable.add( structInst->get_name() );
    180   }
    181   enterScope();
    182   acceptAll( structInst->get_parameters(), *this );
    183   leaveScope();
    184 }
    185 
    186 void
    187 Indexer::visit( UnionInstType *unionInst )
    188 {
    189   if( !unionTable.lookup( unionInst->get_name() ) ) {
    190     debugPrint( "Adding union " << unionInst->get_name() << " from implicit forward declaration" << std::endl );
    191     unionTable.add( unionInst->get_name() );
    192   }
    193   enterScope();
    194   acceptAll( unionInst->get_parameters(), *this );
    195   leaveScope();
    196 }
    197 
    198 void
    199 Indexer::lookupId( const std::string &id, std::list< DeclarationWithType* > &list ) const
    200 {
    201   idTable.lookupId( id, list );
    202 }
    203 
    204 NamedTypeDecl *
    205 Indexer::lookupType( const std::string &id ) const
    206 {
    207   return typeTable.lookup( id );
    208 }
    209 
    210 StructDecl *
    211 Indexer::lookupStruct( const std::string &id ) const
    212 {
    213   return structTable.lookup( id );
    214 }
    215 
    216 EnumDecl *
    217 Indexer::lookupEnum( const std::string &id ) const
    218 {
    219   return enumTable.lookup( id );
    220 }
    221 
    222 UnionDecl *
    223 Indexer::lookupUnion( const std::string &id ) const
    224 {
    225   return unionTable.lookup( id );
    226 }
    227 
    228 ContextDecl *
    229 Indexer::lookupContext( const std::string &id ) const
    230 {
    231   return contextTable.lookup( id );
    232 }
    233 
    234 void
    235 Indexer::enterScope()
    236 {
    237   if( doDebug ) {
    238     std::cout << "--- Entering scope" << std::endl;
    239   }
    240   idTable.enterScope();
    241   typeTable.enterScope();
    242   structTable.enterScope();
    243   enumTable.enterScope();
    244   unionTable.enterScope();
    245   contextTable.enterScope();
    246 }
    247 
    248 void
    249 Indexer::leaveScope()
    250 {
    251   using std::cout;
    252   using std::endl;
    253  
    254   if( doDebug ) {
    255     cout << "--- Leaving scope containing" << endl;
    256     idTable.dump( cout );
    257     typeTable.dump( cout );
    258     structTable.dump( cout );
    259     enumTable.dump( cout );
    260     unionTable.dump( cout );
    261     contextTable.dump( cout );
    262   }
    263   idTable.leaveScope();
    264   typeTable.leaveScope();
    265   structTable.leaveScope();
    266   enumTable.leaveScope();
    267   unionTable.leaveScope();
    268   contextTable.leaveScope();
    269 }
    270 
    271 void
    272 Indexer::print( std::ostream &os, int indent ) const
    273 {
    274     idTable.dump( os );
    275     typeTable.dump( os );
    276     structTable.dump( os );
    277     enumTable.dump( os );
    278     unionTable.dump( os );
    279     contextTable.dump( os );
    280 }
    281 
     57    void Indexer::visit( TypeDecl *typeDecl ) {
     58        // see A NOTE ON THE ORDER OF TRAVERSAL, above
     59        // note that assertions come after the type is added to the symtab, since they aren't part
     60        // of the type proper and may depend on the type itself
     61        enterScope();
     62        acceptAll( typeDecl->get_parameters(), *this );
     63        maybeAccept( typeDecl->get_base(), *this );
     64        leaveScope();
     65        debugPrint( "Adding type " << typeDecl->get_name() << std::endl );
     66        typeTable.add( typeDecl );
     67        acceptAll( typeDecl->get_assertions(), *this );
     68    }
     69
     70    void Indexer::visit( TypedefDecl *typeDecl ) {
     71        enterScope();
     72        acceptAll( typeDecl->get_parameters(), *this );
     73        maybeAccept( typeDecl->get_base(), *this );
     74        leaveScope();
     75        debugPrint( "Adding typedef " << typeDecl->get_name() << std::endl );
     76        typeTable.add( typeDecl );
     77    }
     78
     79    void Indexer::visit( StructDecl *aggregateDecl ) {
     80        // make up a forward declaration and add it before processing the members
     81        StructDecl fwdDecl( aggregateDecl->get_name() );
     82        cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() );
     83        debugPrint( "Adding fwd decl for struct " << fwdDecl.get_name() << std::endl );
     84        structTable.add( &fwdDecl );
     85 
     86        enterScope();
     87        acceptAll( aggregateDecl->get_parameters(), *this );
     88        acceptAll( aggregateDecl->get_members(), *this );
     89        leaveScope();
     90 
     91        debugPrint( "Adding struct " << aggregateDecl->get_name() << std::endl );
     92        // this addition replaces the forward declaration
     93        structTable.add( aggregateDecl );
     94    }
     95
     96    void Indexer::visit( UnionDecl *aggregateDecl ) {
     97        // make up a forward declaration and add it before processing the members
     98        UnionDecl fwdDecl( aggregateDecl->get_name() );
     99        cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() );
     100        debugPrint( "Adding fwd decl for union " << fwdDecl.get_name() << std::endl );
     101        unionTable.add( &fwdDecl );
     102 
     103        enterScope();
     104        acceptAll( aggregateDecl->get_parameters(), *this );
     105        acceptAll( aggregateDecl->get_members(), *this );
     106        leaveScope();
     107 
     108        debugPrint( "Adding union " << aggregateDecl->get_name() << std::endl );
     109        unionTable.add( aggregateDecl );
     110    }
     111
     112    void Indexer::visit( EnumDecl *aggregateDecl ) {
     113        debugPrint( "Adding enum " << aggregateDecl->get_name() << std::endl );
     114        enumTable.add( aggregateDecl );
     115        // unlike structs, contexts, and unions, enums inject their members into the global scope
     116        acceptAll( aggregateDecl->get_members(), *this );
     117    }
     118
     119    void Indexer::visit( ContextDecl *aggregateDecl ) {
     120        enterScope();
     121        acceptAll( aggregateDecl->get_parameters(), *this );
     122        acceptAll( aggregateDecl->get_members(), *this );
     123        leaveScope();
     124 
     125        debugPrint( "Adding context " << aggregateDecl->get_name() << std::endl );
     126        contextTable.add( aggregateDecl );
     127    }
     128
     129    void Indexer::visit( CompoundStmt *compoundStmt ) {
     130        enterScope();
     131        acceptAll( compoundStmt->get_kids(), *this );
     132        leaveScope();
     133    }
     134
     135    void Indexer::visit( ContextInstType *contextInst ) {
     136        acceptAll( contextInst->get_parameters(), *this );
     137        acceptAll( contextInst->get_members(), *this );
     138    }
     139
     140    void Indexer::visit( StructInstType *structInst ) {
     141        if ( ! structTable.lookup( structInst->get_name() ) ) {
     142            debugPrint( "Adding struct " << structInst->get_name() << " from implicit forward declaration" << std::endl );
     143            structTable.add( structInst->get_name() );
     144        }
     145        enterScope();
     146        acceptAll( structInst->get_parameters(), *this );
     147        leaveScope();
     148    }
     149
     150    void Indexer::visit( UnionInstType *unionInst ) {
     151        if ( ! unionTable.lookup( unionInst->get_name() ) ) {
     152            debugPrint( "Adding union " << unionInst->get_name() << " from implicit forward declaration" << std::endl );
     153            unionTable.add( unionInst->get_name() );
     154        }
     155        enterScope();
     156        acceptAll( unionInst->get_parameters(), *this );
     157        leaveScope();
     158    }
     159
     160    void Indexer::lookupId( const std::string &id, std::list< DeclarationWithType* > &list ) const {
     161        idTable.lookupId( id, list );
     162    }
     163
     164    NamedTypeDecl *Indexer::lookupType( const std::string &id ) const {
     165        return typeTable.lookup( id );
     166    }
     167
     168    StructDecl *Indexer::lookupStruct( const std::string &id ) const {
     169        return structTable.lookup( id );
     170    }
     171
     172    EnumDecl *Indexer::lookupEnum( const std::string &id ) const {
     173        return enumTable.lookup( id );
     174    }
     175
     176    UnionDecl *Indexer::lookupUnion( const std::string &id ) const {
     177        return unionTable.lookup( id );
     178    }
     179
     180    ContextDecl  * Indexer::lookupContext( const std::string &id ) const {
     181        return contextTable.lookup( id );
     182    }
     183
     184    void Indexer::enterScope() {
     185        if ( doDebug ) {
     186            std::cout << "--- Entering scope" << std::endl;
     187        }
     188        idTable.enterScope();
     189        typeTable.enterScope();
     190        structTable.enterScope();
     191        enumTable.enterScope();
     192        unionTable.enterScope();
     193        contextTable.enterScope();
     194    }
     195
     196    void Indexer::leaveScope() {
     197        using std::cout;
     198        using std::endl;
     199 
     200        if ( doDebug ) {
     201            cout << "--- Leaving scope containing" << endl;
     202            idTable.dump( cout );
     203            typeTable.dump( cout );
     204            structTable.dump( cout );
     205            enumTable.dump( cout );
     206            unionTable.dump( cout );
     207            contextTable.dump( cout );
     208        }
     209        idTable.leaveScope();
     210        typeTable.leaveScope();
     211        structTable.leaveScope();
     212        enumTable.leaveScope();
     213        unionTable.leaveScope();
     214        contextTable.leaveScope();
     215    }
     216
     217    void Indexer::print( std::ostream &os, int indent ) const {
     218        idTable.dump( os );
     219        typeTable.dump( os );
     220        structTable.dump( os );
     221        enumTable.dump( os );
     222        unionTable.dump( os );
     223        contextTable.dump( os );
     224    }
    282225} // namespace SymTab
Note: See TracChangeset for help on using the changeset viewer.