[dc12481] | 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 | // ScopedMap.h -- |
---|
| 8 | // |
---|
| 9 | // Author : Aaron B. Moss |
---|
| 10 | // Created On : Fri Feb 19 13:55:00 2016 |
---|
| 11 | // Last Modified By : Aaron B. Moss |
---|
| 12 | // Last Modified On : Fri Feb 19 13:55:00 2016 |
---|
| 13 | // Update Count : 1 |
---|
| 14 | // |
---|
| 15 | |
---|
| 16 | #ifndef _TYPEMAP_H |
---|
| 17 | #define _TYPEMAP_H |
---|
| 18 | |
---|
| 19 | #include <map> |
---|
| 20 | #include <string> |
---|
| 21 | #include <utility> |
---|
| 22 | #include <vector> |
---|
| 23 | |
---|
| 24 | #include "SynTree/Type.h" |
---|
| 25 | #include "SynTree/Visitor.h" |
---|
| 26 | |
---|
| 27 | namespace ResolvExpr { |
---|
| 28 | |
---|
| 29 | /// A map from types to some value; lookup is done by structural decomposition on types. |
---|
| 30 | /// The TypeMap stores its values by reference, so stored objects should be kept alive by the caller. |
---|
| 31 | /// The scoping mechanism essentially by keeping a list of changes to roll back, then rolling them back |
---|
| 32 | /// WARNING: This map is only incompletely and approximately consistent with the resolution rules in Unify.cc; |
---|
| 33 | /// it has potential, if extended, to form the basis of a resolver that is more performant than the current |
---|
| 34 | /// linear-search-by-unification approach, but is only currently used for finding assignment operators in GenPoly::box. |
---|
| 35 | template< typename Value > |
---|
| 36 | class TypeMap { |
---|
| 37 | /// Map of names to types |
---|
| 38 | typedef typename std::map< std::string, Value* > ValueMap; |
---|
| 39 | typedef typename ValueMap::iterator ValueMapIterator; |
---|
[62e5546] | 40 | |
---|
[dc12481] | 41 | Value *voidValue; ///< Value for void type |
---|
| 42 | Value *basicValue[BasicType::NUMBER_OF_BASIC_TYPES]; ///< Values for basic types |
---|
| 43 | Value *pointerValue; ///< Value for all pointer types |
---|
| 44 | Value *voidPointerValue; ///< Value for void* types |
---|
| 45 | Value *functionPointerValue; ///< Value for all function pointer types |
---|
| 46 | ValueMap structValue; ///< Values for struct types, indexed by name |
---|
| 47 | ValueMap unionValue; ///< Values for struct types, indexed by name |
---|
| 48 | ValueMap enumValue; ///< Values for struct types, indexed by name |
---|
| 49 | |
---|
| 50 | /// Information needed to roll back one scope change |
---|
| 51 | struct Rollback { |
---|
| 52 | /// One scope of pointer rollbacks |
---|
| 53 | typedef std::vector< std::pair< Value **, Value* > > PointerScope; |
---|
| 54 | /// One scope of map rollbacks |
---|
| 55 | typedef std::vector< std::pair< ValueMapIterator, Value* > > MapScope; |
---|
[62e5546] | 56 | |
---|
[dc12481] | 57 | PointerScope pointers; ///< Value pointers to roll back to their previous state |
---|
| 58 | MapScope mapNodes; ///< Value map iterators to roll back to their previous state |
---|
| 59 | |
---|
| 60 | void addRollback( Value **loc, Value *old ) { |
---|
| 61 | pointers.push_back( std::make_pair( loc, old ) ); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | void addRollback( ValueMapIterator loc, Value *old ) { |
---|
| 65 | mapNodes.push_back( std::make_pair( loc, old ) ); |
---|
| 66 | } |
---|
| 67 | }; |
---|
| 68 | |
---|
| 69 | std::vector< Rollback > scopes; ///< Scope rollback information |
---|
[62e5546] | 70 | |
---|
[dc12481] | 71 | struct Lookup : public Visitor { |
---|
| 72 | Lookup( TypeMap<Value> &typeMap ) : typeMap( typeMap ), found( 0 ), toInsert( 0 ) {} |
---|
| 73 | |
---|
| 74 | /// Inserts a new value into the map; returns the old value (if set, NULL otherwise). |
---|
| 75 | /// key must be non-null. |
---|
| 76 | Value *insert( Type *key, Value *val ) { |
---|
| 77 | toInsert = val; |
---|
| 78 | key->accept( *this ); |
---|
| 79 | return found; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | /// Looks up a value in the map. |
---|
| 83 | /// key must be non-null. |
---|
| 84 | Value *find( Type *key ) { |
---|
| 85 | //toInsert = 0; |
---|
| 86 | key->accept( *this ); |
---|
| 87 | return found; |
---|
| 88 | } |
---|
[62e5546] | 89 | |
---|
[dc12481] | 90 | void findAndReplace( Value *&loc ) { |
---|
| 91 | found = loc; |
---|
| 92 | if ( toInsert ) { |
---|
| 93 | typeMap.scopes.back().addRollback( &loc, found ); |
---|
| 94 | loc = toInsert; |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | void findAndReplace( ValueMap &map, const std::string &name ) { |
---|
| 99 | ValueMapIterator loc = map.find( name ); |
---|
| 100 | if ( loc != map.end() ) { |
---|
| 101 | found = loc->second; |
---|
| 102 | if ( toInsert ) { |
---|
| 103 | typeMap.scopes.back().addRollback( loc, found ); |
---|
| 104 | loc->second = toInsert; |
---|
| 105 | } |
---|
| 106 | } else if ( toInsert ) { |
---|
| 107 | loc = map.insert( loc, std::make_pair( name, toInsert ) ); |
---|
| 108 | typeMap.scopes.back().addRollback( loc, found ); |
---|
| 109 | } |
---|
| 110 | } |
---|
[62e5546] | 111 | |
---|
[dc12481] | 112 | virtual void visit( VoidType *voidType ) { |
---|
| 113 | findAndReplace( typeMap.voidValue ); |
---|
| 114 | } |
---|
[62e5546] | 115 | |
---|
[dc12481] | 116 | virtual void visit( BasicType *basicType ) { |
---|
| 117 | findAndReplace( typeMap.basicValue[basicType->get_kind()] ); |
---|
| 118 | } |
---|
[62e5546] | 119 | |
---|
[dc12481] | 120 | virtual void visit( PointerType *pointerType ) { |
---|
| 121 | // NOTE This is one of the places where the apporoximation of the resolver is (deliberately) poor; |
---|
| 122 | // A better version would likely not equate all pointer types to a match. |
---|
| 123 | if ( dynamic_cast< FunctionType* >( pointerType->get_base() ) ) { |
---|
| 124 | findAndReplace( typeMap.functionPointerValue ); |
---|
| 125 | } else if ( dynamic_cast< VoidType* >( pointerType->get_base() ) ) { |
---|
| 126 | findAndReplace( typeMap.voidPointerValue ); |
---|
| 127 | } else { |
---|
| 128 | findAndReplace( typeMap.pointerValue ); |
---|
| 129 | } |
---|
| 130 | } |
---|
[62e5546] | 131 | |
---|
[dc12481] | 132 | virtual void visit( ArrayType *arrayType ) { |
---|
| 133 | if ( dynamic_cast< FunctionType* >( arrayType->get_base() ) ) { |
---|
| 134 | findAndReplace( typeMap.functionPointerValue ); |
---|
| 135 | } else { |
---|
| 136 | findAndReplace( typeMap.pointerValue ); |
---|
| 137 | } |
---|
| 138 | } |
---|
[62e5546] | 139 | |
---|
[dc12481] | 140 | virtual void visit( FunctionType *functionType ) { |
---|
| 141 | findAndReplace( typeMap.functionPointerValue ); |
---|
| 142 | } |
---|
[62e5546] | 143 | |
---|
[dc12481] | 144 | virtual void visit( StructInstType *structType ) { |
---|
| 145 | findAndReplace( typeMap.structValue, structType->get_name() ); |
---|
| 146 | } |
---|
[62e5546] | 147 | |
---|
[dc12481] | 148 | virtual void visit( UnionInstType *unionType ) { |
---|
| 149 | findAndReplace( typeMap.unionValue, unionType->get_name() ); |
---|
| 150 | } |
---|
[62e5546] | 151 | |
---|
[dc12481] | 152 | virtual void visit( EnumInstType *enumType ) { |
---|
| 153 | findAndReplace( typeMap.enumValue, enumType->get_name() ); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | TypeMap<Value> &typeMap; ///< map storage |
---|
| 157 | Value *found; ///< Value found (NULL if none yet) |
---|
| 158 | Value *toInsert; ///< Value to insert (NULL if a lookup) |
---|
[62e5546] | 159 | }; // struct Lookup |
---|
| 160 | friend struct Lookup; |
---|
| 161 | |
---|
[dc12481] | 162 | public: |
---|
| 163 | /// Starts a new scope |
---|
| 164 | void beginScope() { |
---|
| 165 | Rollback scope; |
---|
| 166 | scopes.push_back(scope); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | /// Ends a scope; rolls back any changes made during that scope |
---|
| 170 | void endScope() { |
---|
| 171 | Rollback &scope = scopes.back(); |
---|
| 172 | /// Roll back pointer changes |
---|
| 173 | for (unsigned i = 0; i < scope.pointers.size(); ++i) { |
---|
| 174 | *scope.pointers[i].first = scope.pointers[i].second; |
---|
| 175 | } |
---|
| 176 | /// Roll back map changes |
---|
| 177 | for (unsigned i = 0; i < scope.mapNodes.size(); ++i) { |
---|
| 178 | scope.mapNodes[i].first->second = scope.mapNodes[i].second; |
---|
| 179 | } |
---|
| 180 | scopes.pop_back(); |
---|
| 181 | } |
---|
[62e5546] | 182 | |
---|
[dc12481] | 183 | TypeMap() : voidValue( 0 ), pointerValue( 0 ), voidPointerValue( 0 ), functionPointerValue( 0 ), structValue(), unionValue(), enumValue(), scopes() { |
---|
| 184 | beginScope(); |
---|
| 185 | for (int i = 0; i < BasicType::NUMBER_OF_BASIC_TYPES; ++i) { basicValue[i] = 0; } |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | /// Inserts a new value into the map; returns the old value (if set, NULL otherwise). |
---|
| 189 | /// key, val must be non-null. |
---|
| 190 | Value *insert( Type *key, Value *val ) { |
---|
| 191 | Lookup searcher( *this ); |
---|
| 192 | return searcher.insert( key, val ); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | /// Looks up a value in the map. |
---|
| 196 | /// key must be non-null |
---|
| 197 | Value *find( Type *key ) { |
---|
| 198 | Lookup searcher( *this ); |
---|
| 199 | return searcher.find( key ); |
---|
| 200 | } |
---|
[62e5546] | 201 | |
---|
[dc12481] | 202 | }; // class TypeMap |
---|
| 203 | |
---|
| 204 | } // namespace ResolvExpr |
---|
| 205 | |
---|
| 206 | #endif // _TYPEMAP_H |
---|
| 207 | |
---|
| 208 | // Local Variables: // |
---|
| 209 | // tab-width: 4 // |
---|
| 210 | // mode: c++ // |
---|
| 211 | // compile-command: "make install" // |
---|
[62e5546] | 212 | // End: // |
---|