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