[0dd3a2f] | 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 | // |
---|
[9cb8e88d] | 7 | // Validate.cc -- |
---|
[0dd3a2f] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 21:50:04 2015 |
---|
[4e06c1e] | 11 | // Last Modified By : Peter A. Buhr |
---|
[a7c90d4] | 12 | // Last Modified On : Tue Mar 7 07:51:36 2017 |
---|
| 13 | // Update Count : 349 |
---|
[0dd3a2f] | 14 | // |
---|
| 15 | |
---|
| 16 | // The "validate" phase of translation is used to take a syntax tree and convert it into a standard form that aims to be |
---|
| 17 | // as regular in structure as possible. Some assumptions can be made regarding the state of the tree after this pass is |
---|
| 18 | // complete, including: |
---|
| 19 | // |
---|
| 20 | // - No nested structure or union definitions; any in the input are "hoisted" to the level of the containing struct or |
---|
| 21 | // union. |
---|
| 22 | // |
---|
| 23 | // - All enumeration constants have type EnumInstType. |
---|
| 24 | // |
---|
[3c13c03] | 25 | // - The type "void" never occurs in lists of function parameter or return types. A function |
---|
| 26 | // taking no arguments has no argument types. |
---|
[0dd3a2f] | 27 | // |
---|
| 28 | // - No context instances exist; they are all replaced by the set of declarations signified by the context, instantiated |
---|
| 29 | // by the particular set of type arguments. |
---|
| 30 | // |
---|
| 31 | // - Every declaration is assigned a unique id. |
---|
| 32 | // |
---|
| 33 | // - No typedef declarations or instances exist; the actual type is substituted for each instance. |
---|
| 34 | // |
---|
| 35 | // - Each type, struct, and union definition is followed by an appropriate assignment operator. |
---|
| 36 | // |
---|
| 37 | // - Each use of a struct or union is connected to a complete definition of that struct or union, even if that |
---|
| 38 | // definition occurs later in the input. |
---|
[51b7345] | 39 | |
---|
| 40 | #include <list> |
---|
| 41 | #include <iterator> |
---|
[e491159] | 42 | #include "Common/ScopedMap.h" |
---|
[630a82a] | 43 | #include "Common/utility.h" |
---|
| 44 | #include "Common/UniqueName.h" |
---|
[51b7345] | 45 | #include "Validate.h" |
---|
| 46 | #include "SynTree/Visitor.h" |
---|
| 47 | #include "SynTree/Mutator.h" |
---|
| 48 | #include "SynTree/Type.h" |
---|
[630a82a] | 49 | #include "SynTree/Expression.h" |
---|
[51b7345] | 50 | #include "SynTree/Statement.h" |
---|
| 51 | #include "SynTree/TypeSubstitution.h" |
---|
[68cd1ce] | 52 | #include "Indexer.h" |
---|
[51b7345] | 53 | #include "FixFunction.h" |
---|
[cc79d97] | 54 | // #include "ImplementationType.h" |
---|
[630a82a] | 55 | #include "GenPoly/DeclMutator.h" |
---|
[51b7345] | 56 | #include "AddVisit.h" |
---|
[f6d7e0f] | 57 | #include "MakeLibCfa.h" |
---|
[cc79d97] | 58 | #include "TypeEquality.h" |
---|
[620cb95] | 59 | #include "Autogen.h" |
---|
[1cbca6e] | 60 | #include "ResolvExpr/typeops.h" |
---|
[79970ed] | 61 | #include <algorithm> |
---|
[d1969a6] | 62 | #include "InitTweak/InitTweak.h" |
---|
[9facf3b] | 63 | #include "CodeGen/CodeGenerator.h" |
---|
[51b7345] | 64 | |
---|
[c8ffe20b] | 65 | #define debugPrint( x ) if ( doDebug ) { std::cout << x; } |
---|
[51b7345] | 66 | |
---|
| 67 | namespace SymTab { |
---|
[9facf3b] | 68 | class HoistStruct final : public Visitor { |
---|
[c0aa336] | 69 | template< typename Visitor > |
---|
| 70 | friend void acceptAndAdd( std::list< Declaration * > &translationUnit, Visitor &visitor ); |
---|
| 71 | template< typename Visitor > |
---|
| 72 | friend void addVisitStatementList( std::list< Statement* > &stmts, Visitor &visitor ); |
---|
[a08ba92] | 73 | public: |
---|
[82dd287] | 74 | /// Flattens nested struct types |
---|
[0dd3a2f] | 75 | static void hoistStruct( std::list< Declaration * > &translationUnit ); |
---|
[9cb8e88d] | 76 | |
---|
[0dd3a2f] | 77 | std::list< Declaration * > &get_declsToAdd() { return declsToAdd; } |
---|
[9cb8e88d] | 78 | |
---|
[c0aa336] | 79 | virtual void visit( EnumInstType *enumInstType ); |
---|
| 80 | virtual void visit( StructInstType *structInstType ); |
---|
| 81 | virtual void visit( UnionInstType *unionInstType ); |
---|
[0dd3a2f] | 82 | virtual void visit( StructDecl *aggregateDecl ); |
---|
| 83 | virtual void visit( UnionDecl *aggregateDecl ); |
---|
[c8ffe20b] | 84 | |
---|
[0dd3a2f] | 85 | virtual void visit( CompoundStmt *compoundStmt ); |
---|
| 86 | virtual void visit( SwitchStmt *switchStmt ); |
---|
[a08ba92] | 87 | private: |
---|
[0dd3a2f] | 88 | HoistStruct(); |
---|
[c8ffe20b] | 89 | |
---|
[0dd3a2f] | 90 | template< typename AggDecl > void handleAggregate( AggDecl *aggregateDecl ); |
---|
[c8ffe20b] | 91 | |
---|
[c0aa336] | 92 | std::list< Declaration * > declsToAdd, declsToAddAfter; |
---|
[0dd3a2f] | 93 | bool inStruct; |
---|
[a08ba92] | 94 | }; |
---|
[c8ffe20b] | 95 | |
---|
[cce9429] | 96 | /// Fix return types so that every function returns exactly one value |
---|
[9facf3b] | 97 | class ReturnTypeFixer final : public Visitor { |
---|
[cce9429] | 98 | public: |
---|
[9facf3b] | 99 | typedef Visitor Parent; |
---|
| 100 | using Parent::visit; |
---|
| 101 | |
---|
[cce9429] | 102 | static void fix( std::list< Declaration * > &translationUnit ); |
---|
| 103 | |
---|
[9facf3b] | 104 | virtual void visit( FunctionDecl * functionDecl ); |
---|
[cce9429] | 105 | virtual void visit( FunctionType * ftype ); |
---|
| 106 | }; |
---|
| 107 | |
---|
[de91427b] | 108 | /// Replaces enum types by int, and function or array types in function parameter and return lists by appropriate pointers. |
---|
[9facf3b] | 109 | class EnumAndPointerDecayPass final : public Visitor { |
---|
[0dd3a2f] | 110 | typedef Visitor Parent; |
---|
| 111 | virtual void visit( EnumDecl *aggregateDecl ); |
---|
| 112 | virtual void visit( FunctionType *func ); |
---|
[a08ba92] | 113 | }; |
---|
[82dd287] | 114 | |
---|
| 115 | /// Associates forward declarations of aggregates with their definitions |
---|
[cce9429] | 116 | class LinkReferenceToTypes final : public Indexer { |
---|
[0dd3a2f] | 117 | typedef Indexer Parent; |
---|
[a08ba92] | 118 | public: |
---|
[cce9429] | 119 | LinkReferenceToTypes( bool doDebug, const Indexer *indexer ); |
---|
[a08ba92] | 120 | private: |
---|
[4a9ccc3] | 121 | using Parent::visit; |
---|
[c0aa336] | 122 | void visit( EnumInstType *enumInst ) final; |
---|
[62e5546] | 123 | void visit( StructInstType *structInst ) final; |
---|
| 124 | void visit( UnionInstType *unionInst ) final; |
---|
| 125 | void visit( TraitInstType *contextInst ) final; |
---|
[c0aa336] | 126 | void visit( EnumDecl *enumDecl ) final; |
---|
[62e5546] | 127 | void visit( StructDecl *structDecl ) final; |
---|
| 128 | void visit( UnionDecl *unionDecl ) final; |
---|
| 129 | void visit( TypeInstType *typeInst ) final; |
---|
[0dd3a2f] | 130 | |
---|
| 131 | const Indexer *indexer; |
---|
[9cb8e88d] | 132 | |
---|
[c0aa336] | 133 | typedef std::map< std::string, std::list< EnumInstType * > > ForwardEnumsType; |
---|
[0dd3a2f] | 134 | typedef std::map< std::string, std::list< StructInstType * > > ForwardStructsType; |
---|
| 135 | typedef std::map< std::string, std::list< UnionInstType * > > ForwardUnionsType; |
---|
[c0aa336] | 136 | ForwardEnumsType forwardEnums; |
---|
[0dd3a2f] | 137 | ForwardStructsType forwardStructs; |
---|
| 138 | ForwardUnionsType forwardUnions; |
---|
[a08ba92] | 139 | }; |
---|
[c8ffe20b] | 140 | |
---|
[82dd287] | 141 | /// Replaces array and function types in forall lists by appropriate pointer type |
---|
[4a9ccc3] | 142 | class Pass3 final : public Indexer { |
---|
[0dd3a2f] | 143 | typedef Indexer Parent; |
---|
[a08ba92] | 144 | public: |
---|
[4a9ccc3] | 145 | using Parent::visit; |
---|
[0dd3a2f] | 146 | Pass3( const Indexer *indexer ); |
---|
[a08ba92] | 147 | private: |
---|
[4a9ccc3] | 148 | virtual void visit( ObjectDecl *object ) override; |
---|
| 149 | virtual void visit( FunctionDecl *func ) override; |
---|
[c8ffe20b] | 150 | |
---|
[0dd3a2f] | 151 | const Indexer *indexer; |
---|
[a08ba92] | 152 | }; |
---|
[c8ffe20b] | 153 | |
---|
[de91427b] | 154 | class ReturnChecker : public Visitor { |
---|
| 155 | public: |
---|
| 156 | /// Checks that return statements return nothing if their return type is void |
---|
| 157 | /// and return something if the return type is non-void. |
---|
| 158 | static void checkFunctionReturns( std::list< Declaration * > & translationUnit ); |
---|
| 159 | private: |
---|
| 160 | virtual void visit( FunctionDecl * functionDecl ); |
---|
| 161 | virtual void visit( ReturnStmt * returnStmt ); |
---|
| 162 | |
---|
| 163 | std::list< DeclarationWithType * > returnVals; |
---|
| 164 | }; |
---|
| 165 | |
---|
[a08ba92] | 166 | class EliminateTypedef : public Mutator { |
---|
| 167 | public: |
---|
[de91427b] | 168 | EliminateTypedef() : scopeLevel( 0 ) {} |
---|
| 169 | /// Replaces typedefs by forward declarations |
---|
[0dd3a2f] | 170 | static void eliminateTypedef( std::list< Declaration * > &translationUnit ); |
---|
[a08ba92] | 171 | private: |
---|
[0dd3a2f] | 172 | virtual Declaration *mutate( TypedefDecl *typeDecl ); |
---|
| 173 | virtual TypeDecl *mutate( TypeDecl *typeDecl ); |
---|
| 174 | virtual DeclarationWithType *mutate( FunctionDecl *funcDecl ); |
---|
[1db21619] | 175 | virtual DeclarationWithType *mutate( ObjectDecl *objDecl ); |
---|
[0dd3a2f] | 176 | virtual CompoundStmt *mutate( CompoundStmt *compoundStmt ); |
---|
| 177 | virtual Type *mutate( TypeInstType *aggregateUseType ); |
---|
| 178 | virtual Expression *mutate( CastExpr *castExpr ); |
---|
[cc79d97] | 179 | |
---|
[85c4ef0] | 180 | virtual Declaration *mutate( StructDecl * structDecl ); |
---|
| 181 | virtual Declaration *mutate( UnionDecl * unionDecl ); |
---|
| 182 | virtual Declaration *mutate( EnumDecl * enumDecl ); |
---|
[4040425] | 183 | virtual Declaration *mutate( TraitDecl * contextDecl ); |
---|
[85c4ef0] | 184 | |
---|
| 185 | template<typename AggDecl> |
---|
| 186 | AggDecl *handleAggregate( AggDecl * aggDecl ); |
---|
| 187 | |
---|
[45161b4d] | 188 | template<typename AggDecl> |
---|
| 189 | void addImplicitTypedef( AggDecl * aggDecl ); |
---|
[70a06f6] | 190 | |
---|
[46f6134] | 191 | typedef std::unique_ptr<TypedefDecl> TypedefDeclPtr; |
---|
[e491159] | 192 | typedef ScopedMap< std::string, std::pair< TypedefDeclPtr, int > > TypedefMap; |
---|
[679864e1] | 193 | typedef std::map< std::string, TypeDecl * > TypeDeclMap; |
---|
[cc79d97] | 194 | TypedefMap typedefNames; |
---|
[679864e1] | 195 | TypeDeclMap typedeclNames; |
---|
[cc79d97] | 196 | int scopeLevel; |
---|
[a08ba92] | 197 | }; |
---|
[c8ffe20b] | 198 | |
---|
[d1969a6] | 199 | class VerifyCtorDtorAssign : public Visitor { |
---|
[9cb8e88d] | 200 | public: |
---|
[d1969a6] | 201 | /// ensure that constructors, destructors, and assignment have at least one |
---|
| 202 | /// parameter, the first of which must be a pointer, and that ctor/dtors have no |
---|
[9cb8e88d] | 203 | /// return values. |
---|
| 204 | static void verify( std::list< Declaration * > &translationUnit ); |
---|
| 205 | |
---|
| 206 | virtual void visit( FunctionDecl *funcDecl ); |
---|
[5f98ce5] | 207 | }; |
---|
[70a06f6] | 208 | |
---|
[62e5546] | 209 | class CompoundLiteral final : public GenPoly::DeclMutator { |
---|
[a7c90d4] | 210 | DeclarationNode::StorageClasses storageClasses; |
---|
[630a82a] | 211 | |
---|
[62e5546] | 212 | using GenPoly::DeclMutator::mutate; |
---|
| 213 | DeclarationWithType * mutate( ObjectDecl *objectDecl ) final; |
---|
| 214 | Expression *mutate( CompoundLiteralExpr *compLitExpr ) final; |
---|
[9cb8e88d] | 215 | }; |
---|
| 216 | |
---|
[a08ba92] | 217 | void validate( std::list< Declaration * > &translationUnit, bool doDebug ) { |
---|
[bda58ad] | 218 | EnumAndPointerDecayPass epc; |
---|
[cce9429] | 219 | LinkReferenceToTypes lrt( doDebug, 0 ); |
---|
[0dd3a2f] | 220 | Pass3 pass3( 0 ); |
---|
[630a82a] | 221 | CompoundLiteral compoundliteral; |
---|
| 222 | |
---|
[0dd3a2f] | 223 | EliminateTypedef::eliminateTypedef( translationUnit ); |
---|
| 224 | HoistStruct::hoistStruct( translationUnit ); |
---|
[cce9429] | 225 | ReturnTypeFixer::fix( translationUnit ); // must happen before autogen |
---|
[bda58ad] | 226 | autogenerateRoutines( translationUnit ); // moved up, used to be below compoundLiteral - currently needs EnumAndPointerDecayPass |
---|
| 227 | acceptAll( translationUnit, epc ); |
---|
[cce9429] | 228 | acceptAll( translationUnit, lrt ); |
---|
[de91427b] | 229 | ReturnChecker::checkFunctionReturns( translationUnit ); |
---|
[40e636a] | 230 | compoundliteral.mutateDeclarationList( translationUnit ); |
---|
[0dd3a2f] | 231 | acceptAll( translationUnit, pass3 ); |
---|
[d1969a6] | 232 | VerifyCtorDtorAssign::verify( translationUnit ); |
---|
[a08ba92] | 233 | } |
---|
[9cb8e88d] | 234 | |
---|
[a08ba92] | 235 | void validateType( Type *type, const Indexer *indexer ) { |
---|
[bda58ad] | 236 | EnumAndPointerDecayPass epc; |
---|
[cce9429] | 237 | LinkReferenceToTypes lrt( false, indexer ); |
---|
[0dd3a2f] | 238 | Pass3 pass3( indexer ); |
---|
[bda58ad] | 239 | type->accept( epc ); |
---|
[cce9429] | 240 | type->accept( lrt ); |
---|
[0dd3a2f] | 241 | type->accept( pass3 ); |
---|
[a08ba92] | 242 | } |
---|
[c8ffe20b] | 243 | |
---|
[a08ba92] | 244 | void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) { |
---|
[0dd3a2f] | 245 | HoistStruct hoister; |
---|
[c0aa336] | 246 | acceptAndAdd( translationUnit, hoister ); |
---|
[a08ba92] | 247 | } |
---|
[c8ffe20b] | 248 | |
---|
[a08ba92] | 249 | HoistStruct::HoistStruct() : inStruct( false ) { |
---|
| 250 | } |
---|
[c8ffe20b] | 251 | |
---|
[a08ba92] | 252 | void filter( std::list< Declaration * > &declList, bool (*pred)( Declaration * ), bool doDelete ) { |
---|
[0dd3a2f] | 253 | std::list< Declaration * >::iterator i = declList.begin(); |
---|
| 254 | while ( i != declList.end() ) { |
---|
| 255 | std::list< Declaration * >::iterator next = i; |
---|
| 256 | ++next; |
---|
| 257 | if ( pred( *i ) ) { |
---|
| 258 | if ( doDelete ) { |
---|
| 259 | delete *i; |
---|
| 260 | } // if |
---|
| 261 | declList.erase( i ); |
---|
| 262 | } // if |
---|
| 263 | i = next; |
---|
| 264 | } // while |
---|
[a08ba92] | 265 | } |
---|
[c8ffe20b] | 266 | |
---|
[a08ba92] | 267 | bool isStructOrUnion( Declaration *decl ) { |
---|
[0dd3a2f] | 268 | return dynamic_cast< StructDecl * >( decl ) || dynamic_cast< UnionDecl * >( decl ); |
---|
[a08ba92] | 269 | } |
---|
[c0aa336] | 270 | |
---|
[a08ba92] | 271 | template< typename AggDecl > |
---|
| 272 | void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) { |
---|
[0dd3a2f] | 273 | if ( inStruct ) { |
---|
| 274 | // Add elements in stack order corresponding to nesting structure. |
---|
| 275 | declsToAdd.push_front( aggregateDecl ); |
---|
| 276 | Visitor::visit( aggregateDecl ); |
---|
| 277 | } else { |
---|
| 278 | inStruct = true; |
---|
| 279 | Visitor::visit( aggregateDecl ); |
---|
| 280 | inStruct = false; |
---|
| 281 | } // if |
---|
| 282 | // Always remove the hoisted aggregate from the inner structure. |
---|
| 283 | filter( aggregateDecl->get_members(), isStructOrUnion, false ); |
---|
[a08ba92] | 284 | } |
---|
[c8ffe20b] | 285 | |
---|
[c0aa336] | 286 | void HoistStruct::visit( EnumInstType *structInstType ) { |
---|
| 287 | if ( structInstType->get_baseEnum() ) { |
---|
| 288 | declsToAdd.push_front( structInstType->get_baseEnum() ); |
---|
| 289 | } |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | void HoistStruct::visit( StructInstType *structInstType ) { |
---|
| 293 | if ( structInstType->get_baseStruct() ) { |
---|
| 294 | declsToAdd.push_front( structInstType->get_baseStruct() ); |
---|
| 295 | } |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | void HoistStruct::visit( UnionInstType *structInstType ) { |
---|
| 299 | if ( structInstType->get_baseUnion() ) { |
---|
| 300 | declsToAdd.push_front( structInstType->get_baseUnion() ); |
---|
| 301 | } |
---|
| 302 | } |
---|
| 303 | |
---|
[a08ba92] | 304 | void HoistStruct::visit( StructDecl *aggregateDecl ) { |
---|
[0dd3a2f] | 305 | handleAggregate( aggregateDecl ); |
---|
[a08ba92] | 306 | } |
---|
[c8ffe20b] | 307 | |
---|
[a08ba92] | 308 | void HoistStruct::visit( UnionDecl *aggregateDecl ) { |
---|
[0dd3a2f] | 309 | handleAggregate( aggregateDecl ); |
---|
[a08ba92] | 310 | } |
---|
[c8ffe20b] | 311 | |
---|
[a08ba92] | 312 | void HoistStruct::visit( CompoundStmt *compoundStmt ) { |
---|
[0dd3a2f] | 313 | addVisit( compoundStmt, *this ); |
---|
[a08ba92] | 314 | } |
---|
[c8ffe20b] | 315 | |
---|
[a08ba92] | 316 | void HoistStruct::visit( SwitchStmt *switchStmt ) { |
---|
[0dd3a2f] | 317 | addVisit( switchStmt, *this ); |
---|
[a08ba92] | 318 | } |
---|
[c8ffe20b] | 319 | |
---|
[bda58ad] | 320 | void EnumAndPointerDecayPass::visit( EnumDecl *enumDecl ) { |
---|
[0dd3a2f] | 321 | // Set the type of each member of the enumeration to be EnumConstant |
---|
| 322 | for ( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) { |
---|
[f6d7e0f] | 323 | ObjectDecl * obj = dynamic_cast< ObjectDecl * >( *i ); |
---|
[0dd3a2f] | 324 | assert( obj ); |
---|
[c0aa336] | 325 | obj->set_type( new EnumInstType( Type::Qualifiers( true, false, false, false, false ), enumDecl->get_name() ) ); |
---|
[0dd3a2f] | 326 | } // for |
---|
| 327 | Parent::visit( enumDecl ); |
---|
[a08ba92] | 328 | } |
---|
[51b7345] | 329 | |
---|
[a08ba92] | 330 | namespace { |
---|
[83de11e] | 331 | template< typename DWTList > |
---|
| 332 | void fixFunctionList( DWTList & dwts, FunctionType * func ) { |
---|
[0dd3a2f] | 333 | // the only case in which "void" is valid is where it is the only one in the list; then it should be removed |
---|
| 334 | // entirely other fix ups are handled by the FixFunction class |
---|
[83de11e] | 335 | typedef typename DWTList::iterator DWTIterator; |
---|
| 336 | DWTIterator begin( dwts.begin() ), end( dwts.end() ); |
---|
[0dd3a2f] | 337 | if ( begin == end ) return; |
---|
| 338 | FixFunction fixer; |
---|
| 339 | DWTIterator i = begin; |
---|
[83de11e] | 340 | *i = (*i)->acceptMutator( fixer ); |
---|
[0dd3a2f] | 341 | if ( fixer.get_isVoid() ) { |
---|
| 342 | DWTIterator j = i; |
---|
| 343 | ++i; |
---|
[bda58ad] | 344 | delete *j; |
---|
[83de11e] | 345 | dwts.erase( j ); |
---|
[9cb8e88d] | 346 | if ( i != end ) { |
---|
[0dd3a2f] | 347 | throw SemanticError( "invalid type void in function type ", func ); |
---|
| 348 | } // if |
---|
| 349 | } else { |
---|
| 350 | ++i; |
---|
| 351 | for ( ; i != end; ++i ) { |
---|
| 352 | FixFunction fixer; |
---|
| 353 | *i = (*i )->acceptMutator( fixer ); |
---|
| 354 | if ( fixer.get_isVoid() ) { |
---|
| 355 | throw SemanticError( "invalid type void in function type ", func ); |
---|
| 356 | } // if |
---|
| 357 | } // for |
---|
| 358 | } // if |
---|
| 359 | } |
---|
[a08ba92] | 360 | } |
---|
[c8ffe20b] | 361 | |
---|
[bda58ad] | 362 | void EnumAndPointerDecayPass::visit( FunctionType *func ) { |
---|
[0dd3a2f] | 363 | // Fix up parameters and return types |
---|
[83de11e] | 364 | fixFunctionList( func->get_parameters(), func ); |
---|
| 365 | fixFunctionList( func->get_returnVals(), func ); |
---|
[0dd3a2f] | 366 | Visitor::visit( func ); |
---|
[a08ba92] | 367 | } |
---|
[c8ffe20b] | 368 | |
---|
[cce9429] | 369 | LinkReferenceToTypes::LinkReferenceToTypes( bool doDebug, const Indexer *other_indexer ) : Indexer( doDebug ) { |
---|
[0dd3a2f] | 370 | if ( other_indexer ) { |
---|
| 371 | indexer = other_indexer; |
---|
| 372 | } else { |
---|
| 373 | indexer = this; |
---|
| 374 | } // if |
---|
[a08ba92] | 375 | } |
---|
[c8ffe20b] | 376 | |
---|
[c0aa336] | 377 | void LinkReferenceToTypes::visit( EnumInstType *enumInst ) { |
---|
| 378 | Parent::visit( enumInst ); |
---|
| 379 | EnumDecl *st = indexer->lookupEnum( enumInst->get_name() ); |
---|
| 380 | // it's not a semantic error if the enum is not found, just an implicit forward declaration |
---|
| 381 | if ( st ) { |
---|
| 382 | //assert( ! enumInst->get_baseEnum() || enumInst->get_baseEnum()->get_members().empty() || ! st->get_members().empty() ); |
---|
| 383 | enumInst->set_baseEnum( st ); |
---|
| 384 | } // if |
---|
| 385 | if ( ! st || st->get_members().empty() ) { |
---|
| 386 | // use of forward declaration |
---|
| 387 | forwardEnums[ enumInst->get_name() ].push_back( enumInst ); |
---|
| 388 | } // if |
---|
| 389 | } |
---|
| 390 | |
---|
[cce9429] | 391 | void LinkReferenceToTypes::visit( StructInstType *structInst ) { |
---|
[0dd3a2f] | 392 | Parent::visit( structInst ); |
---|
| 393 | StructDecl *st = indexer->lookupStruct( structInst->get_name() ); |
---|
| 394 | // it's not a semantic error if the struct is not found, just an implicit forward declaration |
---|
| 395 | if ( st ) { |
---|
[98735ef] | 396 | //assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() ); |
---|
[0dd3a2f] | 397 | structInst->set_baseStruct( st ); |
---|
| 398 | } // if |
---|
| 399 | if ( ! st || st->get_members().empty() ) { |
---|
| 400 | // use of forward declaration |
---|
| 401 | forwardStructs[ structInst->get_name() ].push_back( structInst ); |
---|
| 402 | } // if |
---|
[a08ba92] | 403 | } |
---|
[c8ffe20b] | 404 | |
---|
[cce9429] | 405 | void LinkReferenceToTypes::visit( UnionInstType *unionInst ) { |
---|
[0dd3a2f] | 406 | Parent::visit( unionInst ); |
---|
| 407 | UnionDecl *un = indexer->lookupUnion( unionInst->get_name() ); |
---|
| 408 | // it's not a semantic error if the union is not found, just an implicit forward declaration |
---|
| 409 | if ( un ) { |
---|
| 410 | unionInst->set_baseUnion( un ); |
---|
| 411 | } // if |
---|
| 412 | if ( ! un || un->get_members().empty() ) { |
---|
| 413 | // use of forward declaration |
---|
| 414 | forwardUnions[ unionInst->get_name() ].push_back( unionInst ); |
---|
| 415 | } // if |
---|
[a08ba92] | 416 | } |
---|
[c8ffe20b] | 417 | |
---|
[4a9ccc3] | 418 | void LinkReferenceToTypes::visit( TraitInstType *traitInst ) { |
---|
| 419 | Parent::visit( traitInst ); |
---|
| 420 | if ( traitInst->get_name() == "sized" ) { |
---|
[2c57025] | 421 | // "sized" is a special trait with no members - just flick the sized status on for the type variable |
---|
[4a9ccc3] | 422 | if ( traitInst->get_parameters().size() != 1 ) { |
---|
| 423 | throw SemanticError( "incorrect number of trait parameters: ", traitInst ); |
---|
[2c57025] | 424 | } |
---|
[4a9ccc3] | 425 | TypeExpr * param = safe_dynamic_cast< TypeExpr * > ( traitInst->get_parameters().front() ); |
---|
[2c57025] | 426 | TypeInstType * inst = safe_dynamic_cast< TypeInstType * > ( param->get_type() ); |
---|
| 427 | TypeDecl * decl = inst->get_baseType(); |
---|
| 428 | decl->set_sized( true ); |
---|
| 429 | // since "sized" is special, the next few steps don't apply |
---|
| 430 | return; |
---|
| 431 | } |
---|
[4a9ccc3] | 432 | TraitDecl *traitDecl = indexer->lookupTrait( traitInst->get_name() ); |
---|
| 433 | if ( ! traitDecl ) { |
---|
| 434 | throw SemanticError( "use of undeclared trait " + traitInst->get_name() ); |
---|
[17cd4eb] | 435 | } // if |
---|
[4a9ccc3] | 436 | if ( traitDecl->get_parameters().size() != traitInst->get_parameters().size() ) { |
---|
| 437 | throw SemanticError( "incorrect number of trait parameters: ", traitInst ); |
---|
| 438 | } // if |
---|
| 439 | |
---|
| 440 | for ( TypeDecl * td : traitDecl->get_parameters() ) { |
---|
| 441 | for ( DeclarationWithType * assert : td->get_assertions() ) { |
---|
| 442 | traitInst->get_members().push_back( assert->clone() ); |
---|
[0dd3a2f] | 443 | } // for |
---|
| 444 | } // for |
---|
[51b986f] | 445 | |
---|
[4a9ccc3] | 446 | // need to clone members of the trait for ownership purposes |
---|
[79970ed] | 447 | std::list< Declaration * > members; |
---|
[4a9ccc3] | 448 | std::transform( traitDecl->get_members().begin(), traitDecl->get_members().end(), back_inserter( members ), [](Declaration * dwt) { return dwt->clone(); } ); |
---|
| 449 | |
---|
| 450 | applySubstitution( traitDecl->get_parameters().begin(), traitDecl->get_parameters().end(), traitInst->get_parameters().begin(), members.begin(), members.end(), back_inserter( traitInst->get_members() ) ); |
---|
[79970ed] | 451 | |
---|
[4a9ccc3] | 452 | // need to carry over the 'sized' status of each decl in the instance |
---|
| 453 | for ( auto p : group_iterate( traitDecl->get_parameters(), traitInst->get_parameters() ) ) { |
---|
| 454 | TypeExpr * expr = safe_dynamic_cast< TypeExpr * >( std::get<1>(p) ); |
---|
| 455 | if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( expr->get_type() ) ) { |
---|
| 456 | TypeDecl * formalDecl = std::get<0>(p); |
---|
| 457 | TypeDecl * instDecl = inst->get_baseType(); |
---|
| 458 | if ( formalDecl->get_sized() ) instDecl->set_sized( true ); |
---|
| 459 | } |
---|
| 460 | } |
---|
[a08ba92] | 461 | } |
---|
[c8ffe20b] | 462 | |
---|
[c0aa336] | 463 | void LinkReferenceToTypes::visit( EnumDecl *enumDecl ) { |
---|
| 464 | // visit enum members first so that the types of self-referencing members are updated properly |
---|
| 465 | Parent::visit( enumDecl ); |
---|
| 466 | if ( ! enumDecl->get_members().empty() ) { |
---|
| 467 | ForwardEnumsType::iterator fwds = forwardEnums.find( enumDecl->get_name() ); |
---|
| 468 | if ( fwds != forwardEnums.end() ) { |
---|
| 469 | for ( std::list< EnumInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { |
---|
| 470 | (*inst )->set_baseEnum( enumDecl ); |
---|
| 471 | } // for |
---|
| 472 | forwardEnums.erase( fwds ); |
---|
| 473 | } // if |
---|
| 474 | } // if |
---|
| 475 | } |
---|
| 476 | |
---|
[cce9429] | 477 | void LinkReferenceToTypes::visit( StructDecl *structDecl ) { |
---|
[677c1be] | 478 | // visit struct members first so that the types of self-referencing members are updated properly |
---|
| 479 | Parent::visit( structDecl ); |
---|
[0dd3a2f] | 480 | if ( ! structDecl->get_members().empty() ) { |
---|
| 481 | ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() ); |
---|
| 482 | if ( fwds != forwardStructs.end() ) { |
---|
| 483 | for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { |
---|
| 484 | (*inst )->set_baseStruct( structDecl ); |
---|
| 485 | } // for |
---|
| 486 | forwardStructs.erase( fwds ); |
---|
| 487 | } // if |
---|
| 488 | } // if |
---|
[a08ba92] | 489 | } |
---|
[c8ffe20b] | 490 | |
---|
[cce9429] | 491 | void LinkReferenceToTypes::visit( UnionDecl *unionDecl ) { |
---|
[677c1be] | 492 | Parent::visit( unionDecl ); |
---|
[0dd3a2f] | 493 | if ( ! unionDecl->get_members().empty() ) { |
---|
| 494 | ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() ); |
---|
| 495 | if ( fwds != forwardUnions.end() ) { |
---|
| 496 | for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { |
---|
| 497 | (*inst )->set_baseUnion( unionDecl ); |
---|
| 498 | } // for |
---|
| 499 | forwardUnions.erase( fwds ); |
---|
| 500 | } // if |
---|
| 501 | } // if |
---|
[a08ba92] | 502 | } |
---|
[c8ffe20b] | 503 | |
---|
[cce9429] | 504 | void LinkReferenceToTypes::visit( TypeInstType *typeInst ) { |
---|
[0dd3a2f] | 505 | if ( NamedTypeDecl *namedTypeDecl = lookupType( typeInst->get_name() ) ) { |
---|
| 506 | if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) { |
---|
| 507 | typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype ); |
---|
| 508 | } // if |
---|
| 509 | } // if |
---|
[a08ba92] | 510 | } |
---|
[c8ffe20b] | 511 | |
---|
[a08ba92] | 512 | Pass3::Pass3( const Indexer *other_indexer ) : Indexer( false ) { |
---|
[0dd3a2f] | 513 | if ( other_indexer ) { |
---|
| 514 | indexer = other_indexer; |
---|
| 515 | } else { |
---|
| 516 | indexer = this; |
---|
| 517 | } // if |
---|
[a08ba92] | 518 | } |
---|
[c8ffe20b] | 519 | |
---|
[4a9ccc3] | 520 | /// Fix up assertions - flattens assertion lists, removing all trait instances |
---|
| 521 | void forallFixer( Type * func ) { |
---|
| 522 | for ( TypeDecl * type : func->get_forall() ) { |
---|
[0dd3a2f] | 523 | std::list< DeclarationWithType * > toBeDone, nextRound; |
---|
[4a9ccc3] | 524 | toBeDone.splice( toBeDone.end(), type->get_assertions() ); |
---|
[0dd3a2f] | 525 | while ( ! toBeDone.empty() ) { |
---|
[4a9ccc3] | 526 | for ( DeclarationWithType * assertion : toBeDone ) { |
---|
| 527 | if ( TraitInstType *traitInst = dynamic_cast< TraitInstType * >( assertion->get_type() ) ) { |
---|
| 528 | // expand trait instance into all of its members |
---|
| 529 | for ( Declaration * member : traitInst->get_members() ) { |
---|
| 530 | DeclarationWithType *dwt = safe_dynamic_cast< DeclarationWithType * >( member ); |
---|
[0dd3a2f] | 531 | nextRound.push_back( dwt->clone() ); |
---|
| 532 | } |
---|
[4a9ccc3] | 533 | delete traitInst; |
---|
[0dd3a2f] | 534 | } else { |
---|
[4a9ccc3] | 535 | // pass assertion through |
---|
[0dd3a2f] | 536 | FixFunction fixer; |
---|
[4a9ccc3] | 537 | assertion = assertion->acceptMutator( fixer ); |
---|
[0dd3a2f] | 538 | if ( fixer.get_isVoid() ) { |
---|
| 539 | throw SemanticError( "invalid type void in assertion of function ", func ); |
---|
| 540 | } |
---|
[4a9ccc3] | 541 | type->get_assertions().push_back( assertion ); |
---|
[0dd3a2f] | 542 | } // if |
---|
| 543 | } // for |
---|
| 544 | toBeDone.clear(); |
---|
| 545 | toBeDone.splice( toBeDone.end(), nextRound ); |
---|
| 546 | } // while |
---|
| 547 | } // for |
---|
[a08ba92] | 548 | } |
---|
[c8ffe20b] | 549 | |
---|
[a08ba92] | 550 | void Pass3::visit( ObjectDecl *object ) { |
---|
[0dd3a2f] | 551 | forallFixer( object->get_type() ); |
---|
| 552 | if ( PointerType *pointer = dynamic_cast< PointerType * >( object->get_type() ) ) { |
---|
| 553 | forallFixer( pointer->get_base() ); |
---|
| 554 | } // if |
---|
| 555 | Parent::visit( object ); |
---|
| 556 | object->fixUniqueId(); |
---|
[a08ba92] | 557 | } |
---|
[c8ffe20b] | 558 | |
---|
[a08ba92] | 559 | void Pass3::visit( FunctionDecl *func ) { |
---|
[0dd3a2f] | 560 | forallFixer( func->get_type() ); |
---|
| 561 | Parent::visit( func ); |
---|
| 562 | func->fixUniqueId(); |
---|
[a08ba92] | 563 | } |
---|
[c8ffe20b] | 564 | |
---|
[de91427b] | 565 | void ReturnChecker::checkFunctionReturns( std::list< Declaration * > & translationUnit ) { |
---|
| 566 | ReturnChecker checker; |
---|
| 567 | acceptAll( translationUnit, checker ); |
---|
| 568 | } |
---|
| 569 | |
---|
| 570 | void ReturnChecker::visit( FunctionDecl * functionDecl ) { |
---|
| 571 | std::list< DeclarationWithType * > oldReturnVals = returnVals; |
---|
| 572 | returnVals = functionDecl->get_functionType()->get_returnVals(); |
---|
| 573 | Visitor::visit( functionDecl ); |
---|
| 574 | returnVals = oldReturnVals; |
---|
| 575 | } |
---|
| 576 | |
---|
| 577 | void ReturnChecker::visit( ReturnStmt * returnStmt ) { |
---|
[74d1804] | 578 | // Previously this also checked for the existence of an expr paired with no return values on |
---|
| 579 | // the function return type. This is incorrect, since you can have an expression attached to |
---|
| 580 | // a return statement in a void-returning function in C. The expression is treated as if it |
---|
| 581 | // were cast to void. |
---|
[de91427b] | 582 | if ( returnStmt->get_expr() == NULL && returnVals.size() != 0 ) { |
---|
| 583 | throw SemanticError( "Non-void function returns no values: " , returnStmt ); |
---|
| 584 | } |
---|
| 585 | } |
---|
| 586 | |
---|
| 587 | |
---|
[a08ba92] | 588 | bool isTypedef( Declaration *decl ) { |
---|
[0dd3a2f] | 589 | return dynamic_cast< TypedefDecl * >( decl ); |
---|
[a08ba92] | 590 | } |
---|
[c8ffe20b] | 591 | |
---|
[a08ba92] | 592 | void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) { |
---|
[0dd3a2f] | 593 | EliminateTypedef eliminator; |
---|
| 594 | mutateAll( translationUnit, eliminator ); |
---|
[5f98ce5] | 595 | if ( eliminator.typedefNames.count( "size_t" ) ) { |
---|
| 596 | // grab and remember declaration of size_t |
---|
| 597 | SizeType = eliminator.typedefNames["size_t"].first->get_base()->clone(); |
---|
| 598 | } else { |
---|
[40e636a] | 599 | // xxx - missing global typedef for size_t - default to long unsigned int, even though that may be wrong |
---|
| 600 | // eventually should have a warning for this case. |
---|
| 601 | SizeType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); |
---|
[5f98ce5] | 602 | } |
---|
[0dd3a2f] | 603 | filter( translationUnit, isTypedef, true ); |
---|
[5f98ce5] | 604 | |
---|
[a08ba92] | 605 | } |
---|
[c8ffe20b] | 606 | |
---|
[85c4ef0] | 607 | Type *EliminateTypedef::mutate( TypeInstType * typeInst ) { |
---|
[9cb8e88d] | 608 | // instances of typedef types will come here. If it is an instance |
---|
[cc79d97] | 609 | // of a typdef type, link the instance to its actual type. |
---|
| 610 | TypedefMap::const_iterator def = typedefNames.find( typeInst->get_name() ); |
---|
[0dd3a2f] | 611 | if ( def != typedefNames.end() ) { |
---|
[cc79d97] | 612 | Type *ret = def->second.first->get_base()->clone(); |
---|
[0dd3a2f] | 613 | ret->get_qualifiers() += typeInst->get_qualifiers(); |
---|
[0215a76f] | 614 | // place instance parameters on the typedef'd type |
---|
| 615 | if ( ! typeInst->get_parameters().empty() ) { |
---|
| 616 | ReferenceToType *rtt = dynamic_cast<ReferenceToType*>(ret); |
---|
| 617 | if ( ! rtt ) { |
---|
| 618 | throw SemanticError("cannot apply type parameters to base type of " + typeInst->get_name()); |
---|
| 619 | } |
---|
| 620 | rtt->get_parameters().clear(); |
---|
[b644d6f] | 621 | cloneAll( typeInst->get_parameters(), rtt->get_parameters() ); |
---|
| 622 | mutateAll( rtt->get_parameters(), *this ); // recursively fix typedefs on parameters |
---|
[1db21619] | 623 | } // if |
---|
[0dd3a2f] | 624 | delete typeInst; |
---|
| 625 | return ret; |
---|
[679864e1] | 626 | } else { |
---|
| 627 | TypeDeclMap::const_iterator base = typedeclNames.find( typeInst->get_name() ); |
---|
[43c89a7] | 628 | assertf( base != typedeclNames.end(), "Can't find typedecl name %s", typeInst->get_name().c_str() ); |
---|
[1e8b02f5] | 629 | typeInst->set_baseType( base->second ); |
---|
[0dd3a2f] | 630 | } // if |
---|
| 631 | return typeInst; |
---|
[a08ba92] | 632 | } |
---|
[c8ffe20b] | 633 | |
---|
[85c4ef0] | 634 | Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) { |
---|
[0dd3a2f] | 635 | Declaration *ret = Mutator::mutate( tyDecl ); |
---|
[5f98ce5] | 636 | |
---|
[cc79d97] | 637 | if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) { |
---|
[9cb8e88d] | 638 | // typedef to the same name from the same scope |
---|
[cc79d97] | 639 | // must be from the same type |
---|
| 640 | |
---|
| 641 | Type * t1 = tyDecl->get_base(); |
---|
| 642 | Type * t2 = typedefNames[ tyDecl->get_name() ].first->get_base(); |
---|
[1cbca6e] | 643 | if ( ! ResolvExpr::typesCompatible( t1, t2, Indexer() ) ) { |
---|
[cc79d97] | 644 | throw SemanticError( "cannot redefine typedef: " + tyDecl->get_name() ); |
---|
[85c4ef0] | 645 | } |
---|
[cc79d97] | 646 | } else { |
---|
[46f6134] | 647 | typedefNames[ tyDecl->get_name() ] = std::make_pair( TypedefDeclPtr( tyDecl ), scopeLevel ); |
---|
[cc79d97] | 648 | } // if |
---|
| 649 | |
---|
[0dd3a2f] | 650 | // When a typedef is a forward declaration: |
---|
| 651 | // typedef struct screen SCREEN; |
---|
| 652 | // the declaration portion must be retained: |
---|
| 653 | // struct screen; |
---|
| 654 | // because the expansion of the typedef is: |
---|
| 655 | // void rtn( SCREEN *p ) => void rtn( struct screen *p ) |
---|
| 656 | // hence the type-name "screen" must be defined. |
---|
| 657 | // Note, qualifiers on the typedef are superfluous for the forward declaration. |
---|
| 658 | if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( tyDecl->get_base() ) ) { |
---|
[dd020c0] | 659 | return new StructDecl( aggDecl->get_name() ); |
---|
[0dd3a2f] | 660 | } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( tyDecl->get_base() ) ) { |
---|
[dd020c0] | 661 | return new UnionDecl( aggDecl->get_name() ); |
---|
[956a9c7] | 662 | } else if ( EnumInstType *enumDecl = dynamic_cast< EnumInstType * >( tyDecl->get_base() ) ) { |
---|
| 663 | return new EnumDecl( enumDecl->get_name() ); |
---|
[0dd3a2f] | 664 | } else { |
---|
[46f6134] | 665 | return ret->clone(); |
---|
[0dd3a2f] | 666 | } // if |
---|
[a08ba92] | 667 | } |
---|
[c8ffe20b] | 668 | |
---|
[85c4ef0] | 669 | TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) { |
---|
[cc79d97] | 670 | TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() ); |
---|
[0dd3a2f] | 671 | if ( i != typedefNames.end() ) { |
---|
| 672 | typedefNames.erase( i ) ; |
---|
| 673 | } // if |
---|
[679864e1] | 674 | |
---|
| 675 | typedeclNames[ typeDecl->get_name() ] = typeDecl; |
---|
[2c57025] | 676 | return Mutator::mutate( typeDecl ); |
---|
[a08ba92] | 677 | } |
---|
[c8ffe20b] | 678 | |
---|
[85c4ef0] | 679 | DeclarationWithType *EliminateTypedef::mutate( FunctionDecl * funcDecl ) { |
---|
[46f6134] | 680 | typedefNames.beginScope(); |
---|
[0dd3a2f] | 681 | DeclarationWithType *ret = Mutator::mutate( funcDecl ); |
---|
[46f6134] | 682 | typedefNames.endScope(); |
---|
[0dd3a2f] | 683 | return ret; |
---|
[a08ba92] | 684 | } |
---|
[c8ffe20b] | 685 | |
---|
[1db21619] | 686 | DeclarationWithType *EliminateTypedef::mutate( ObjectDecl * objDecl ) { |
---|
[46f6134] | 687 | typedefNames.beginScope(); |
---|
[1db21619] | 688 | DeclarationWithType *ret = Mutator::mutate( objDecl ); |
---|
[46f6134] | 689 | typedefNames.endScope(); |
---|
[dd020c0] | 690 | |
---|
| 691 | if ( FunctionType *funtype = dynamic_cast<FunctionType *>( ret->get_type() ) ) { // function type? |
---|
[02e5ab6] | 692 | // replace the current object declaration with a function declaration |
---|
[a7c90d4] | 693 | FunctionDecl * newDecl = new FunctionDecl( ret->get_name(), ret->get_storageClasses(), ret->get_linkage(), funtype, 0, objDecl->get_attributes(), ret->get_funcSpec() ); |
---|
[0a86a30] | 694 | objDecl->get_attributes().clear(); |
---|
[dbe8f244] | 695 | objDecl->set_type( nullptr ); |
---|
[0a86a30] | 696 | delete objDecl; |
---|
| 697 | return newDecl; |
---|
[1db21619] | 698 | } // if |
---|
[0dd3a2f] | 699 | return ret; |
---|
[a08ba92] | 700 | } |
---|
[c8ffe20b] | 701 | |
---|
[85c4ef0] | 702 | Expression *EliminateTypedef::mutate( CastExpr * castExpr ) { |
---|
[46f6134] | 703 | typedefNames.beginScope(); |
---|
[0dd3a2f] | 704 | Expression *ret = Mutator::mutate( castExpr ); |
---|
[46f6134] | 705 | typedefNames.endScope(); |
---|
[0dd3a2f] | 706 | return ret; |
---|
[a08ba92] | 707 | } |
---|
[c8ffe20b] | 708 | |
---|
[85c4ef0] | 709 | CompoundStmt *EliminateTypedef::mutate( CompoundStmt * compoundStmt ) { |
---|
[46f6134] | 710 | typedefNames.beginScope(); |
---|
[cc79d97] | 711 | scopeLevel += 1; |
---|
[0dd3a2f] | 712 | CompoundStmt *ret = Mutator::mutate( compoundStmt ); |
---|
[cc79d97] | 713 | scopeLevel -= 1; |
---|
[0dd3a2f] | 714 | std::list< Statement * >::iterator i = compoundStmt->get_kids().begin(); |
---|
| 715 | while ( i != compoundStmt->get_kids().end() ) { |
---|
[85c4ef0] | 716 | std::list< Statement * >::iterator next = i+1; |
---|
[0dd3a2f] | 717 | if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( *i ) ) { |
---|
| 718 | if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) { |
---|
| 719 | delete *i; |
---|
| 720 | compoundStmt->get_kids().erase( i ); |
---|
| 721 | } // if |
---|
| 722 | } // if |
---|
| 723 | i = next; |
---|
| 724 | } // while |
---|
[46f6134] | 725 | typedefNames.endScope(); |
---|
[0dd3a2f] | 726 | return ret; |
---|
[a08ba92] | 727 | } |
---|
[85c4ef0] | 728 | |
---|
[43c89a7] | 729 | // there may be typedefs nested within aggregates. in order for everything to work properly, these should be removed |
---|
[45161b4d] | 730 | // as well |
---|
[85c4ef0] | 731 | template<typename AggDecl> |
---|
| 732 | AggDecl *EliminateTypedef::handleAggregate( AggDecl * aggDecl ) { |
---|
| 733 | std::list<Declaration *>::iterator it = aggDecl->get_members().begin(); |
---|
| 734 | for ( ; it != aggDecl->get_members().end(); ) { |
---|
| 735 | std::list< Declaration * >::iterator next = it+1; |
---|
| 736 | if ( dynamic_cast< TypedefDecl * >( *it ) ) { |
---|
| 737 | delete *it; |
---|
| 738 | aggDecl->get_members().erase( it ); |
---|
| 739 | } // if |
---|
| 740 | it = next; |
---|
| 741 | } |
---|
| 742 | return aggDecl; |
---|
| 743 | } |
---|
| 744 | |
---|
[45161b4d] | 745 | template<typename AggDecl> |
---|
| 746 | void EliminateTypedef::addImplicitTypedef( AggDecl * aggDecl ) { |
---|
| 747 | if ( typedefNames.count( aggDecl->get_name() ) == 0 ) { |
---|
[62e5546] | 748 | Type *type = nullptr; |
---|
[45161b4d] | 749 | if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( aggDecl ) ) { |
---|
| 750 | type = new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ); |
---|
| 751 | } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( aggDecl ) ) { |
---|
| 752 | type = new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ); |
---|
| 753 | } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( aggDecl ) ) { |
---|
| 754 | type = new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ); |
---|
| 755 | } // if |
---|
[a7c90d4] | 756 | TypedefDeclPtr tyDecl( new TypedefDecl( aggDecl->get_name(), DeclarationNode::StorageClasses(), type ) ); |
---|
[46f6134] | 757 | typedefNames[ aggDecl->get_name() ] = std::make_pair( std::move( tyDecl ), scopeLevel ); |
---|
[45161b4d] | 758 | } // if |
---|
| 759 | } |
---|
[4e06c1e] | 760 | |
---|
[85c4ef0] | 761 | Declaration *EliminateTypedef::mutate( StructDecl * structDecl ) { |
---|
[45161b4d] | 762 | addImplicitTypedef( structDecl ); |
---|
[85c4ef0] | 763 | Mutator::mutate( structDecl ); |
---|
| 764 | return handleAggregate( structDecl ); |
---|
| 765 | } |
---|
| 766 | |
---|
| 767 | Declaration *EliminateTypedef::mutate( UnionDecl * unionDecl ) { |
---|
[45161b4d] | 768 | addImplicitTypedef( unionDecl ); |
---|
[85c4ef0] | 769 | Mutator::mutate( unionDecl ); |
---|
| 770 | return handleAggregate( unionDecl ); |
---|
| 771 | } |
---|
| 772 | |
---|
| 773 | Declaration *EliminateTypedef::mutate( EnumDecl * enumDecl ) { |
---|
[45161b4d] | 774 | addImplicitTypedef( enumDecl ); |
---|
[85c4ef0] | 775 | Mutator::mutate( enumDecl ); |
---|
| 776 | return handleAggregate( enumDecl ); |
---|
| 777 | } |
---|
| 778 | |
---|
[45161b4d] | 779 | Declaration *EliminateTypedef::mutate( TraitDecl * contextDecl ) { |
---|
[85c4ef0] | 780 | Mutator::mutate( contextDecl ); |
---|
| 781 | return handleAggregate( contextDecl ); |
---|
| 782 | } |
---|
| 783 | |
---|
[d1969a6] | 784 | void VerifyCtorDtorAssign::verify( std::list< Declaration * > & translationUnit ) { |
---|
| 785 | VerifyCtorDtorAssign verifier; |
---|
[9cb8e88d] | 786 | acceptAll( translationUnit, verifier ); |
---|
| 787 | } |
---|
| 788 | |
---|
[d1969a6] | 789 | void VerifyCtorDtorAssign::visit( FunctionDecl * funcDecl ) { |
---|
[9cb8e88d] | 790 | FunctionType * funcType = funcDecl->get_functionType(); |
---|
| 791 | std::list< DeclarationWithType * > &returnVals = funcType->get_returnVals(); |
---|
| 792 | std::list< DeclarationWithType * > ¶ms = funcType->get_parameters(); |
---|
| 793 | |
---|
[d1969a6] | 794 | if ( InitTweak::isCtorDtorAssign( funcDecl->get_name() ) ) { |
---|
[9cb8e88d] | 795 | if ( params.size() == 0 ) { |
---|
[d1969a6] | 796 | throw SemanticError( "Constructors, destructors, and assignment functions require at least one parameter ", funcDecl ); |
---|
[9cb8e88d] | 797 | } |
---|
| 798 | if ( ! dynamic_cast< PointerType * >( params.front()->get_type() ) ) { |
---|
[d1969a6] | 799 | throw SemanticError( "First parameter of a constructor, destructor, or assignment function must be a pointer ", funcDecl ); |
---|
[9cb8e88d] | 800 | } |
---|
[d1969a6] | 801 | if ( InitTweak::isCtorDtor( funcDecl->get_name() ) && returnVals.size() != 0 ) { |
---|
[9cb8e88d] | 802 | throw SemanticError( "Constructors and destructors cannot have explicit return values ", funcDecl ); |
---|
| 803 | } |
---|
| 804 | } |
---|
| 805 | |
---|
| 806 | Visitor::visit( funcDecl ); |
---|
| 807 | } |
---|
[70a06f6] | 808 | |
---|
[630a82a] | 809 | DeclarationWithType * CompoundLiteral::mutate( ObjectDecl *objectDecl ) { |
---|
[a7c90d4] | 810 | storageClasses = objectDecl->get_storageClasses(); |
---|
[630a82a] | 811 | DeclarationWithType * temp = Mutator::mutate( objectDecl ); |
---|
| 812 | return temp; |
---|
| 813 | } |
---|
| 814 | |
---|
| 815 | Expression *CompoundLiteral::mutate( CompoundLiteralExpr *compLitExpr ) { |
---|
| 816 | // transform [storage_class] ... (struct S){ 3, ... }; |
---|
| 817 | // into [storage_class] struct S temp = { 3, ... }; |
---|
| 818 | static UniqueName indexName( "_compLit" ); |
---|
| 819 | |
---|
[a7c90d4] | 820 | ObjectDecl *tempvar = new ObjectDecl( indexName.newName(), storageClasses, LinkageSpec::C, 0, compLitExpr->get_type(), compLitExpr->get_initializer() ); |
---|
[630a82a] | 821 | compLitExpr->set_type( 0 ); |
---|
| 822 | compLitExpr->set_initializer( 0 ); |
---|
| 823 | delete compLitExpr; |
---|
| 824 | DeclarationWithType * newtempvar = mutate( tempvar ); |
---|
| 825 | addDeclaration( newtempvar ); // add modified temporary to current block |
---|
| 826 | return new VariableExpr( newtempvar ); |
---|
| 827 | } |
---|
[cce9429] | 828 | |
---|
| 829 | void ReturnTypeFixer::fix( std::list< Declaration * > &translationUnit ) { |
---|
| 830 | ReturnTypeFixer fixer; |
---|
| 831 | acceptAll( translationUnit, fixer ); |
---|
| 832 | } |
---|
| 833 | |
---|
[9facf3b] | 834 | void ReturnTypeFixer::visit( FunctionDecl * functionDecl ) { |
---|
| 835 | Parent::visit( functionDecl ); |
---|
| 836 | FunctionType * ftype = functionDecl->get_functionType(); |
---|
| 837 | std::list< DeclarationWithType * > & retVals = ftype->get_returnVals(); |
---|
| 838 | assertf( retVals.size() == 0 || retVals.size() == 1, "Function %s has too many return values: %d", functionDecl->get_name().c_str(), retVals.size() ); |
---|
| 839 | if ( retVals.size() == 1 ) { |
---|
| 840 | // ensure all function return values have a name - use the name of the function to disambiguate (this also provides a nice bit of help for debugging) |
---|
| 841 | // ensure other return values have a name |
---|
| 842 | DeclarationWithType * ret = retVals.front(); |
---|
| 843 | if ( ret->get_name() == "" ) { |
---|
| 844 | ret->set_name( toString( "_retval_", CodeGen::genName( functionDecl ) ) ); |
---|
| 845 | } |
---|
| 846 | } |
---|
| 847 | } |
---|
[cce9429] | 848 | |
---|
[9facf3b] | 849 | void ReturnTypeFixer::visit( FunctionType * ftype ) { |
---|
[cce9429] | 850 | // xxx - need to handle named return values - this information needs to be saved somehow |
---|
| 851 | // so that resolution has access to the names. |
---|
| 852 | // Note that this pass needs to happen early so that other passes which look for tuple types |
---|
| 853 | // find them in all of the right places, including function return types. |
---|
| 854 | std::list< DeclarationWithType * > & retVals = ftype->get_returnVals(); |
---|
| 855 | if ( retVals.size() > 1 ) { |
---|
| 856 | // generate a single return parameter which is the tuple of all of the return values |
---|
| 857 | TupleType * tupleType = safe_dynamic_cast< TupleType * >( ResolvExpr::extractResultType( ftype ) ); |
---|
| 858 | // ensure return value is not destructed by explicitly creating an empty ListInit node wherein maybeConstruct is false. |
---|
[a7c90d4] | 859 | ObjectDecl * newRet = new ObjectDecl( "", DeclarationNode::StorageClasses(), LinkageSpec::Cforall, 0, tupleType, new ListInit( std::list<Initializer*>(), noDesignators, false ) ); |
---|
[cce9429] | 860 | deleteAll( retVals ); |
---|
| 861 | retVals.clear(); |
---|
| 862 | retVals.push_back( newRet ); |
---|
| 863 | } |
---|
| 864 | } |
---|
[51b7345] | 865 | } // namespace SymTab |
---|
[0dd3a2f] | 866 | |
---|
| 867 | // Local Variables: // |
---|
| 868 | // tab-width: 4 // |
---|
| 869 | // mode: c++ // |
---|
| 870 | // compile-command: "make install" // |
---|
| 871 | // End: // |
---|