| 1 | //
|
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
|
|---|
| 3 | //
|
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
|---|
| 5 | // file "LICENCE" distributed with Cforall.
|
|---|
| 6 | //
|
|---|
| 7 | // TypedefTable.cc --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Rodolfo G. Esteves
|
|---|
| 10 | // Created On : Sat May 16 15:20:13 2015
|
|---|
| 11 | // Last Modified By : Peter A. Buhr
|
|---|
| 12 | // Last Modified On : Mon Aug 15 18:24:42 2016
|
|---|
| 13 | // Update Count : 25
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 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
|
|---|
| 22 |
|
|---|
| 23 | #include "Parser/ParserTypes.h" // for typedefTable
|
|---|
| 24 | #include "Parser/parser.hh" // for IDENTIFIER
|
|---|
| 25 | #include "TypedefTable.h"
|
|---|
| 26 |
|
|---|
| 27 | using namespace std;
|
|---|
| 28 |
|
|---|
| 29 | #if 0
|
|---|
| 30 | #include <iostream>
|
|---|
| 31 |
|
|---|
| 32 | #define debugPrint( x ) cerr << x
|
|---|
| 33 | #else
|
|---|
| 34 | #define debugPrint( x )
|
|---|
| 35 | #endif
|
|---|
| 36 |
|
|---|
| 37 | TypedefTable::TypedefTable() : currentScope( 0 ) {}
|
|---|
| 38 |
|
|---|
| 39 | bool TypedefTable::exists( const string &identifier ) {
|
|---|
| 40 | return table.count( identifier ) > 0;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | int TypedefTable::isKind( const string &identifier ) const {
|
|---|
| 44 | tableType::const_iterator id_pos = table.find( identifier );
|
|---|
| 45 | // 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 | }
|
|---|
| 55 |
|
|---|
| 56 | // 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 ) {
|
|---|
| 60 | if ( ! typedefTable.exists( name ) ) {
|
|---|
| 61 | typedefTable.addToEnclosingScope( name, TypedefTable::TD );
|
|---|
| 62 | } // if
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 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 | }
|
|---|
| 126 |
|
|---|
| 127 | void TypedefTable::enterScope() {
|
|---|
| 128 | currentScope += 1;
|
|---|
| 129 | deferListStack.push( deferListType() );
|
|---|
| 130 | nextIdentifiers.push( "" );
|
|---|
| 131 | debugPrint( "Entering scope " << currentScope << ", nextIdentifiers size is " << nextIdentifiers.size() << endl );
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | void 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 | }
|
|---|
| 154 |
|
|---|
| 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 | }
|
|---|
| 174 |
|
|---|
| 175 | // Local Variables: //
|
|---|
| 176 | // tab-width: 4 //
|
|---|
| 177 | // mode: c++ //
|
|---|
| 178 | // compile-command: "make install" //
|
|---|
| 179 | // End: //
|
|---|