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