Ignore:
Timestamp:
May 25, 2018, 2:51:06 PM (6 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
new-env, with_gc
Children:
cdc4d43
Parents:
3ef35bd (diff), 58e822a (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge remote-tracking branch 'origin/master' into with_gc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/TypedefTable.cc

    r3ef35bd reba74ba  
    77// TypedefTable.cc --
    88//
    9 // Author           : Rodolfo G. Esteves
     9// Author           : Peter A. Buhr
    1010// Created On       : Sat May 16 15:20:13 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Aug 15 18:24:42 2016
    13 // Update Count     : 25
     12// Last Modified On : Tue May 22 08:40:01 2018
     13// Update Count     : 121
    1414//
    1515
    16 #include <ext/alloc_traits.h>    // for __alloc_traits<>::value_type
    17 #include <cassert>               // for assert
    18 #include <list>                  // for list, _List_iterator, list<>::iterator
    19 #include <map>                   // for _Rb_tree_iterator, _Rb_tree_const_it...
    20 #include <memory>                // for allocator_traits<>::value_type
    21 #include <utility>               // for pair
    2216
    23 #include "Parser/ParserTypes.h"  // for typedefTable
    24 #include "Parser/parser.hh"      // for IDENTIFIER
    2517#include "TypedefTable.h"
    26 
    27 using namespace std;
     18#include <cassert>                                                                              // for assert
    2819
    2920#if 0
    3021#include <iostream>
    31 
    3222#define debugPrint( x ) cerr << x
    3323#else
     
    3525#endif
    3626
    37 TypedefTable::TypedefTable() : currentScope( 0 ) {}
     27using namespace std;                                                                    // string, iostream
    3828
    39 bool TypedefTable::exists( const string &identifier ) {
    40         return table.count( identifier ) > 0;
    41 }
     29TypedefTable::~TypedefTable() {
     30        if ( ! SemanticErrorThrow && kindTable.currentScope() != 0 ) {
     31                // std::cerr << "scope failure " << kindTable.currentScope() << endl;
     32        } // if
     33} // TypedefTable::~TypedefTable
    4234
    43 int TypedefTable::isKind( const string &identifier ) const {
    44         tableType::const_iterator id_pos = table.find( identifier );
     35bool TypedefTable::exists( const string & identifier ) {
     36        return kindTable.find( identifier ) != kindTable.end();
     37} // TypedefTable::exists
     38
     39int TypedefTable::isKind( const string & identifier ) const {
     40        KindTable::const_iterator posn = kindTable.find( identifier );
    4541        // Name lookup defaults to identifier, and then the identifier's kind is set by the parser.
    46         if ( id_pos == table.end() ) return IDENTIFIER;
    47         return id_pos->second.begin()->kind;
    48 }
     42        if ( posn == kindTable.end() ) return IDENTIFIER;
     43        return posn->second;
     44} // TypedefTable::isKind
    4945
    50 void TypedefTable::changeKind( const string &identifier, kind_t kind ) {
    51         tableType::iterator id_pos = table.find( identifier );
    52         if ( id_pos == table.end() ) return;
    53         id_pos->second.begin()->kind = kind;
    54 }
     46void TypedefTable::changeKind( const string & identifier, int kind ) {
     47        KindTable::iterator posn = kindTable.find( identifier );
     48        if ( posn != kindTable.end() ) posn->second = kind;     // exists => update
     49} // TypedefTable::changeKind
    5550
    5651// SKULLDUGGERY: Generate a typedef for the aggregate name so the aggregate does not have to be qualified by
    57 // "struct". Only generate the typedef, if the name is not in use. The typedef is implicitly (silently) removed
    58 // if the name is explicitly used.
    59 void TypedefTable::makeTypedef( const string &name ) {
     52// "struct". Only generate the typedef, if the name is not in use. The typedef is implicitly (silently) removed if the
     53// name is explicitly used.
     54void TypedefTable::makeTypedef( const string & name ) {
    6055        if ( ! typedefTable.exists( name ) ) {
    61                 typedefTable.addToEnclosingScope( name, TypedefTable::TD );
     56                typedefTable.addToEnclosingScope( name, TYPEDEFname );
    6257        } // if
    63 }
     58} // TypedefTable::makeTypedef
    6459
    65 void TypedefTable::addToScope( const std::string &identifier, kind_t kind, int scope ) {
    66         if ( currentTrait != "" && scope == contextScope ) {
    67                 DeferredEntry entry = { identifier, kind };
    68                 contexts[currentTrait].push_back( entry );
    69         } else {
    70                 debugPrint( "Adding " << identifier << " as kind " << kind << " scope " << scope << " from scope " << currentScope << endl );
    71                 Entry newEntry = { scope, kind };
    72                 tableType::iterator curPos = table.find( identifier );
    73                 if ( curPos == table.end()) {
    74                         list< Entry > newList;
    75                         newList.push_front( newEntry );
    76                         table[identifier] = newList;
    77                 } else {
    78                         list< Entry >::iterator listPos = (*curPos ).second.begin();
    79                         while ( listPos != (*curPos ).second.end() && listPos->scope > scope ) {
    80                                 listPos++;
    81                         } // while
    82                         (*curPos ).second.insert( listPos, newEntry );
    83                 } // if
    84         } // if
    85 }
    86 
    87 void TypedefTable::addToCurrentScope( const std::string &identifier, kind_t kind ) {
    88         addToScope( identifier, kind, currentScope );
    89 }
    90 
    91 void TypedefTable::addToCurrentScope( kind_t kind ) {
    92         addToCurrentScope( nextIdentifiers.top(), kind );
    93 }
    94 
    95 void TypedefTable::addToEnclosingScope( const std::string &identifier, kind_t kind ) {
    96         assert( currentScope >= 1 );
    97         addToScope( identifier, kind, currentScope - 1 );
    98 }
    99 
    100 void TypedefTable::addToEnclosingScope( kind_t kind ) {
    101         addToEnclosingScope( nextIdentifiers.top(), kind );
    102 }
    103 
    104 void TypedefTable::addToEnclosingScope2( const std::string &identifier, kind_t kind ) {
    105         assert( currentScope >= 2 );
    106         addToScope( identifier, kind, currentScope - 2 );
    107 }
    108 
    109 void TypedefTable::addToEnclosingScope2( kind_t kind ) {
    110         addToEnclosingScope2( nextIdentifiers.top(), kind );
    111 }
    112 
    113 void TypedefTable::setNextIdentifier( const std::string &identifier ) {
    114         nextIdentifiers.top() = identifier;
    115 }
    116 
    117 void TypedefTable::openTrait( const std::string &contextName ) {
    118         map< string, deferListType >::iterator i = contexts.find( contextName );
    119         if ( i != contexts.end() ) {
    120                 deferListType &entries = i->second;
    121                 for ( deferListType::iterator i = entries.begin(); i != entries.end(); i++) {
    122                         addToEnclosingScope( i->identifier, i->kind );
    123                 } // for
    124         } // if
    125 }
     60void TypedefTable::addToEnclosingScope( const std::string & identifier, int kind ) {
     61        assert( kindTable.currentScope() >= 1 );
     62        auto scope = kindTable.currentScope() - 1;
     63        debugPrint( "Adding " << identifier << " as kind " << kind << " scope " << scope << endl );
     64        auto ret = kindTable.insertAt( scope, identifier, kind );
     65        if ( ! ret.second ) ret.first->second = kind;           // exists => update
     66} // TypedefTable::addToEnclosingScope
    12667
    12768void TypedefTable::enterScope() {
    128         currentScope += 1;
    129         deferListStack.push( deferListType() );
    130         nextIdentifiers.push( "" );
    131         debugPrint( "Entering scope " << currentScope << ", nextIdentifiers size is " << nextIdentifiers.size() << endl );
    132 }
     69        kindTable.beginScope();
     70        debugPrint( "Entering scope " << kindTable.currentScope() << endl );
     71} // TypedefTable::enterScope
    13372
    13473void TypedefTable::leaveScope() {
    135         debugPrint( "Leaving scope " << currentScope << endl );
    136         for ( tableType::iterator i = table.begin(); i != table.end(); ) {
    137                 list< Entry > &declList = (*i).second;
    138                 while ( ! declList.empty() && declList.front().scope == currentScope ) {
    139                         declList.pop_front();
    140                 }
    141                 if ( declList.empty() ) {                                               // standard idom for erasing during traversal
    142                         table.erase( i++ );
    143                 } else
    144                         ++i;
    145         } // for
    146         currentScope -= 1;
    147         for ( deferListType::iterator i = deferListStack.top().begin(); i != deferListStack.top().end(); i++ ) {
    148                 addToCurrentScope( i->identifier, i->kind );
    149         } // for
    150         deferListStack.pop();
    151         debugPrint( "nextIdentifiers size is " << nextIdentifiers.size() << " top is " << nextIdentifiers.top() << endl );
    152         nextIdentifiers.pop();
    153 }
     74        debugPrint( "Leaving scope " << kindTable.currentScope() << endl );
     75        kindTable.endScope();
     76} // TypedefTable::leaveScope
    15477
    155 void TypedefTable::enterTrait( const std::string &contextName ) {
    156         currentTrait = contextName;
    157         contextScope = currentScope;
    158 }
    159 
    160 void TypedefTable::leaveTrait() {
    161         currentTrait = "";
    162 }
    163 
    164 void TypedefTable::print( void ) const {
    165         for ( tableType::const_iterator i = table.begin(); i != table.end(); i++) {
    166                 debugPrint( (*i ).first << ": " );
    167                 list< Entry > declList = (*i).second;
    168                 for ( list< Entry >::const_iterator j = declList.begin(); j != declList.end(); j++ ) {
    169                         debugPrint( "(" << (*j).scope << " " << (*j).kind << ") " );
    170                 }
    171                 debugPrint( endl );
    172         } // for
    173 }
     78// void TypedefTable::print( void ) const {
     79//      for ( KindTable::const_iterator i = table.begin(); i != table.end(); i++) {
     80//              debugPrint( (*i ).first << ": " );
     81//              list< Entry > declList = (*i).second;
     82//              for ( list< Entry >::const_iterator j = declList.begin(); j != declList.end(); j++ ) {
     83//                      debugPrint( "(" << (*j).scope << " " << (*j).kind << ") " );
     84//              }
     85//              debugPrint( endl );
     86//      } // for
     87// }
    17488
    17589// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.