[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
|
---|
| 12 | // Last Modified On : Tue Jul 12 17:49:21 2016
|
---|
| 13 | // Update Count : 298
|
---|
[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.
|
---|
[51b73452] | 39 |
|
---|
| 40 | #include <list>
|
---|
| 41 | #include <iterator>
|
---|
[e491159] | 42 | #include "Common/ScopedMap.h"
|
---|
[630a82a] | 43 | #include "Common/utility.h"
|
---|
| 44 | #include "Common/UniqueName.h"
|
---|
[51b73452] | 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"
|
---|
[51b73452] | 50 | #include "SynTree/Statement.h"
|
---|
| 51 | #include "SynTree/TypeSubstitution.h"
|
---|
[68cd1ce] | 52 | #include "Indexer.h"
|
---|
[51b73452] | 53 | #include "FixFunction.h"
|
---|
[cc79d97] | 54 | // #include "ImplementationType.h"
|
---|
[630a82a] | 55 | #include "GenPoly/DeclMutator.h"
|
---|
[51b73452] | 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"
|
---|
[51b73452] | 63 |
|
---|
[c8ffe20b] | 64 | #define debugPrint( x ) if ( doDebug ) { std::cout << x; }
|
---|
[51b73452] | 65 |
|
---|
| 66 | namespace SymTab {
|
---|
[a08ba92] | 67 | class HoistStruct : public Visitor {
|
---|
| 68 | public:
|
---|
[82dd287] | 69 | /// Flattens nested struct types
|
---|
[0dd3a2f] | 70 | static void hoistStruct( std::list< Declaration * > &translationUnit );
|
---|
[9cb8e88d] | 71 |
|
---|
[0dd3a2f] | 72 | std::list< Declaration * > &get_declsToAdd() { return declsToAdd; }
|
---|
[9cb8e88d] | 73 |
|
---|
[0dd3a2f] | 74 | virtual void visit( StructDecl *aggregateDecl );
|
---|
| 75 | virtual void visit( UnionDecl *aggregateDecl );
|
---|
[c8ffe20b] | 76 |
|
---|
[0dd3a2f] | 77 | virtual void visit( CompoundStmt *compoundStmt );
|
---|
| 78 | virtual void visit( SwitchStmt *switchStmt );
|
---|
[a08ba92] | 79 | private:
|
---|
[0dd3a2f] | 80 | HoistStruct();
|
---|
[c8ffe20b] | 81 |
|
---|
[0dd3a2f] | 82 | template< typename AggDecl > void handleAggregate( AggDecl *aggregateDecl );
|
---|
[c8ffe20b] | 83 |
|
---|
[0dd3a2f] | 84 | std::list< Declaration * > declsToAdd;
|
---|
| 85 | bool inStruct;
|
---|
[a08ba92] | 86 | };
|
---|
[c8ffe20b] | 87 |
|
---|
[de91427b] | 88 | /// Replaces enum types by int, and function or array types in function parameter and return lists by appropriate pointers.
|
---|
[bda58ad] | 89 | class EnumAndPointerDecayPass : public Visitor {
|
---|
[0dd3a2f] | 90 | typedef Visitor Parent;
|
---|
| 91 | virtual void visit( EnumDecl *aggregateDecl );
|
---|
| 92 | virtual void visit( FunctionType *func );
|
---|
[a08ba92] | 93 | };
|
---|
[82dd287] | 94 |
|
---|
| 95 | /// Associates forward declarations of aggregates with their definitions
|
---|
[62e5546] | 96 | class Pass2 final : public Indexer {
|
---|
[0dd3a2f] | 97 | typedef Indexer Parent;
|
---|
[a08ba92] | 98 | public:
|
---|
[0dd3a2f] | 99 | Pass2( bool doDebug, const Indexer *indexer );
|
---|
[a08ba92] | 100 | private:
|
---|
[62e5546] | 101 | using Indexer::visit;
|
---|
| 102 | void visit( StructInstType *structInst ) final;
|
---|
| 103 | void visit( UnionInstType *unionInst ) final;
|
---|
| 104 | void visit( TraitInstType *contextInst ) final;
|
---|
| 105 | void visit( StructDecl *structDecl ) final;
|
---|
| 106 | void visit( UnionDecl *unionDecl ) final;
|
---|
| 107 | void visit( TypeInstType *typeInst ) final;
|
---|
[0dd3a2f] | 108 |
|
---|
| 109 | const Indexer *indexer;
|
---|
[9cb8e88d] | 110 |
|
---|
[0dd3a2f] | 111 | typedef std::map< std::string, std::list< StructInstType * > > ForwardStructsType;
|
---|
| 112 | typedef std::map< std::string, std::list< UnionInstType * > > ForwardUnionsType;
|
---|
| 113 | ForwardStructsType forwardStructs;
|
---|
| 114 | ForwardUnionsType forwardUnions;
|
---|
[a08ba92] | 115 | };
|
---|
[c8ffe20b] | 116 |
|
---|
[82dd287] | 117 | /// Replaces array and function types in forall lists by appropriate pointer type
|
---|
[a08ba92] | 118 | class Pass3 : public Indexer {
|
---|
[0dd3a2f] | 119 | typedef Indexer Parent;
|
---|
[a08ba92] | 120 | public:
|
---|
[0dd3a2f] | 121 | Pass3( const Indexer *indexer );
|
---|
[a08ba92] | 122 | private:
|
---|
[0dd3a2f] | 123 | virtual void visit( ObjectDecl *object );
|
---|
| 124 | virtual void visit( FunctionDecl *func );
|
---|
[c8ffe20b] | 125 |
|
---|
[0dd3a2f] | 126 | const Indexer *indexer;
|
---|
[a08ba92] | 127 | };
|
---|
[c8ffe20b] | 128 |
|
---|
[de91427b] | 129 | class ReturnChecker : public Visitor {
|
---|
| 130 | public:
|
---|
| 131 | /// Checks that return statements return nothing if their return type is void
|
---|
| 132 | /// and return something if the return type is non-void.
|
---|
| 133 | static void checkFunctionReturns( std::list< Declaration * > & translationUnit );
|
---|
| 134 | private:
|
---|
| 135 | virtual void visit( FunctionDecl * functionDecl );
|
---|
| 136 |
|
---|
| 137 | virtual void visit( ReturnStmt * returnStmt );
|
---|
| 138 |
|
---|
| 139 | std::list< DeclarationWithType * > returnVals;
|
---|
| 140 | };
|
---|
| 141 |
|
---|
[a08ba92] | 142 | class EliminateTypedef : public Mutator {
|
---|
| 143 | public:
|
---|
[de91427b] | 144 | EliminateTypedef() : scopeLevel( 0 ) {}
|
---|
| 145 | /// Replaces typedefs by forward declarations
|
---|
[0dd3a2f] | 146 | static void eliminateTypedef( std::list< Declaration * > &translationUnit );
|
---|
[a08ba92] | 147 | private:
|
---|
[0dd3a2f] | 148 | virtual Declaration *mutate( TypedefDecl *typeDecl );
|
---|
| 149 | virtual TypeDecl *mutate( TypeDecl *typeDecl );
|
---|
| 150 | virtual DeclarationWithType *mutate( FunctionDecl *funcDecl );
|
---|
[1db21619] | 151 | virtual DeclarationWithType *mutate( ObjectDecl *objDecl );
|
---|
[0dd3a2f] | 152 | virtual CompoundStmt *mutate( CompoundStmt *compoundStmt );
|
---|
| 153 | virtual Type *mutate( TypeInstType *aggregateUseType );
|
---|
| 154 | virtual Expression *mutate( CastExpr *castExpr );
|
---|
[cc79d97] | 155 |
|
---|
[85c4ef0] | 156 | virtual Declaration *mutate( StructDecl * structDecl );
|
---|
| 157 | virtual Declaration *mutate( UnionDecl * unionDecl );
|
---|
| 158 | virtual Declaration *mutate( EnumDecl * enumDecl );
|
---|
[4040425] | 159 | virtual Declaration *mutate( TraitDecl * contextDecl );
|
---|
[85c4ef0] | 160 |
|
---|
| 161 | template<typename AggDecl>
|
---|
| 162 | AggDecl *handleAggregate( AggDecl * aggDecl );
|
---|
| 163 |
|
---|
[45161b4d] | 164 | template<typename AggDecl>
|
---|
| 165 | void addImplicitTypedef( AggDecl * aggDecl );
|
---|
[70a06f6] | 166 |
|
---|
[46f6134] | 167 | typedef std::unique_ptr<TypedefDecl> TypedefDeclPtr;
|
---|
[e491159] | 168 | typedef ScopedMap< std::string, std::pair< TypedefDeclPtr, int > > TypedefMap;
|
---|
[679864e1] | 169 | typedef std::map< std::string, TypeDecl * > TypeDeclMap;
|
---|
[cc79d97] | 170 | TypedefMap typedefNames;
|
---|
[679864e1] | 171 | TypeDeclMap typedeclNames;
|
---|
[cc79d97] | 172 | int scopeLevel;
|
---|
[a08ba92] | 173 | };
|
---|
[c8ffe20b] | 174 |
|
---|
[d1969a6] | 175 | class VerifyCtorDtorAssign : public Visitor {
|
---|
[9cb8e88d] | 176 | public:
|
---|
[d1969a6] | 177 | /// ensure that constructors, destructors, and assignment have at least one
|
---|
| 178 | /// parameter, the first of which must be a pointer, and that ctor/dtors have no
|
---|
[9cb8e88d] | 179 | /// return values.
|
---|
| 180 | static void verify( std::list< Declaration * > &translationUnit );
|
---|
| 181 |
|
---|
| 182 | virtual void visit( FunctionDecl *funcDecl );
|
---|
[5f98ce5] | 183 | };
|
---|
[70a06f6] | 184 |
|
---|
[62e5546] | 185 | class CompoundLiteral final : public GenPoly::DeclMutator {
|
---|
[630a82a] | 186 | DeclarationNode::StorageClass storageclass = DeclarationNode::NoStorageClass;
|
---|
| 187 |
|
---|
[62e5546] | 188 | using GenPoly::DeclMutator::mutate;
|
---|
| 189 | DeclarationWithType * mutate( ObjectDecl *objectDecl ) final;
|
---|
| 190 | Expression *mutate( CompoundLiteralExpr *compLitExpr ) final;
|
---|
[9cb8e88d] | 191 | };
|
---|
| 192 |
|
---|
[a08ba92] | 193 | void validate( std::list< Declaration * > &translationUnit, bool doDebug ) {
|
---|
[bda58ad] | 194 | EnumAndPointerDecayPass epc;
|
---|
[0dd3a2f] | 195 | Pass2 pass2( doDebug, 0 );
|
---|
| 196 | Pass3 pass3( 0 );
|
---|
[630a82a] | 197 | CompoundLiteral compoundliteral;
|
---|
| 198 |
|
---|
[0dd3a2f] | 199 | EliminateTypedef::eliminateTypedef( translationUnit );
|
---|
| 200 | HoistStruct::hoistStruct( translationUnit );
|
---|
[bda58ad] | 201 | autogenerateRoutines( translationUnit ); // moved up, used to be below compoundLiteral - currently needs EnumAndPointerDecayPass
|
---|
| 202 | acceptAll( translationUnit, epc );
|
---|
[0dd3a2f] | 203 | acceptAll( translationUnit, pass2 );
|
---|
[de91427b] | 204 | ReturnChecker::checkFunctionReturns( translationUnit );
|
---|
[40e636a] | 205 | compoundliteral.mutateDeclarationList( translationUnit );
|
---|
[0dd3a2f] | 206 | acceptAll( translationUnit, pass3 );
|
---|
[d1969a6] | 207 | VerifyCtorDtorAssign::verify( translationUnit );
|
---|
[a08ba92] | 208 | }
|
---|
[9cb8e88d] | 209 |
|
---|
[a08ba92] | 210 | void validateType( Type *type, const Indexer *indexer ) {
|
---|
[bda58ad] | 211 | EnumAndPointerDecayPass epc;
|
---|
[0dd3a2f] | 212 | Pass2 pass2( false, indexer );
|
---|
| 213 | Pass3 pass3( indexer );
|
---|
[bda58ad] | 214 | type->accept( epc );
|
---|
[0dd3a2f] | 215 | type->accept( pass2 );
|
---|
| 216 | type->accept( pass3 );
|
---|
[a08ba92] | 217 | }
|
---|
[c8ffe20b] | 218 |
|
---|
[a08ba92] | 219 | void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) {
|
---|
[0dd3a2f] | 220 | HoistStruct hoister;
|
---|
| 221 | acceptAndAdd( translationUnit, hoister, true );
|
---|
[a08ba92] | 222 | }
|
---|
[c8ffe20b] | 223 |
|
---|
[a08ba92] | 224 | HoistStruct::HoistStruct() : inStruct( false ) {
|
---|
| 225 | }
|
---|
[c8ffe20b] | 226 |
|
---|
[a08ba92] | 227 | void filter( std::list< Declaration * > &declList, bool (*pred)( Declaration * ), bool doDelete ) {
|
---|
[0dd3a2f] | 228 | std::list< Declaration * >::iterator i = declList.begin();
|
---|
| 229 | while ( i != declList.end() ) {
|
---|
| 230 | std::list< Declaration * >::iterator next = i;
|
---|
| 231 | ++next;
|
---|
| 232 | if ( pred( *i ) ) {
|
---|
| 233 | if ( doDelete ) {
|
---|
| 234 | delete *i;
|
---|
| 235 | } // if
|
---|
| 236 | declList.erase( i );
|
---|
| 237 | } // if
|
---|
| 238 | i = next;
|
---|
| 239 | } // while
|
---|
[a08ba92] | 240 | }
|
---|
[c8ffe20b] | 241 |
|
---|
[a08ba92] | 242 | bool isStructOrUnion( Declaration *decl ) {
|
---|
[0dd3a2f] | 243 | return dynamic_cast< StructDecl * >( decl ) || dynamic_cast< UnionDecl * >( decl );
|
---|
[a08ba92] | 244 | }
|
---|
[b3b2077] | 245 | // xxx - shouldn't this be declsToAddBefore?
|
---|
[a08ba92] | 246 | template< typename AggDecl >
|
---|
| 247 | void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) {
|
---|
[0dd3a2f] | 248 | if ( inStruct ) {
|
---|
| 249 | // Add elements in stack order corresponding to nesting structure.
|
---|
| 250 | declsToAdd.push_front( aggregateDecl );
|
---|
| 251 | Visitor::visit( aggregateDecl );
|
---|
| 252 | } else {
|
---|
| 253 | inStruct = true;
|
---|
| 254 | Visitor::visit( aggregateDecl );
|
---|
| 255 | inStruct = false;
|
---|
| 256 | } // if
|
---|
| 257 | // Always remove the hoisted aggregate from the inner structure.
|
---|
| 258 | filter( aggregateDecl->get_members(), isStructOrUnion, false );
|
---|
[a08ba92] | 259 | }
|
---|
[c8ffe20b] | 260 |
|
---|
[a08ba92] | 261 | void HoistStruct::visit( StructDecl *aggregateDecl ) {
|
---|
[0dd3a2f] | 262 | handleAggregate( aggregateDecl );
|
---|
[a08ba92] | 263 | }
|
---|
[c8ffe20b] | 264 |
|
---|
[a08ba92] | 265 | void HoistStruct::visit( UnionDecl *aggregateDecl ) {
|
---|
[0dd3a2f] | 266 | handleAggregate( aggregateDecl );
|
---|
[a08ba92] | 267 | }
|
---|
[c8ffe20b] | 268 |
|
---|
[a08ba92] | 269 | void HoistStruct::visit( CompoundStmt *compoundStmt ) {
|
---|
[0dd3a2f] | 270 | addVisit( compoundStmt, *this );
|
---|
[a08ba92] | 271 | }
|
---|
[c8ffe20b] | 272 |
|
---|
[a08ba92] | 273 | void HoistStruct::visit( SwitchStmt *switchStmt ) {
|
---|
[0dd3a2f] | 274 | addVisit( switchStmt, *this );
|
---|
[a08ba92] | 275 | }
|
---|
[c8ffe20b] | 276 |
|
---|
[bda58ad] | 277 | void EnumAndPointerDecayPass::visit( EnumDecl *enumDecl ) {
|
---|
[0dd3a2f] | 278 | // Set the type of each member of the enumeration to be EnumConstant
|
---|
| 279 | for ( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) {
|
---|
[f6d7e0f] | 280 | ObjectDecl * obj = dynamic_cast< ObjectDecl * >( *i );
|
---|
[0dd3a2f] | 281 | assert( obj );
|
---|
[a436947] | 282 | obj->set_type( new EnumInstType( Type::Qualifiers( true, false, false, false, false, false ), enumDecl->get_name() ) );
|
---|
[0dd3a2f] | 283 | } // for
|
---|
| 284 | Parent::visit( enumDecl );
|
---|
[a08ba92] | 285 | }
|
---|
[51b73452] | 286 |
|
---|
[a08ba92] | 287 | namespace {
|
---|
[83de11e] | 288 | template< typename DWTList >
|
---|
| 289 | void fixFunctionList( DWTList & dwts, FunctionType * func ) {
|
---|
[0dd3a2f] | 290 | // the only case in which "void" is valid is where it is the only one in the list; then it should be removed
|
---|
| 291 | // entirely other fix ups are handled by the FixFunction class
|
---|
[83de11e] | 292 | typedef typename DWTList::iterator DWTIterator;
|
---|
| 293 | DWTIterator begin( dwts.begin() ), end( dwts.end() );
|
---|
[0dd3a2f] | 294 | if ( begin == end ) return;
|
---|
| 295 | FixFunction fixer;
|
---|
| 296 | DWTIterator i = begin;
|
---|
[83de11e] | 297 | *i = (*i)->acceptMutator( fixer );
|
---|
[0dd3a2f] | 298 | if ( fixer.get_isVoid() ) {
|
---|
| 299 | DWTIterator j = i;
|
---|
| 300 | ++i;
|
---|
[bda58ad] | 301 | delete *j;
|
---|
[83de11e] | 302 | dwts.erase( j );
|
---|
[9cb8e88d] | 303 | if ( i != end ) {
|
---|
[0dd3a2f] | 304 | throw SemanticError( "invalid type void in function type ", func );
|
---|
| 305 | } // if
|
---|
| 306 | } else {
|
---|
| 307 | ++i;
|
---|
| 308 | for ( ; i != end; ++i ) {
|
---|
| 309 | FixFunction fixer;
|
---|
| 310 | *i = (*i )->acceptMutator( fixer );
|
---|
| 311 | if ( fixer.get_isVoid() ) {
|
---|
| 312 | throw SemanticError( "invalid type void in function type ", func );
|
---|
| 313 | } // if
|
---|
| 314 | } // for
|
---|
| 315 | } // if
|
---|
| 316 | }
|
---|
[a08ba92] | 317 | }
|
---|
[c8ffe20b] | 318 |
|
---|
[bda58ad] | 319 | void EnumAndPointerDecayPass::visit( FunctionType *func ) {
|
---|
[0dd3a2f] | 320 | // Fix up parameters and return types
|
---|
[83de11e] | 321 | fixFunctionList( func->get_parameters(), func );
|
---|
| 322 | fixFunctionList( func->get_returnVals(), func );
|
---|
[0dd3a2f] | 323 | Visitor::visit( func );
|
---|
[a08ba92] | 324 | }
|
---|
[c8ffe20b] | 325 |
|
---|
[a08ba92] | 326 | Pass2::Pass2( bool doDebug, const Indexer *other_indexer ) : Indexer( doDebug ) {
|
---|
[0dd3a2f] | 327 | if ( other_indexer ) {
|
---|
| 328 | indexer = other_indexer;
|
---|
| 329 | } else {
|
---|
| 330 | indexer = this;
|
---|
| 331 | } // if
|
---|
[a08ba92] | 332 | }
|
---|
[c8ffe20b] | 333 |
|
---|
[a08ba92] | 334 | void Pass2::visit( StructInstType *structInst ) {
|
---|
[0dd3a2f] | 335 | Parent::visit( structInst );
|
---|
| 336 | StructDecl *st = indexer->lookupStruct( structInst->get_name() );
|
---|
| 337 | // it's not a semantic error if the struct is not found, just an implicit forward declaration
|
---|
| 338 | if ( st ) {
|
---|
[98735ef] | 339 | //assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() );
|
---|
[0dd3a2f] | 340 | structInst->set_baseStruct( st );
|
---|
| 341 | } // if
|
---|
| 342 | if ( ! st || st->get_members().empty() ) {
|
---|
| 343 | // use of forward declaration
|
---|
| 344 | forwardStructs[ structInst->get_name() ].push_back( structInst );
|
---|
| 345 | } // if
|
---|
[a08ba92] | 346 | }
|
---|
[c8ffe20b] | 347 |
|
---|
[a08ba92] | 348 | void Pass2::visit( UnionInstType *unionInst ) {
|
---|
[0dd3a2f] | 349 | Parent::visit( unionInst );
|
---|
| 350 | UnionDecl *un = indexer->lookupUnion( unionInst->get_name() );
|
---|
| 351 | // it's not a semantic error if the union is not found, just an implicit forward declaration
|
---|
| 352 | if ( un ) {
|
---|
| 353 | unionInst->set_baseUnion( un );
|
---|
| 354 | } // if
|
---|
| 355 | if ( ! un || un->get_members().empty() ) {
|
---|
| 356 | // use of forward declaration
|
---|
| 357 | forwardUnions[ unionInst->get_name() ].push_back( unionInst );
|
---|
| 358 | } // if
|
---|
[a08ba92] | 359 | }
|
---|
[c8ffe20b] | 360 |
|
---|
[4040425] | 361 | void Pass2::visit( TraitInstType *contextInst ) {
|
---|
[0dd3a2f] | 362 | Parent::visit( contextInst );
|
---|
[4040425] | 363 | TraitDecl *ctx = indexer->lookupTrait( contextInst->get_name() );
|
---|
[0dd3a2f] | 364 | if ( ! ctx ) {
|
---|
| 365 | throw SemanticError( "use of undeclared context " + contextInst->get_name() );
|
---|
[17cd4eb] | 366 | } // if
|
---|
[0dd3a2f] | 367 | for ( std::list< TypeDecl * >::const_iterator i = ctx->get_parameters().begin(); i != ctx->get_parameters().end(); ++i ) {
|
---|
| 368 | for ( std::list< DeclarationWithType * >::const_iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) {
|
---|
[4040425] | 369 | if ( TraitInstType *otherCtx = dynamic_cast< TraitInstType * >(*assert ) ) {
|
---|
[0dd3a2f] | 370 | cloneAll( otherCtx->get_members(), contextInst->get_members() );
|
---|
| 371 | } else {
|
---|
| 372 | contextInst->get_members().push_back( (*assert )->clone() );
|
---|
| 373 | } // if
|
---|
| 374 | } // for
|
---|
| 375 | } // for
|
---|
[51b986f] | 376 |
|
---|
| 377 | if ( ctx->get_parameters().size() != contextInst->get_parameters().size() ) {
|
---|
| 378 | throw SemanticError( "incorrect number of context parameters: ", contextInst );
|
---|
| 379 | } // if
|
---|
| 380 |
|
---|
[79970ed] | 381 | // need to clone members of the context for ownership purposes
|
---|
| 382 | std::list< Declaration * > members;
|
---|
| 383 | std::transform( ctx->get_members().begin(), ctx->get_members().end(), back_inserter( members ), [](Declaration * dwt) { return dwt->clone(); } );
|
---|
| 384 |
|
---|
| 385 | applySubstitution( ctx->get_parameters().begin(), ctx->get_parameters().end(), contextInst->get_parameters().begin(), members.begin(), members.end(), back_inserter( contextInst->get_members() ) );
|
---|
[a08ba92] | 386 | }
|
---|
[c8ffe20b] | 387 |
|
---|
[a08ba92] | 388 | void Pass2::visit( StructDecl *structDecl ) {
|
---|
[677c1be] | 389 | // visit struct members first so that the types of self-referencing members are updated properly
|
---|
| 390 | Parent::visit( structDecl );
|
---|
[0dd3a2f] | 391 | if ( ! structDecl->get_members().empty() ) {
|
---|
| 392 | ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() );
|
---|
| 393 | if ( fwds != forwardStructs.end() ) {
|
---|
| 394 | for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
|
---|
| 395 | (*inst )->set_baseStruct( structDecl );
|
---|
| 396 | } // for
|
---|
| 397 | forwardStructs.erase( fwds );
|
---|
| 398 | } // if
|
---|
| 399 | } // if
|
---|
[a08ba92] | 400 | }
|
---|
[c8ffe20b] | 401 |
|
---|
[a08ba92] | 402 | void Pass2::visit( UnionDecl *unionDecl ) {
|
---|
[677c1be] | 403 | Parent::visit( unionDecl );
|
---|
[0dd3a2f] | 404 | if ( ! unionDecl->get_members().empty() ) {
|
---|
| 405 | ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() );
|
---|
| 406 | if ( fwds != forwardUnions.end() ) {
|
---|
| 407 | for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
|
---|
| 408 | (*inst )->set_baseUnion( unionDecl );
|
---|
| 409 | } // for
|
---|
| 410 | forwardUnions.erase( fwds );
|
---|
| 411 | } // if
|
---|
| 412 | } // if
|
---|
[a08ba92] | 413 | }
|
---|
[c8ffe20b] | 414 |
|
---|
[a08ba92] | 415 | void Pass2::visit( TypeInstType *typeInst ) {
|
---|
[0dd3a2f] | 416 | if ( NamedTypeDecl *namedTypeDecl = lookupType( typeInst->get_name() ) ) {
|
---|
| 417 | if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) {
|
---|
| 418 | typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype );
|
---|
| 419 | } // if
|
---|
| 420 | } // if
|
---|
[a08ba92] | 421 | }
|
---|
[c8ffe20b] | 422 |
|
---|
[a08ba92] | 423 | Pass3::Pass3( const Indexer *other_indexer ) : Indexer( false ) {
|
---|
[0dd3a2f] | 424 | if ( other_indexer ) {
|
---|
| 425 | indexer = other_indexer;
|
---|
| 426 | } else {
|
---|
| 427 | indexer = this;
|
---|
| 428 | } // if
|
---|
[a08ba92] | 429 | }
|
---|
[c8ffe20b] | 430 |
|
---|
[82dd287] | 431 | /// Fix up assertions
|
---|
[a08ba92] | 432 | void forallFixer( Type *func ) {
|
---|
[8c49c0e] | 433 | for ( Type::ForallList::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) {
|
---|
[0dd3a2f] | 434 | std::list< DeclarationWithType * > toBeDone, nextRound;
|
---|
| 435 | toBeDone.splice( toBeDone.end(), (*type )->get_assertions() );
|
---|
| 436 | while ( ! toBeDone.empty() ) {
|
---|
| 437 | for ( std::list< DeclarationWithType * >::iterator assertion = toBeDone.begin(); assertion != toBeDone.end(); ++assertion ) {
|
---|
[4040425] | 438 | if ( TraitInstType *ctx = dynamic_cast< TraitInstType * >( (*assertion )->get_type() ) ) {
|
---|
[0dd3a2f] | 439 | for ( std::list< Declaration * >::const_iterator i = ctx->get_members().begin(); i != ctx->get_members().end(); ++i ) {
|
---|
| 440 | DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *i );
|
---|
| 441 | assert( dwt );
|
---|
| 442 | nextRound.push_back( dwt->clone() );
|
---|
| 443 | }
|
---|
| 444 | delete ctx;
|
---|
| 445 | } else {
|
---|
| 446 | FixFunction fixer;
|
---|
| 447 | *assertion = (*assertion )->acceptMutator( fixer );
|
---|
| 448 | if ( fixer.get_isVoid() ) {
|
---|
| 449 | throw SemanticError( "invalid type void in assertion of function ", func );
|
---|
| 450 | }
|
---|
| 451 | (*type )->get_assertions().push_back( *assertion );
|
---|
| 452 | } // if
|
---|
| 453 | } // for
|
---|
| 454 | toBeDone.clear();
|
---|
| 455 | toBeDone.splice( toBeDone.end(), nextRound );
|
---|
| 456 | } // while
|
---|
| 457 | } // for
|
---|
[a08ba92] | 458 | }
|
---|
[c8ffe20b] | 459 |
|
---|
[a08ba92] | 460 | void Pass3::visit( ObjectDecl *object ) {
|
---|
[0dd3a2f] | 461 | forallFixer( object->get_type() );
|
---|
| 462 | if ( PointerType *pointer = dynamic_cast< PointerType * >( object->get_type() ) ) {
|
---|
| 463 | forallFixer( pointer->get_base() );
|
---|
| 464 | } // if
|
---|
| 465 | Parent::visit( object );
|
---|
| 466 | object->fixUniqueId();
|
---|
[a08ba92] | 467 | }
|
---|
[c8ffe20b] | 468 |
|
---|
[a08ba92] | 469 | void Pass3::visit( FunctionDecl *func ) {
|
---|
[0dd3a2f] | 470 | forallFixer( func->get_type() );
|
---|
| 471 | Parent::visit( func );
|
---|
| 472 | func->fixUniqueId();
|
---|
[a08ba92] | 473 | }
|
---|
[c8ffe20b] | 474 |
|
---|
[de91427b] | 475 | void ReturnChecker::checkFunctionReturns( std::list< Declaration * > & translationUnit ) {
|
---|
| 476 | ReturnChecker checker;
|
---|
| 477 | acceptAll( translationUnit, checker );
|
---|
| 478 | }
|
---|
| 479 |
|
---|
| 480 | void ReturnChecker::visit( FunctionDecl * functionDecl ) {
|
---|
| 481 | std::list< DeclarationWithType * > oldReturnVals = returnVals;
|
---|
| 482 | returnVals = functionDecl->get_functionType()->get_returnVals();
|
---|
| 483 | Visitor::visit( functionDecl );
|
---|
| 484 | returnVals = oldReturnVals;
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 | void ReturnChecker::visit( ReturnStmt * returnStmt ) {
|
---|
[74d1804] | 488 | // Previously this also checked for the existence of an expr paired with no return values on
|
---|
| 489 | // the function return type. This is incorrect, since you can have an expression attached to
|
---|
| 490 | // a return statement in a void-returning function in C. The expression is treated as if it
|
---|
| 491 | // were cast to void.
|
---|
[de91427b] | 492 | if ( returnStmt->get_expr() == NULL && returnVals.size() != 0 ) {
|
---|
| 493 | throw SemanticError( "Non-void function returns no values: " , returnStmt );
|
---|
| 494 | }
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 |
|
---|
[a08ba92] | 498 | bool isTypedef( Declaration *decl ) {
|
---|
[0dd3a2f] | 499 | return dynamic_cast< TypedefDecl * >( decl );
|
---|
[a08ba92] | 500 | }
|
---|
[c8ffe20b] | 501 |
|
---|
[a08ba92] | 502 | void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) {
|
---|
[0dd3a2f] | 503 | EliminateTypedef eliminator;
|
---|
| 504 | mutateAll( translationUnit, eliminator );
|
---|
[5f98ce5] | 505 | if ( eliminator.typedefNames.count( "size_t" ) ) {
|
---|
| 506 | // grab and remember declaration of size_t
|
---|
| 507 | SizeType = eliminator.typedefNames["size_t"].first->get_base()->clone();
|
---|
| 508 | } else {
|
---|
[40e636a] | 509 | // xxx - missing global typedef for size_t - default to long unsigned int, even though that may be wrong
|
---|
| 510 | // eventually should have a warning for this case.
|
---|
| 511 | SizeType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
|
---|
[5f98ce5] | 512 | }
|
---|
[0dd3a2f] | 513 | filter( translationUnit, isTypedef, true );
|
---|
[5f98ce5] | 514 |
|
---|
[a08ba92] | 515 | }
|
---|
[c8ffe20b] | 516 |
|
---|
[85c4ef0] | 517 | Type *EliminateTypedef::mutate( TypeInstType * typeInst ) {
|
---|
[9cb8e88d] | 518 | // instances of typedef types will come here. If it is an instance
|
---|
[cc79d97] | 519 | // of a typdef type, link the instance to its actual type.
|
---|
| 520 | TypedefMap::const_iterator def = typedefNames.find( typeInst->get_name() );
|
---|
[0dd3a2f] | 521 | if ( def != typedefNames.end() ) {
|
---|
[cc79d97] | 522 | Type *ret = def->second.first->get_base()->clone();
|
---|
[0dd3a2f] | 523 | ret->get_qualifiers() += typeInst->get_qualifiers();
|
---|
[0215a76f] | 524 | // place instance parameters on the typedef'd type
|
---|
| 525 | if ( ! typeInst->get_parameters().empty() ) {
|
---|
| 526 | ReferenceToType *rtt = dynamic_cast<ReferenceToType*>(ret);
|
---|
| 527 | if ( ! rtt ) {
|
---|
| 528 | throw SemanticError("cannot apply type parameters to base type of " + typeInst->get_name());
|
---|
| 529 | }
|
---|
| 530 | rtt->get_parameters().clear();
|
---|
[b644d6f] | 531 | cloneAll( typeInst->get_parameters(), rtt->get_parameters() );
|
---|
| 532 | mutateAll( rtt->get_parameters(), *this ); // recursively fix typedefs on parameters
|
---|
[1db21619] | 533 | } // if
|
---|
[0dd3a2f] | 534 | delete typeInst;
|
---|
| 535 | return ret;
|
---|
[679864e1] | 536 | } else {
|
---|
| 537 | TypeDeclMap::const_iterator base = typedeclNames.find( typeInst->get_name() );
|
---|
| 538 | assert( base != typedeclNames.end() );
|
---|
[1e8b02f5] | 539 | typeInst->set_baseType( base->second );
|
---|
[0dd3a2f] | 540 | } // if
|
---|
| 541 | return typeInst;
|
---|
[a08ba92] | 542 | }
|
---|
[c8ffe20b] | 543 |
|
---|
[85c4ef0] | 544 | Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) {
|
---|
[0dd3a2f] | 545 | Declaration *ret = Mutator::mutate( tyDecl );
|
---|
[5f98ce5] | 546 |
|
---|
[cc79d97] | 547 | if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) {
|
---|
[9cb8e88d] | 548 | // typedef to the same name from the same scope
|
---|
[cc79d97] | 549 | // must be from the same type
|
---|
| 550 |
|
---|
| 551 | Type * t1 = tyDecl->get_base();
|
---|
| 552 | Type * t2 = typedefNames[ tyDecl->get_name() ].first->get_base();
|
---|
[1cbca6e] | 553 | if ( ! ResolvExpr::typesCompatible( t1, t2, Indexer() ) ) {
|
---|
[cc79d97] | 554 | throw SemanticError( "cannot redefine typedef: " + tyDecl->get_name() );
|
---|
[85c4ef0] | 555 | }
|
---|
[cc79d97] | 556 | } else {
|
---|
[46f6134] | 557 | typedefNames[ tyDecl->get_name() ] = std::make_pair( TypedefDeclPtr( tyDecl ), scopeLevel );
|
---|
[cc79d97] | 558 | } // if
|
---|
| 559 |
|
---|
[0dd3a2f] | 560 | // When a typedef is a forward declaration:
|
---|
| 561 | // typedef struct screen SCREEN;
|
---|
| 562 | // the declaration portion must be retained:
|
---|
| 563 | // struct screen;
|
---|
| 564 | // because the expansion of the typedef is:
|
---|
| 565 | // void rtn( SCREEN *p ) => void rtn( struct screen *p )
|
---|
| 566 | // hence the type-name "screen" must be defined.
|
---|
| 567 | // Note, qualifiers on the typedef are superfluous for the forward declaration.
|
---|
| 568 | if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( tyDecl->get_base() ) ) {
|
---|
| 569 | return new StructDecl( aggDecl->get_name() );
|
---|
| 570 | } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( tyDecl->get_base() ) ) {
|
---|
| 571 | return new UnionDecl( aggDecl->get_name() );
|
---|
[956a9c77] | 572 | } else if ( EnumInstType *enumDecl = dynamic_cast< EnumInstType * >( tyDecl->get_base() ) ) {
|
---|
| 573 | return new EnumDecl( enumDecl->get_name() );
|
---|
[0dd3a2f] | 574 | } else {
|
---|
[46f6134] | 575 | return ret->clone();
|
---|
[0dd3a2f] | 576 | } // if
|
---|
[a08ba92] | 577 | }
|
---|
[c8ffe20b] | 578 |
|
---|
[85c4ef0] | 579 | TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) {
|
---|
[cc79d97] | 580 | TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() );
|
---|
[0dd3a2f] | 581 | if ( i != typedefNames.end() ) {
|
---|
| 582 | typedefNames.erase( i ) ;
|
---|
| 583 | } // if
|
---|
[679864e1] | 584 |
|
---|
| 585 | typedeclNames[ typeDecl->get_name() ] = typeDecl;
|
---|
[0dd3a2f] | 586 | return typeDecl;
|
---|
[a08ba92] | 587 | }
|
---|
[c8ffe20b] | 588 |
|
---|
[85c4ef0] | 589 | DeclarationWithType *EliminateTypedef::mutate( FunctionDecl * funcDecl ) {
|
---|
[46f6134] | 590 | typedefNames.beginScope();
|
---|
[0dd3a2f] | 591 | DeclarationWithType *ret = Mutator::mutate( funcDecl );
|
---|
[46f6134] | 592 | typedefNames.endScope();
|
---|
[0dd3a2f] | 593 | return ret;
|
---|
[a08ba92] | 594 | }
|
---|
[c8ffe20b] | 595 |
|
---|
[1db21619] | 596 | DeclarationWithType *EliminateTypedef::mutate( ObjectDecl * objDecl ) {
|
---|
[46f6134] | 597 | typedefNames.beginScope();
|
---|
[1db21619] | 598 | DeclarationWithType *ret = Mutator::mutate( objDecl );
|
---|
[46f6134] | 599 | typedefNames.endScope();
|
---|
[02e5ab6] | 600 | // is the type a function?
|
---|
[1db21619] | 601 | if ( FunctionType *funtype = dynamic_cast<FunctionType *>( ret->get_type() ) ) {
|
---|
[02e5ab6] | 602 | // replace the current object declaration with a function declaration
|
---|
[1db21619] | 603 | return new FunctionDecl( ret->get_name(), ret->get_storageClass(), ret->get_linkage(), funtype, 0, ret->get_isInline(), ret->get_isNoreturn() );
|
---|
| 604 | } else if ( objDecl->get_isInline() || objDecl->get_isNoreturn() ) {
|
---|
| 605 | throw SemanticError( "invalid inline or _Noreturn specification in declaration of ", objDecl );
|
---|
| 606 | } // if
|
---|
[0dd3a2f] | 607 | return ret;
|
---|
[a08ba92] | 608 | }
|
---|
[c8ffe20b] | 609 |
|
---|
[85c4ef0] | 610 | Expression *EliminateTypedef::mutate( CastExpr * castExpr ) {
|
---|
[46f6134] | 611 | typedefNames.beginScope();
|
---|
[0dd3a2f] | 612 | Expression *ret = Mutator::mutate( castExpr );
|
---|
[46f6134] | 613 | typedefNames.endScope();
|
---|
[0dd3a2f] | 614 | return ret;
|
---|
[a08ba92] | 615 | }
|
---|
[c8ffe20b] | 616 |
|
---|
[85c4ef0] | 617 | CompoundStmt *EliminateTypedef::mutate( CompoundStmt * compoundStmt ) {
|
---|
[46f6134] | 618 | typedefNames.beginScope();
|
---|
[cc79d97] | 619 | scopeLevel += 1;
|
---|
[0dd3a2f] | 620 | CompoundStmt *ret = Mutator::mutate( compoundStmt );
|
---|
[cc79d97] | 621 | scopeLevel -= 1;
|
---|
[0dd3a2f] | 622 | std::list< Statement * >::iterator i = compoundStmt->get_kids().begin();
|
---|
| 623 | while ( i != compoundStmt->get_kids().end() ) {
|
---|
[85c4ef0] | 624 | std::list< Statement * >::iterator next = i+1;
|
---|
[0dd3a2f] | 625 | if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( *i ) ) {
|
---|
| 626 | if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) {
|
---|
| 627 | delete *i;
|
---|
| 628 | compoundStmt->get_kids().erase( i );
|
---|
| 629 | } // if
|
---|
| 630 | } // if
|
---|
| 631 | i = next;
|
---|
| 632 | } // while
|
---|
[46f6134] | 633 | typedefNames.endScope();
|
---|
[0dd3a2f] | 634 | return ret;
|
---|
[a08ba92] | 635 | }
|
---|
[85c4ef0] | 636 |
|
---|
[45161b4d] | 637 | // there may be typedefs nested within aggregates in order for everything to work properly, these should be removed
|
---|
| 638 | // as well
|
---|
[85c4ef0] | 639 | template<typename AggDecl>
|
---|
| 640 | AggDecl *EliminateTypedef::handleAggregate( AggDecl * aggDecl ) {
|
---|
| 641 | std::list<Declaration *>::iterator it = aggDecl->get_members().begin();
|
---|
| 642 | for ( ; it != aggDecl->get_members().end(); ) {
|
---|
| 643 | std::list< Declaration * >::iterator next = it+1;
|
---|
| 644 | if ( dynamic_cast< TypedefDecl * >( *it ) ) {
|
---|
| 645 | delete *it;
|
---|
| 646 | aggDecl->get_members().erase( it );
|
---|
| 647 | } // if
|
---|
| 648 | it = next;
|
---|
| 649 | }
|
---|
| 650 | return aggDecl;
|
---|
| 651 | }
|
---|
| 652 |
|
---|
[45161b4d] | 653 | template<typename AggDecl>
|
---|
| 654 | void EliminateTypedef::addImplicitTypedef( AggDecl * aggDecl ) {
|
---|
| 655 | if ( typedefNames.count( aggDecl->get_name() ) == 0 ) {
|
---|
[62e5546] | 656 | Type *type = nullptr;
|
---|
[45161b4d] | 657 | if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( aggDecl ) ) {
|
---|
| 658 | type = new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() );
|
---|
| 659 | } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( aggDecl ) ) {
|
---|
| 660 | type = new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() );
|
---|
| 661 | } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( aggDecl ) ) {
|
---|
| 662 | type = new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() );
|
---|
| 663 | } // if
|
---|
[46f6134] | 664 | TypedefDeclPtr tyDecl( new TypedefDecl( aggDecl->get_name(), DeclarationNode::NoStorageClass, type ) );
|
---|
| 665 | typedefNames[ aggDecl->get_name() ] = std::make_pair( std::move( tyDecl ), scopeLevel );
|
---|
[45161b4d] | 666 | } // if
|
---|
| 667 | }
|
---|
[4e06c1e] | 668 |
|
---|
[85c4ef0] | 669 | Declaration *EliminateTypedef::mutate( StructDecl * structDecl ) {
|
---|
[45161b4d] | 670 | addImplicitTypedef( structDecl );
|
---|
[85c4ef0] | 671 | Mutator::mutate( structDecl );
|
---|
| 672 | return handleAggregate( structDecl );
|
---|
| 673 | }
|
---|
| 674 |
|
---|
| 675 | Declaration *EliminateTypedef::mutate( UnionDecl * unionDecl ) {
|
---|
[45161b4d] | 676 | addImplicitTypedef( unionDecl );
|
---|
[85c4ef0] | 677 | Mutator::mutate( unionDecl );
|
---|
| 678 | return handleAggregate( unionDecl );
|
---|
| 679 | }
|
---|
| 680 |
|
---|
| 681 | Declaration *EliminateTypedef::mutate( EnumDecl * enumDecl ) {
|
---|
[45161b4d] | 682 | addImplicitTypedef( enumDecl );
|
---|
[85c4ef0] | 683 | Mutator::mutate( enumDecl );
|
---|
| 684 | return handleAggregate( enumDecl );
|
---|
| 685 | }
|
---|
| 686 |
|
---|
[45161b4d] | 687 | Declaration *EliminateTypedef::mutate( TraitDecl * contextDecl ) {
|
---|
[85c4ef0] | 688 | Mutator::mutate( contextDecl );
|
---|
| 689 | return handleAggregate( contextDecl );
|
---|
| 690 | }
|
---|
| 691 |
|
---|
[d1969a6] | 692 | void VerifyCtorDtorAssign::verify( std::list< Declaration * > & translationUnit ) {
|
---|
| 693 | VerifyCtorDtorAssign verifier;
|
---|
[9cb8e88d] | 694 | acceptAll( translationUnit, verifier );
|
---|
| 695 | }
|
---|
| 696 |
|
---|
[d1969a6] | 697 | void VerifyCtorDtorAssign::visit( FunctionDecl * funcDecl ) {
|
---|
[9cb8e88d] | 698 | FunctionType * funcType = funcDecl->get_functionType();
|
---|
| 699 | std::list< DeclarationWithType * > &returnVals = funcType->get_returnVals();
|
---|
| 700 | std::list< DeclarationWithType * > ¶ms = funcType->get_parameters();
|
---|
| 701 |
|
---|
[d1969a6] | 702 | if ( InitTweak::isCtorDtorAssign( funcDecl->get_name() ) ) {
|
---|
[9cb8e88d] | 703 | if ( params.size() == 0 ) {
|
---|
[d1969a6] | 704 | throw SemanticError( "Constructors, destructors, and assignment functions require at least one parameter ", funcDecl );
|
---|
[9cb8e88d] | 705 | }
|
---|
| 706 | if ( ! dynamic_cast< PointerType * >( params.front()->get_type() ) ) {
|
---|
[d1969a6] | 707 | throw SemanticError( "First parameter of a constructor, destructor, or assignment function must be a pointer ", funcDecl );
|
---|
[9cb8e88d] | 708 | }
|
---|
[d1969a6] | 709 | if ( InitTweak::isCtorDtor( funcDecl->get_name() ) && returnVals.size() != 0 ) {
|
---|
[9cb8e88d] | 710 | throw SemanticError( "Constructors and destructors cannot have explicit return values ", funcDecl );
|
---|
| 711 | }
|
---|
| 712 | }
|
---|
| 713 |
|
---|
| 714 | Visitor::visit( funcDecl );
|
---|
| 715 | }
|
---|
[70a06f6] | 716 |
|
---|
[630a82a] | 717 | DeclarationWithType * CompoundLiteral::mutate( ObjectDecl *objectDecl ) {
|
---|
| 718 | storageclass = objectDecl->get_storageClass();
|
---|
| 719 | DeclarationWithType * temp = Mutator::mutate( objectDecl );
|
---|
| 720 | storageclass = DeclarationNode::NoStorageClass;
|
---|
| 721 | return temp;
|
---|
| 722 | }
|
---|
| 723 |
|
---|
| 724 | Expression *CompoundLiteral::mutate( CompoundLiteralExpr *compLitExpr ) {
|
---|
| 725 | // transform [storage_class] ... (struct S){ 3, ... };
|
---|
| 726 | // into [storage_class] struct S temp = { 3, ... };
|
---|
| 727 | static UniqueName indexName( "_compLit" );
|
---|
| 728 |
|
---|
| 729 | ObjectDecl *tempvar = new ObjectDecl( indexName.newName(), storageclass, LinkageSpec::C, 0, compLitExpr->get_type(), compLitExpr->get_initializer() );
|
---|
| 730 | compLitExpr->set_type( 0 );
|
---|
| 731 | compLitExpr->set_initializer( 0 );
|
---|
| 732 | delete compLitExpr;
|
---|
| 733 | DeclarationWithType * newtempvar = mutate( tempvar );
|
---|
| 734 | addDeclaration( newtempvar ); // add modified temporary to current block
|
---|
| 735 | return new VariableExpr( newtempvar );
|
---|
| 736 | }
|
---|
[51b73452] | 737 | } // namespace SymTab
|
---|
[0dd3a2f] | 738 |
|
---|
| 739 | // Local Variables: //
|
---|
| 740 | // tab-width: 4 //
|
---|
| 741 | // mode: c++ //
|
---|
| 742 | // compile-command: "make install" //
|
---|
| 743 | // End: //
|
---|