Ignore:
Timestamp:
Aug 27, 2018, 4:40:34 PM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
b7c89aa
Parents:
f9feab8 (diff), 305581d (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 branch 'master' into cleanup-dtors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/TypedefTable.cc

    rf9feab8 r90152a4  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // TypedefTable.cc -- 
     7// 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 : Wed Jul 25 15:32:35 2018
     13// Update Count     : 258
    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
     19#include <iostream>
    2820
    2921#if 0
    30 #include <iostream>
    31 
    32 #define debugPrint( x ) cerr << x
     22#define debugPrint( code ) code
    3323#else
    34 #define debugPrint( x )
     24#define debugPrint( code )
    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 }
     29debugPrint(
     30static const char *kindName( int kind ) {
     31        switch ( kind ) {
     32          case IDENTIFIER: return "identifier";
     33          case TYPEDEFname: return "typedef";
     34          case TYPEGENname: return "typegen";
     35          default:
     36                cerr << "Error: cfa-cpp internal error, invalid kind of identifier" << endl;
     37                abort();
     38        } // switch
     39} // kindName
     40)
    4241
    43 int TypedefTable::isKind( const string &identifier ) const {
    44         tableType::const_iterator id_pos = table.find( identifier );
     42TypedefTable::~TypedefTable() {
     43        if ( ! SemanticErrorThrow && kindTable.currentScope() != 0 ) {
     44                cerr << "Error: cfa-cpp internal error, scope failure " << kindTable.currentScope() << endl;
     45                abort();
     46        } // if
     47} // TypedefTable::~TypedefTable
     48
     49bool TypedefTable::exists( const string & identifier ) {
     50        return kindTable.find( identifier ) != kindTable.end();
     51} // TypedefTable::exists
     52
     53bool TypedefTable::existsCurr( const string & identifier ) {
     54        return kindTable.findAt( kindTable.currentScope() - 1, identifier ) != kindTable.end();
     55} // TypedefTable::exists
     56
     57int TypedefTable::isKind( const string & identifier ) const {
     58        KindTable::const_iterator posn = kindTable.find( identifier );
    4559        // 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 }
    49 
    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 }
     60        if ( posn == kindTable.end() ) return IDENTIFIER;
     61        return posn->second;
     62} // TypedefTable::isKind
    5563
    5664// 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 ) {
     65// "struct". Only generate the typedef, if the name is not in use. The typedef is implicitly (silently) removed if the
     66// name is explicitly used.
     67void TypedefTable::makeTypedef( const string & name, int kind ) {
     68//    Check for existence is necessary to handle:
     69//        struct Fred {};
     70//        void Fred();
     71//        void fred() {
     72//           struct Fred act; // do not add as type in this scope
     73//           Fred();
     74//        }
    6075        if ( ! typedefTable.exists( name ) ) {
    61                 typedefTable.addToEnclosingScope( name, TypedefTable::TD );
     76                typedefTable.addToEnclosingScope( name, kind, "MTD" );
    6277        } // if
    63 }
     78} // TypedefTable::makeTypedef
    6479
    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 }
     80void TypedefTable::addToScope( const string & identifier, int kind, const char * locn __attribute__((unused)) ) {
     81        auto scope = kindTable.currentScope();
     82        debugPrint( cerr << "Adding current at " << locn << " " << identifier << " as " << kindName( kind ) << " scope " << scope << endl );
     83        kindTable.insertAt( scope, identifier, kind );
     84} // TypedefTable::addToScope
    8685
    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 }
     86void TypedefTable::addToEnclosingScope( const string & identifier, int kind, const char * locn __attribute__((unused)) ) {
     87        auto scope = kindTable.currentScope() - 1 - kindTable.getNote( kindTable.currentScope() - 1 ).level;
     88//      auto scope = level - kindTable.getNote( kindTable.currentScope() - 1 ).level;
     89        debugPrint( cerr << "Adding enclosing at " << locn << " " << identifier << " as " << kindName( kind ) << " scope " << scope << " level " << level << " note " << kindTable.getNote( kindTable.currentScope() - 1 ).level << endl );
     90        auto ret = kindTable.insertAt( scope, identifier, kind );
     91        if ( ! ret.second ) ret.first->second = kind;   // exists => update
     92} // TypedefTable::addToEnclosingScope
    12693
    12794void TypedefTable::enterScope() {
    128         currentScope += 1;
    129         deferListStack.push( deferListType() );
    130         nextIdentifiers.push( "" );
    131         debugPrint( "Entering scope " << currentScope << ", nextIdentifiers size is " << nextIdentifiers.size() << endl );
    132 }
     95        kindTable.beginScope( (Note){ 0, false } );
     96        debugPrint( cerr << "Entering scope " << kindTable.currentScope() << " level " << level << endl; print() );
     97} // TypedefTable::enterScope
    13398
    13499void 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 }
     100        debugPrint( cerr << "Leaving scope " << kindTable.currentScope() << endl; print() );
     101        kindTable.endScope();
     102} // TypedefTable::leaveScope
    154103
    155 void TypedefTable::enterTrait( const std::string &contextName ) {
    156         currentTrait = contextName;
    157         contextScope = currentScope;
    158 }
     104void TypedefTable::up( bool forall ) {
     105        level += 1;
     106        kindTable.getNote( kindTable.currentScope() ) = (Note){ level, forall || getEnclForall() };
     107        debugPrint( cerr << "Up " << " level " << level << " note " << kindTable.getNote( level ).level << ", " << kindTable.getNote( level ).forall << endl; );
     108} // TypedefTable::up
    159109
    160 void TypedefTable::leaveTrait() {
    161         currentTrait = "";
    162 }
     110void TypedefTable::down() {
     111        level -= 1;
     112        debugPrint( cerr << "Down " << " level " << level << " note " << kindTable.getNote( level ).level << endl; );
     113} // TypedefTable::down
    163114
    164115void 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 );
     116        KindTable::size_type scope = kindTable.currentScope();
     117        debugPrint( cerr << "[" << scope << "] " << kindTable.getNote( scope ).level << ", " << kindTable.getNote( scope ).forall << ":" );
     118        for ( KindTable::const_iterator i = kindTable.begin(); i != kindTable.end(); i++ ) {
     119                while ( i.get_level() != scope ) {
     120                        --scope;
     121                        debugPrint( cerr << endl << "[" << scope << "] " << kindTable.getNote( scope ).level << ", " << kindTable.getNote( scope ).forall << ":" );
     122                } // while
     123                debugPrint( cerr << " " << (*i).first << ":" << kindName( (*i).second ) );
    172124        } // for
    173 }
     125        while ( scope > 0 ) {
     126                --scope;
     127                debugPrint( cerr << endl << "[" << scope << "] " << kindTable.getNote( scope ).level << ", " << kindTable.getNote( scope ).forall << ":" );
     128        } // while
     129        debugPrint( cerr << endl );
     130} // TypedefTable::print
    174131
    175132// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.