| [51587aa] | 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 | // | 
|---|
| [ae63a18] | 7 | // Box.cc -- | 
|---|
| [51587aa] | 8 | // | 
|---|
|  | 9 | // Author           : Richard C. Bilson | 
|---|
|  | 10 | // Created On       : Mon May 18 07:44:20 2015 | 
|---|
| [ca35c51] | 11 | // Last Modified By : Peter A. Buhr | 
|---|
|  | 12 | // Last Modified On : Wed Jun 29 21:43:03 2016 | 
|---|
|  | 13 | // Update Count     : 296 | 
|---|
| [51587aa] | 14 | // | 
|---|
| [51b73452] | 15 |  | 
|---|
| [4e284ea6] | 16 | #include <algorithm> | 
|---|
|  | 17 | #include <iterator> | 
|---|
|  | 18 | #include <list> | 
|---|
|  | 19 | #include <map> | 
|---|
| [51b73452] | 20 | #include <set> | 
|---|
| [b1a6d6b] | 21 | #include <stack> | 
|---|
| [51b73452] | 22 | #include <string> | 
|---|
| [4e284ea6] | 23 | #include <utility> | 
|---|
|  | 24 | #include <vector> | 
|---|
| [51b73452] | 25 | #include <cassert> | 
|---|
|  | 26 |  | 
|---|
|  | 27 | #include "Box.h" | 
|---|
| [9d7b3ea] | 28 | #include "DeclMutator.h" | 
|---|
| [51b73452] | 29 | #include "PolyMutator.h" | 
|---|
|  | 30 | #include "FindFunction.h" | 
|---|
| [89173242] | 31 | #include "ScopedSet.h" | 
|---|
| [51b73452] | 32 | #include "ScrubTyVars.h" | 
|---|
|  | 33 |  | 
|---|
| [68cd1ce] | 34 | #include "Parser/ParseNode.h" | 
|---|
|  | 35 |  | 
|---|
| [cdec5af] | 36 | #include "SynTree/Constant.h" | 
|---|
| [4e284ea6] | 37 | #include "SynTree/Declaration.h" | 
|---|
| [51b73452] | 38 | #include "SynTree/Expression.h" | 
|---|
|  | 39 | #include "SynTree/Initializer.h" | 
|---|
|  | 40 | #include "SynTree/Mutator.h" | 
|---|
| [4e284ea6] | 41 | #include "SynTree/Statement.h" | 
|---|
|  | 42 | #include "SynTree/Type.h" | 
|---|
|  | 43 | #include "SynTree/TypeSubstitution.h" | 
|---|
| [68cd1ce] | 44 |  | 
|---|
| [51b73452] | 45 | #include "ResolvExpr/TypeEnvironment.h" | 
|---|
| [dc12481] | 46 | #include "ResolvExpr/TypeMap.h" | 
|---|
|  | 47 | #include "ResolvExpr/typeops.h" | 
|---|
| [68cd1ce] | 48 |  | 
|---|
| [4e284ea6] | 49 | #include "SymTab/Indexer.h" | 
|---|
| [51b73452] | 50 | #include "SymTab/Mangler.h" | 
|---|
|  | 51 |  | 
|---|
| [e491159] | 52 | #include "Common/ScopedMap.h" | 
|---|
| [d3b7937] | 53 | #include "Common/SemanticError.h" | 
|---|
|  | 54 | #include "Common/UniqueName.h" | 
|---|
|  | 55 | #include "Common/utility.h" | 
|---|
| [51b73452] | 56 |  | 
|---|
|  | 57 | #include <ext/functional> // temporary | 
|---|
|  | 58 |  | 
|---|
|  | 59 | namespace GenPoly { | 
|---|
| [01aeade] | 60 | namespace { | 
|---|
|  | 61 | const std::list<Label> noLabels; | 
|---|
|  | 62 |  | 
|---|
| [e56cfdb0] | 63 | FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars ); | 
|---|
|  | 64 |  | 
|---|
| [9d7b3ea] | 65 | /// Adds layout-generation functions to polymorphic types | 
|---|
| [62e5546] | 66 | class LayoutFunctionBuilder final : public DeclMutator { | 
|---|
| [9d7b3ea] | 67 | unsigned int functionNesting;  // current level of nested functions | 
|---|
|  | 68 | public: | 
|---|
|  | 69 | LayoutFunctionBuilder() : functionNesting( 0 ) {} | 
|---|
|  | 70 |  | 
|---|
| [62e5546] | 71 | using DeclMutator::mutate; | 
|---|
|  | 72 | virtual DeclarationWithType *mutate( FunctionDecl *functionDecl ) override; | 
|---|
|  | 73 | virtual Declaration *mutate( StructDecl *structDecl ) override; | 
|---|
|  | 74 | virtual Declaration *mutate( UnionDecl *unionDecl ) override; | 
|---|
| [9d7b3ea] | 75 | }; | 
|---|
| [70a06f6] | 76 |  | 
|---|
| [f8b961b] | 77 | /// Replaces polymorphic return types with out-parameters, replaces calls to polymorphic functions with adapter calls as needed, and adds appropriate type variables to the function call | 
|---|
| [62e5546] | 78 | class Pass1 final : public PolyMutator { | 
|---|
| [01aeade] | 79 | public: | 
|---|
|  | 80 | Pass1(); | 
|---|
| [62e5546] | 81 |  | 
|---|
|  | 82 | using PolyMutator::mutate; | 
|---|
|  | 83 | virtual Expression *mutate( ApplicationExpr *appExpr ) override; | 
|---|
|  | 84 | virtual Expression *mutate( AddressExpr *addrExpr ) override; | 
|---|
|  | 85 | virtual Expression *mutate( UntypedExpr *expr ) override; | 
|---|
|  | 86 | virtual DeclarationWithType* mutate( FunctionDecl *functionDecl ) override; | 
|---|
|  | 87 | virtual TypeDecl *mutate( TypeDecl *typeDecl ) override; | 
|---|
|  | 88 | virtual Expression *mutate( CommaExpr *commaExpr ) override; | 
|---|
|  | 89 | virtual Expression *mutate( ConditionalExpr *condExpr ) override; | 
|---|
|  | 90 | virtual Statement * mutate( ReturnStmt *returnStmt ) override; | 
|---|
|  | 91 | virtual Type *mutate( PointerType *pointerType ) override; | 
|---|
|  | 92 | virtual Type * mutate( FunctionType *functionType ) override; | 
|---|
|  | 93 |  | 
|---|
|  | 94 | virtual void doBeginScope() override; | 
|---|
|  | 95 | virtual void doEndScope() override; | 
|---|
| [01aeade] | 96 | private: | 
|---|
| [5c52b06] | 97 | /// Pass the extra type parameters from polymorphic generic arguments or return types into a function application | 
|---|
|  | 98 | void passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes ); | 
|---|
| [05d47278] | 99 | /// passes extra type parameters into a polymorphic function application | 
|---|
| [d9fa60a] | 100 | void passTypeVars( ApplicationExpr *appExpr, Type *polyRetType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ); | 
|---|
| [48ca586] | 101 | /// wraps a function application with a new temporary for the out-parameter return value | 
|---|
| [01aeade] | 102 | Expression *addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg ); | 
|---|
| [48ca586] | 103 | /// Replaces all the type parameters of a generic type with their concrete equivalents under the current environment | 
|---|
|  | 104 | void replaceParametersWithConcrete( ApplicationExpr *appExpr, std::list< Expression* >& params ); | 
|---|
|  | 105 | /// Replaces a polymorphic type with its concrete equivalant under the current environment (returns itself if concrete). | 
|---|
|  | 106 | /// If `doClone` is set to false, will not clone interior types | 
|---|
|  | 107 | Type *replaceWithConcrete( ApplicationExpr *appExpr, Type *type, bool doClone = true ); | 
|---|
|  | 108 | /// wraps a function application returning a polymorphic type with a new temporary for the out-parameter return value | 
|---|
| [d9fa60a] | 109 | Expression *addDynRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *polyType, std::list< Expression *>::iterator &arg ); | 
|---|
| [01aeade] | 110 | Expression *applyAdapter( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ); | 
|---|
|  | 111 | void boxParam( Type *formal, Expression *&arg, const TyVarMap &exprTyVars ); | 
|---|
|  | 112 | void boxParams( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ); | 
|---|
|  | 113 | void addInferredParams( ApplicationExpr *appExpr, FunctionType *functionType, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ); | 
|---|
| [1194734] | 114 | /// Stores assignment operators from assertion list in local map of assignment operations | 
|---|
| [8c49c0e] | 115 | void findTypeOps( const Type::ForallList &forall ); | 
|---|
| [01aeade] | 116 | void passAdapters( ApplicationExpr *appExpr, FunctionType *functionType, const TyVarMap &exprTyVars ); | 
|---|
|  | 117 | FunctionDecl *makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ); | 
|---|
| [05d47278] | 118 | /// Replaces intrinsic operator functions with their arithmetic desugaring | 
|---|
| [01aeade] | 119 | Expression *handleIntrinsics( ApplicationExpr *appExpr ); | 
|---|
| [05d47278] | 120 | /// Inserts a new temporary variable into the current scope with an auto-generated name | 
|---|
| [01aeade] | 121 | ObjectDecl *makeTemporary( Type *type ); | 
|---|
| [c29d9ce] | 122 |  | 
|---|
| [c2ad3c9] | 123 | ScopedMap< std::string, DeclarationWithType* > assignOps;    ///< Currently known type variable assignment operators | 
|---|
|  | 124 | ScopedMap< std::string, DeclarationWithType* > ctorOps;      ///< Currently known type variable constructors | 
|---|
|  | 125 | ScopedMap< std::string, DeclarationWithType* > copyOps;      ///< Currently known type variable copy constructors | 
|---|
|  | 126 | ScopedMap< std::string, DeclarationWithType* > dtorOps;      ///< Currently known type variable destructors | 
|---|
| [89173242] | 127 | ResolvExpr::TypeMap< DeclarationWithType > scopedAssignOps;  ///< Currently known assignment operators | 
|---|
| [c2ad3c9] | 128 | ResolvExpr::TypeMap< DeclarationWithType > scopedCtorOps;    ///< Currently known assignment operators | 
|---|
|  | 129 | ResolvExpr::TypeMap< DeclarationWithType > scopedCopyOps;    ///< Currently known assignment operators | 
|---|
|  | 130 | ResolvExpr::TypeMap< DeclarationWithType > scopedDtorOps;    ///< Currently known assignment operators | 
|---|
| [89173242] | 131 | ScopedMap< std::string, DeclarationWithType* > adapters;     ///< Set of adapter functions in the current scope | 
|---|
| [70a06f6] | 132 |  | 
|---|
| [01aeade] | 133 | DeclarationWithType *retval; | 
|---|
|  | 134 | bool useRetval; | 
|---|
|  | 135 | UniqueName tempNamer; | 
|---|
|  | 136 | }; | 
|---|
|  | 137 |  | 
|---|
| [89173242] | 138 | /// * Moves polymorphic returns in function types to pointer-type parameters | 
|---|
|  | 139 | /// * adds type size and assertion parameters to parameter lists | 
|---|
| [62e5546] | 140 | class Pass2 final : public PolyMutator { | 
|---|
| [01aeade] | 141 | public: | 
|---|
|  | 142 | template< typename DeclClass > | 
|---|
|  | 143 | DeclClass *handleDecl( DeclClass *decl, Type *type ); | 
|---|
| [62e5546] | 144 |  | 
|---|
|  | 145 | using PolyMutator::mutate; | 
|---|
|  | 146 | virtual DeclarationWithType *mutate( FunctionDecl *functionDecl ) override; | 
|---|
|  | 147 | virtual ObjectDecl *mutate( ObjectDecl *objectDecl ) override; | 
|---|
|  | 148 | virtual TypeDecl *mutate( TypeDecl *typeDecl ) override; | 
|---|
|  | 149 | virtual TypedefDecl *mutate( TypedefDecl *typedefDecl ) override; | 
|---|
|  | 150 | virtual Type *mutate( PointerType *pointerType ) override; | 
|---|
|  | 151 | virtual Type *mutate( FunctionType *funcType ) override; | 
|---|
| [70a06f6] | 152 |  | 
|---|
| [01aeade] | 153 | private: | 
|---|
|  | 154 | void addAdapters( FunctionType *functionType ); | 
|---|
| [ae63a18] | 155 |  | 
|---|
| [01aeade] | 156 | std::map< UniqueId, std::string > adapterName; | 
|---|
|  | 157 | }; | 
|---|
|  | 158 |  | 
|---|
| [8a34677] | 159 | /// Replaces member and size/align/offsetof expressions on polymorphic generic types with calculated expressions. | 
|---|
|  | 160 | /// * Replaces member expressions for polymorphic types with calculated add-field-offset-and-dereference | 
|---|
|  | 161 | /// * Calculates polymorphic offsetof expressions from offset array | 
|---|
|  | 162 | /// * Inserts dynamic calculation of polymorphic type layouts where needed | 
|---|
| [62e5546] | 163 | class PolyGenericCalculator final : public PolyMutator { | 
|---|
| [8a34677] | 164 | public: | 
|---|
| [1ba88a0] | 165 | typedef PolyMutator Parent; | 
|---|
|  | 166 | using Parent::mutate; | 
|---|
|  | 167 |  | 
|---|
| [05d47278] | 168 | template< typename DeclClass > | 
|---|
|  | 169 | DeclClass *handleDecl( DeclClass *decl, Type *type ); | 
|---|
| [62e5546] | 170 | virtual DeclarationWithType *mutate( FunctionDecl *functionDecl ) override; | 
|---|
|  | 171 | virtual ObjectDecl *mutate( ObjectDecl *objectDecl ) override; | 
|---|
|  | 172 | virtual TypedefDecl *mutate( TypedefDecl *objectDecl ) override; | 
|---|
|  | 173 | virtual TypeDecl *mutate( TypeDecl *objectDecl ) override; | 
|---|
|  | 174 | virtual Statement *mutate( DeclStmt *declStmt ) override; | 
|---|
|  | 175 | virtual Type *mutate( PointerType *pointerType ) override; | 
|---|
|  | 176 | virtual Type *mutate( FunctionType *funcType ) override; | 
|---|
|  | 177 | virtual Expression *mutate( MemberExpr *memberExpr ) override; | 
|---|
|  | 178 | virtual Expression *mutate( SizeofExpr *sizeofExpr ) override; | 
|---|
|  | 179 | virtual Expression *mutate( AlignofExpr *alignofExpr ) override; | 
|---|
|  | 180 | virtual Expression *mutate( OffsetofExpr *offsetofExpr ) override; | 
|---|
|  | 181 | virtual Expression *mutate( OffsetPackExpr *offsetPackExpr ) override; | 
|---|
|  | 182 |  | 
|---|
|  | 183 | virtual void doBeginScope() override; | 
|---|
|  | 184 | virtual void doEndScope() override; | 
|---|
| [8a34677] | 185 |  | 
|---|
|  | 186 | private: | 
|---|
|  | 187 | /// Makes a new variable in the current scope with the given name, type & optional initializer | 
|---|
|  | 188 | ObjectDecl *makeVar( const std::string &name, Type *type, Initializer *init = 0 ); | 
|---|
|  | 189 | /// returns true if the type has a dynamic layout; such a layout will be stored in appropriately-named local variables when the function returns | 
|---|
|  | 190 | bool findGeneric( Type *ty ); | 
|---|
|  | 191 | /// adds type parameters to the layout call; will generate the appropriate parameters if needed | 
|---|
|  | 192 | void addOtypeParamsToLayoutCall( UntypedExpr *layoutCall, const std::list< Type* > &otypeParams ); | 
|---|
| [aa19ccf] | 193 |  | 
|---|
|  | 194 | /// Enters a new scope for type-variables, adding the type variables from ty | 
|---|
|  | 195 | void beginTypeScope( Type *ty ); | 
|---|
|  | 196 | /// Exits the type-variable scope | 
|---|
|  | 197 | void endTypeScope(); | 
|---|
| [70a06f6] | 198 |  | 
|---|
| [8a34677] | 199 | ScopedSet< std::string > knownLayouts;          ///< Set of generic type layouts known in the current scope, indexed by sizeofName | 
|---|
|  | 200 | ScopedSet< std::string > knownOffsets;          ///< Set of non-generic types for which the offset array exists in the current scope, indexed by offsetofName | 
|---|
| [05d47278] | 201 | }; | 
|---|
| [b4cd03b7] | 202 |  | 
|---|
| [f8b961b] | 203 | /// Replaces initialization of polymorphic values with alloca, declaration of dtype/ftype with appropriate void expression, and sizeof expressions of polymorphic types with the proper variable | 
|---|
| [62e5546] | 204 | class Pass3 final : public PolyMutator { | 
|---|
| [01aeade] | 205 | public: | 
|---|
|  | 206 | template< typename DeclClass > | 
|---|
|  | 207 | DeclClass *handleDecl( DeclClass *decl, Type *type ); | 
|---|
| [62e5546] | 208 |  | 
|---|
|  | 209 | using PolyMutator::mutate; | 
|---|
|  | 210 | virtual DeclarationWithType *mutate( FunctionDecl *functionDecl ) override; | 
|---|
|  | 211 | virtual ObjectDecl *mutate( ObjectDecl *objectDecl ) override; | 
|---|
|  | 212 | virtual TypedefDecl *mutate( TypedefDecl *objectDecl ) override; | 
|---|
|  | 213 | virtual TypeDecl *mutate( TypeDecl *objectDecl ) override; | 
|---|
|  | 214 | virtual Type *mutate( PointerType *pointerType ) override; | 
|---|
|  | 215 | virtual Type *mutate( FunctionType *funcType ) override; | 
|---|
| [01aeade] | 216 | private: | 
|---|
|  | 217 | }; | 
|---|
|  | 218 |  | 
|---|
|  | 219 | } // anonymous namespace | 
|---|
|  | 220 |  | 
|---|
| [05d47278] | 221 | /// version of mutateAll with special handling for translation unit so you can check the end of the prelude when debugging | 
|---|
|  | 222 | template< typename MutatorType > | 
|---|
|  | 223 | inline void mutateTranslationUnit( std::list< Declaration* > &translationUnit, MutatorType &mutator ) { | 
|---|
|  | 224 | bool seenIntrinsic = false; | 
|---|
|  | 225 | SemanticError errors; | 
|---|
|  | 226 | for ( typename std::list< Declaration* >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) { | 
|---|
|  | 227 | try { | 
|---|
|  | 228 | if ( *i ) { | 
|---|
|  | 229 | if ( (*i)->get_linkage() == LinkageSpec::Intrinsic ) { | 
|---|
|  | 230 | seenIntrinsic = true; | 
|---|
|  | 231 | } else if ( seenIntrinsic ) { | 
|---|
|  | 232 | seenIntrinsic = false; // break on this line when debugging for end of prelude | 
|---|
|  | 233 | } | 
|---|
| [b4cd03b7] | 234 |  | 
|---|
| [05d47278] | 235 | *i = dynamic_cast< Declaration* >( (*i)->acceptMutator( mutator ) ); | 
|---|
|  | 236 | assert( *i ); | 
|---|
|  | 237 | } // if | 
|---|
|  | 238 | } catch( SemanticError &e ) { | 
|---|
|  | 239 | errors.append( e ); | 
|---|
|  | 240 | } // try | 
|---|
|  | 241 | } // for | 
|---|
|  | 242 | if ( ! errors.isEmpty() ) { | 
|---|
|  | 243 | throw errors; | 
|---|
|  | 244 | } // if | 
|---|
|  | 245 | } | 
|---|
|  | 246 |  | 
|---|
| [01aeade] | 247 | void box( std::list< Declaration *>& translationUnit ) { | 
|---|
| [9d7b3ea] | 248 | LayoutFunctionBuilder layoutBuilder; | 
|---|
| [01aeade] | 249 | Pass1 pass1; | 
|---|
|  | 250 | Pass2 pass2; | 
|---|
| [8a34677] | 251 | PolyGenericCalculator polyCalculator; | 
|---|
| [01aeade] | 252 | Pass3 pass3; | 
|---|
| [70a06f6] | 253 |  | 
|---|
| [9d7b3ea] | 254 | layoutBuilder.mutateDeclarationList( translationUnit ); | 
|---|
| [05d47278] | 255 | mutateTranslationUnit/*All*/( translationUnit, pass1 ); | 
|---|
|  | 256 | mutateTranslationUnit/*All*/( translationUnit, pass2 ); | 
|---|
| [8a34677] | 257 | mutateTranslationUnit/*All*/( translationUnit, polyCalculator ); | 
|---|
| [05d47278] | 258 | mutateTranslationUnit/*All*/( translationUnit, pass3 ); | 
|---|
| [6c3744e] | 259 | } | 
|---|
|  | 260 |  | 
|---|
| [9d7b3ea] | 261 | ////////////////////////////////// LayoutFunctionBuilder //////////////////////////////////////////// | 
|---|
|  | 262 |  | 
|---|
|  | 263 | DeclarationWithType *LayoutFunctionBuilder::mutate( FunctionDecl *functionDecl ) { | 
|---|
|  | 264 | functionDecl->set_functionType( maybeMutate( functionDecl->get_functionType(), *this ) ); | 
|---|
|  | 265 | mutateAll( functionDecl->get_oldDecls(), *this ); | 
|---|
|  | 266 | ++functionNesting; | 
|---|
|  | 267 | functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) ); | 
|---|
|  | 268 | --functionNesting; | 
|---|
|  | 269 | return functionDecl; | 
|---|
|  | 270 | } | 
|---|
| [70a06f6] | 271 |  | 
|---|
| [9d7b3ea] | 272 | /// Get a list of type declarations that will affect a layout function | 
|---|
|  | 273 | std::list< TypeDecl* > takeOtypeOnly( std::list< TypeDecl* > &decls ) { | 
|---|
|  | 274 | std::list< TypeDecl * > otypeDecls; | 
|---|
|  | 275 |  | 
|---|
|  | 276 | for ( std::list< TypeDecl* >::const_iterator decl = decls.begin(); decl != decls.end(); ++decl ) { | 
|---|
| [2c57025] | 277 | if ( (*decl)->isComplete() ) { | 
|---|
| [9d7b3ea] | 278 | otypeDecls.push_back( *decl ); | 
|---|
|  | 279 | } | 
|---|
|  | 280 | } | 
|---|
| [70a06f6] | 281 |  | 
|---|
| [9d7b3ea] | 282 | return otypeDecls; | 
|---|
|  | 283 | } | 
|---|
|  | 284 |  | 
|---|
|  | 285 | /// Adds parameters for otype layout to a function type | 
|---|
|  | 286 | void addOtypeParams( FunctionType *layoutFnType, std::list< TypeDecl* > &otypeParams ) { | 
|---|
|  | 287 | BasicType sizeAlignType( Type::Qualifiers(), BasicType::LongUnsignedInt ); | 
|---|
| [70a06f6] | 288 |  | 
|---|
| [9d7b3ea] | 289 | for ( std::list< TypeDecl* >::const_iterator param = otypeParams.begin(); param != otypeParams.end(); ++param ) { | 
|---|
| [2e60a1a] | 290 | TypeInstType paramType( Type::Qualifiers(), (*param)->get_name(), *param ); | 
|---|
| [adc6781] | 291 | std::string paramName = mangleType( ¶mType ); | 
|---|
|  | 292 | layoutFnType->get_parameters().push_back( new ObjectDecl( sizeofName( paramName ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) ); | 
|---|
|  | 293 | layoutFnType->get_parameters().push_back( new ObjectDecl( alignofName( paramName ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) ); | 
|---|
| [9d7b3ea] | 294 | } | 
|---|
|  | 295 | } | 
|---|
|  | 296 |  | 
|---|
|  | 297 | /// Builds a layout function declaration | 
|---|
| [adc6781] | 298 | FunctionDecl *buildLayoutFunctionDecl( AggregateDecl *typeDecl, unsigned int functionNesting, FunctionType *layoutFnType ) { | 
|---|
| [9d7b3ea] | 299 | // Routines at global scope marked "static" to prevent multiple definitions is separate translation units | 
|---|
|  | 300 | // because each unit generates copies of the default routines for each aggregate. | 
|---|
|  | 301 | FunctionDecl *layoutDecl = new FunctionDecl( | 
|---|
| [adc6781] | 302 | layoutofName( typeDecl ), functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, layoutFnType, new CompoundStmt( noLabels ), true, false ); | 
|---|
| [9d7b3ea] | 303 | layoutDecl->fixUniqueId(); | 
|---|
|  | 304 | return layoutDecl; | 
|---|
|  | 305 | } | 
|---|
|  | 306 |  | 
|---|
|  | 307 | /// Makes a unary operation | 
|---|
|  | 308 | Expression *makeOp( const std::string &name, Expression *arg ) { | 
|---|
|  | 309 | UntypedExpr *expr = new UntypedExpr( new NameExpr( name ) ); | 
|---|
|  | 310 | expr->get_args().push_back( arg ); | 
|---|
|  | 311 | return expr; | 
|---|
|  | 312 | } | 
|---|
|  | 313 |  | 
|---|
|  | 314 | /// Makes a binary operation | 
|---|
|  | 315 | Expression *makeOp( const std::string &name, Expression *lhs, Expression *rhs ) { | 
|---|
|  | 316 | UntypedExpr *expr = new UntypedExpr( new NameExpr( name ) ); | 
|---|
|  | 317 | expr->get_args().push_back( lhs ); | 
|---|
|  | 318 | expr->get_args().push_back( rhs ); | 
|---|
|  | 319 | return expr; | 
|---|
|  | 320 | } | 
|---|
|  | 321 |  | 
|---|
|  | 322 | /// Returns the dereference of a local pointer variable | 
|---|
|  | 323 | Expression *derefVar( ObjectDecl *var ) { | 
|---|
|  | 324 | return makeOp( "*?", new VariableExpr( var ) ); | 
|---|
|  | 325 | } | 
|---|
|  | 326 |  | 
|---|
|  | 327 | /// makes an if-statement with a single-expression if-block and no then block | 
|---|
|  | 328 | Statement *makeCond( Expression *cond, Expression *ifPart ) { | 
|---|
| [3627356] | 329 | return new IfStmt( noLabels, cond, new ExprStmt( noLabels, ifPart ), 0 ); | 
|---|
| [9d7b3ea] | 330 | } | 
|---|
|  | 331 |  | 
|---|
|  | 332 | /// makes a statement that assigns rhs to lhs if lhs < rhs | 
|---|
|  | 333 | Statement *makeAssignMax( Expression *lhs, Expression *rhs ) { | 
|---|
|  | 334 | return makeCond( makeOp( "?<?", lhs, rhs ), makeOp( "?=?", lhs->clone(), rhs->clone() ) ); | 
|---|
|  | 335 | } | 
|---|
|  | 336 |  | 
|---|
|  | 337 | /// makes a statement that aligns lhs to rhs (rhs should be an integer power of two) | 
|---|
|  | 338 | Statement *makeAlignTo( Expression *lhs, Expression *rhs ) { | 
|---|
|  | 339 | // check that the lhs is zeroed out to the level of rhs | 
|---|
|  | 340 | Expression *ifCond = makeOp( "?&?", lhs, makeOp( "?-?", rhs, new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), "1" ) ) ) ); | 
|---|
|  | 341 | // if not aligned, increment to alignment | 
|---|
|  | 342 | Expression *ifExpr = makeOp( "?+=?", lhs->clone(), makeOp( "?-?", rhs->clone(), ifCond->clone() ) ); | 
|---|
|  | 343 | return makeCond( ifCond, ifExpr ); | 
|---|
|  | 344 | } | 
|---|
| [70a06f6] | 345 |  | 
|---|
| [9d7b3ea] | 346 | /// adds an expression to a compound statement | 
|---|
|  | 347 | void addExpr( CompoundStmt *stmts, Expression *expr ) { | 
|---|
|  | 348 | stmts->get_kids().push_back( new ExprStmt( noLabels, expr ) ); | 
|---|
|  | 349 | } | 
|---|
|  | 350 |  | 
|---|
|  | 351 | /// adds a statement to a compound statement | 
|---|
|  | 352 | void addStmt( CompoundStmt *stmts, Statement *stmt ) { | 
|---|
|  | 353 | stmts->get_kids().push_back( stmt ); | 
|---|
|  | 354 | } | 
|---|
| [70a06f6] | 355 |  | 
|---|
| [3627356] | 356 | Declaration *LayoutFunctionBuilder::mutate( StructDecl *structDecl ) { | 
|---|
| [9d7b3ea] | 357 | // do not generate layout function for "empty" tag structs | 
|---|
|  | 358 | if ( structDecl->get_members().empty() ) return structDecl; | 
|---|
|  | 359 |  | 
|---|
|  | 360 | // get parameters that can change layout, exiting early if none | 
|---|
|  | 361 | std::list< TypeDecl* > otypeParams = takeOtypeOnly( structDecl->get_parameters() ); | 
|---|
|  | 362 | if ( otypeParams.empty() ) return structDecl; | 
|---|
|  | 363 |  | 
|---|
|  | 364 | // build layout function signature | 
|---|
|  | 365 | FunctionType *layoutFnType = new FunctionType( Type::Qualifiers(), false ); | 
|---|
|  | 366 | BasicType *sizeAlignType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); | 
|---|
|  | 367 | PointerType *sizeAlignOutType = new PointerType( Type::Qualifiers(), sizeAlignType ); | 
|---|
| [70a06f6] | 368 |  | 
|---|
| [adc6781] | 369 | ObjectDecl *sizeParam = new ObjectDecl( sizeofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 ); | 
|---|
| [9d7b3ea] | 370 | layoutFnType->get_parameters().push_back( sizeParam ); | 
|---|
| [adc6781] | 371 | ObjectDecl *alignParam = new ObjectDecl( alignofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 ); | 
|---|
| [9d7b3ea] | 372 | layoutFnType->get_parameters().push_back( alignParam ); | 
|---|
| [adc6781] | 373 | ObjectDecl *offsetParam = new ObjectDecl( offsetofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 ); | 
|---|
| [9d7b3ea] | 374 | layoutFnType->get_parameters().push_back( offsetParam ); | 
|---|
|  | 375 | addOtypeParams( layoutFnType, otypeParams ); | 
|---|
|  | 376 |  | 
|---|
|  | 377 | // build function decl | 
|---|
| [adc6781] | 378 | FunctionDecl *layoutDecl = buildLayoutFunctionDecl( structDecl, functionNesting, layoutFnType ); | 
|---|
| [9d7b3ea] | 379 |  | 
|---|
|  | 380 | // calculate struct layout in function body | 
|---|
|  | 381 |  | 
|---|
|  | 382 | // initialize size and alignment to 0 and 1 (will have at least one member to re-edit size | 
|---|
|  | 383 | addExpr( layoutDecl->get_statements(), makeOp( "?=?", derefVar( sizeParam ), new ConstantExpr( Constant( sizeAlignType->clone(), "0" ) ) ) ); | 
|---|
|  | 384 | addExpr( layoutDecl->get_statements(), makeOp( "?=?", derefVar( alignParam ), new ConstantExpr( Constant( sizeAlignType->clone(), "1" ) ) ) ); | 
|---|
|  | 385 | unsigned long n_members = 0; | 
|---|
|  | 386 | bool firstMember = true; | 
|---|
|  | 387 | for ( std::list< Declaration* >::const_iterator member = structDecl->get_members().begin(); member != structDecl->get_members().end(); ++member ) { | 
|---|
|  | 388 | DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *member ); | 
|---|
|  | 389 | assert( dwt ); | 
|---|
| [bd91e2a] | 390 | Type *memberType = dwt->get_type(); | 
|---|
| [9d7b3ea] | 391 |  | 
|---|
|  | 392 | if ( firstMember ) { | 
|---|
|  | 393 | firstMember = false; | 
|---|
|  | 394 | } else { | 
|---|
|  | 395 | // make sure all members after the first (automatically aligned at 0) are properly padded for alignment | 
|---|
| [bd91e2a] | 396 | addStmt( layoutDecl->get_statements(), makeAlignTo( derefVar( sizeParam ), new AlignofExpr( memberType->clone() ) ) ); | 
|---|
| [9d7b3ea] | 397 | } | 
|---|
| [70a06f6] | 398 |  | 
|---|
| [9d7b3ea] | 399 | // place current size in the current offset index | 
|---|
| [cb4c607] | 400 | addExpr( layoutDecl->get_statements(), makeOp( "?=?", makeOp( "?[?]", new VariableExpr( offsetParam ), new ConstantExpr( Constant::from_ulong( n_members ) ) ), | 
|---|
| [9d7b3ea] | 401 | derefVar( sizeParam ) ) ); | 
|---|
|  | 402 | ++n_members; | 
|---|
|  | 403 |  | 
|---|
|  | 404 | // add member size to current size | 
|---|
| [bd91e2a] | 405 | addExpr( layoutDecl->get_statements(), makeOp( "?+=?", derefVar( sizeParam ), new SizeofExpr( memberType->clone() ) ) ); | 
|---|
| [70a06f6] | 406 |  | 
|---|
| [9d7b3ea] | 407 | // take max of member alignment and global alignment | 
|---|
| [bd91e2a] | 408 | addStmt( layoutDecl->get_statements(), makeAssignMax( derefVar( alignParam ), new AlignofExpr( memberType->clone() ) ) ); | 
|---|
| [9d7b3ea] | 409 | } | 
|---|
|  | 410 | // make sure the type is end-padded to a multiple of its alignment | 
|---|
|  | 411 | addStmt( layoutDecl->get_statements(), makeAlignTo( derefVar( sizeParam ), derefVar( alignParam ) ) ); | 
|---|
|  | 412 |  | 
|---|
|  | 413 | addDeclarationAfter( layoutDecl ); | 
|---|
|  | 414 | return structDecl; | 
|---|
|  | 415 | } | 
|---|
| [70a06f6] | 416 |  | 
|---|
| [3627356] | 417 | Declaration *LayoutFunctionBuilder::mutate( UnionDecl *unionDecl ) { | 
|---|
| [9d7b3ea] | 418 | // do not generate layout function for "empty" tag unions | 
|---|
|  | 419 | if ( unionDecl->get_members().empty() ) return unionDecl; | 
|---|
| [70a06f6] | 420 |  | 
|---|
| [9d7b3ea] | 421 | // get parameters that can change layout, exiting early if none | 
|---|
|  | 422 | std::list< TypeDecl* > otypeParams = takeOtypeOnly( unionDecl->get_parameters() ); | 
|---|
|  | 423 | if ( otypeParams.empty() ) return unionDecl; | 
|---|
|  | 424 |  | 
|---|
|  | 425 | // build layout function signature | 
|---|
|  | 426 | FunctionType *layoutFnType = new FunctionType( Type::Qualifiers(), false ); | 
|---|
|  | 427 | BasicType *sizeAlignType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); | 
|---|
|  | 428 | PointerType *sizeAlignOutType = new PointerType( Type::Qualifiers(), sizeAlignType ); | 
|---|
| [70a06f6] | 429 |  | 
|---|
| [adc6781] | 430 | ObjectDecl *sizeParam = new ObjectDecl( sizeofName( unionDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 ); | 
|---|
| [9d7b3ea] | 431 | layoutFnType->get_parameters().push_back( sizeParam ); | 
|---|
| [adc6781] | 432 | ObjectDecl *alignParam = new ObjectDecl( alignofName( unionDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 ); | 
|---|
| [9d7b3ea] | 433 | layoutFnType->get_parameters().push_back( alignParam ); | 
|---|
|  | 434 | addOtypeParams( layoutFnType, otypeParams ); | 
|---|
|  | 435 |  | 
|---|
|  | 436 | // build function decl | 
|---|
| [adc6781] | 437 | FunctionDecl *layoutDecl = buildLayoutFunctionDecl( unionDecl, functionNesting, layoutFnType ); | 
|---|
| [9d7b3ea] | 438 |  | 
|---|
|  | 439 | // calculate union layout in function body | 
|---|
|  | 440 | addExpr( layoutDecl->get_statements(), makeOp( "?=?", derefVar( sizeParam ), new ConstantExpr( Constant( sizeAlignType->clone(), "1" ) ) ) ); | 
|---|
|  | 441 | addExpr( layoutDecl->get_statements(), makeOp( "?=?", derefVar( alignParam ), new ConstantExpr( Constant( sizeAlignType->clone(), "1" ) ) ) ); | 
|---|
|  | 442 | for ( std::list< Declaration* >::const_iterator member = unionDecl->get_members().begin(); member != unionDecl->get_members().end(); ++member ) { | 
|---|
|  | 443 | DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *member ); | 
|---|
|  | 444 | assert( dwt ); | 
|---|
| [bd91e2a] | 445 | Type *memberType = dwt->get_type(); | 
|---|
| [70a06f6] | 446 |  | 
|---|
| [9d7b3ea] | 447 | // take max member size and global size | 
|---|
| [bd91e2a] | 448 | addStmt( layoutDecl->get_statements(), makeAssignMax( derefVar( sizeParam ), new SizeofExpr( memberType->clone() ) ) ); | 
|---|
| [70a06f6] | 449 |  | 
|---|
| [9d7b3ea] | 450 | // take max of member alignment and global alignment | 
|---|
| [bd91e2a] | 451 | addStmt( layoutDecl->get_statements(), makeAssignMax( derefVar( alignParam ), new AlignofExpr( memberType->clone() ) ) ); | 
|---|
| [9d7b3ea] | 452 | } | 
|---|
|  | 453 | // make sure the type is end-padded to a multiple of its alignment | 
|---|
|  | 454 | addStmt( layoutDecl->get_statements(), makeAlignTo( derefVar( sizeParam ), derefVar( alignParam ) ) ); | 
|---|
|  | 455 |  | 
|---|
|  | 456 | addDeclarationAfter( layoutDecl ); | 
|---|
|  | 457 | return unionDecl; | 
|---|
|  | 458 | } | 
|---|
| [70a06f6] | 459 |  | 
|---|
| [01aeade] | 460 | ////////////////////////////////////////// Pass1 //////////////////////////////////////////////////// | 
|---|
|  | 461 |  | 
|---|
|  | 462 | namespace { | 
|---|
| [bdf1954] | 463 | std::string makePolyMonoSuffix( FunctionType * function, const TyVarMap &tyVars ) { | 
|---|
|  | 464 | std::stringstream name; | 
|---|
|  | 465 |  | 
|---|
| [ed1065c] | 466 | // NOTE: this function previously used isPolyObj, which failed to produce | 
|---|
|  | 467 | // the correct thing in some situations. It's not clear to me why this wasn't working. | 
|---|
|  | 468 |  | 
|---|
| [ae63a18] | 469 | // if the return type or a parameter type involved polymorphic types, then the adapter will need | 
|---|
|  | 470 | // to take those polymorphic types as pointers. Therefore, there can be two different functions | 
|---|
| [bdf1954] | 471 | // with the same mangled name, so we need to further mangle the names. | 
|---|
| [ed1065c] | 472 | for ( std::list< DeclarationWithType *>::iterator retval = function->get_returnVals().begin(); retval != function->get_returnVals().end(); ++retval ) { | 
|---|
| [ffad73a] | 473 | if ( isPolyType( (*retval)->get_type(), tyVars ) ) { | 
|---|
| [ed1065c] | 474 | name << "P"; | 
|---|
|  | 475 | } else { | 
|---|
|  | 476 | name << "M"; | 
|---|
|  | 477 | } | 
|---|
| [bdf1954] | 478 | } | 
|---|
|  | 479 | name << "_"; | 
|---|
|  | 480 | std::list< DeclarationWithType *> ¶mList = function->get_parameters(); | 
|---|
|  | 481 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) { | 
|---|
| [ffad73a] | 482 | if ( isPolyType( (*arg)->get_type(), tyVars ) ) { | 
|---|
| [bdf1954] | 483 | name << "P"; | 
|---|
|  | 484 | } else { | 
|---|
| [ae63a18] | 485 | name << "M"; | 
|---|
| [bdf1954] | 486 | } | 
|---|
|  | 487 | } // for | 
|---|
|  | 488 | return name.str(); | 
|---|
|  | 489 | } | 
|---|
|  | 490 |  | 
|---|
|  | 491 | std::string mangleAdapterName( FunctionType * function, const TyVarMap &tyVars ) { | 
|---|
|  | 492 | return SymTab::Mangler::mangle( function ) + makePolyMonoSuffix( function, tyVars ); | 
|---|
|  | 493 | } | 
|---|
|  | 494 |  | 
|---|
| [01aeade] | 495 | std::string makeAdapterName( const std::string &mangleName ) { | 
|---|
|  | 496 | return "_adapter" + mangleName; | 
|---|
|  | 497 | } | 
|---|
| [6c3744e] | 498 |  | 
|---|
| [6635c74] | 499 | Pass1::Pass1() : useRetval( false ), tempNamer( "_temp" ) {} | 
|---|
| [01aeade] | 500 |  | 
|---|
| [c2ad3c9] | 501 | /// Returns T if the given declaration is a function with parameter (T*) for some TypeInstType T, NULL otherwise | 
|---|
|  | 502 | TypeInstType *isTypeInstPtrFn( DeclarationWithType *decl ) { | 
|---|
|  | 503 | if ( FunctionType *funType = getFunctionType( decl->get_type() ) ) { | 
|---|
|  | 504 | if ( funType->get_parameters().size() == 1 ) { | 
|---|
|  | 505 | if ( PointerType *pointer = dynamic_cast< PointerType *>( funType->get_parameters().front()->get_type() ) ) { | 
|---|
|  | 506 | if ( TypeInstType *refType = dynamic_cast< TypeInstType *>( pointer->get_base() ) ) { | 
|---|
|  | 507 | return refType; | 
|---|
|  | 508 | } // if | 
|---|
|  | 509 | } // if | 
|---|
|  | 510 | } // if | 
|---|
|  | 511 | } // if | 
|---|
|  | 512 | return 0; | 
|---|
|  | 513 | } | 
|---|
| [9799ec8] | 514 |  | 
|---|
| [c2ad3c9] | 515 | /// Returns T if the given declaration is a function with parameters (T*, T) for some TypeInstType T, NULL otherwise | 
|---|
|  | 516 | TypeInstType *isTypeInstPtrValFn( DeclarationWithType *decl ) { | 
|---|
|  | 517 | if ( FunctionType *funType = getFunctionType( decl->get_type() ) ) { | 
|---|
|  | 518 | if ( funType->get_parameters().size() == 2 ) { | 
|---|
|  | 519 | if ( PointerType *pointer = dynamic_cast< PointerType *>( funType->get_parameters().front()->get_type() ) ) { | 
|---|
|  | 520 | if ( TypeInstType *refType = dynamic_cast< TypeInstType *>( pointer->get_base() ) ) { | 
|---|
|  | 521 | if ( TypeInstType *refType2 = dynamic_cast< TypeInstType *>( funType->get_parameters().back()->get_type() ) ) { | 
|---|
|  | 522 | if ( refType->get_name() == refType2->get_name() ) { | 
|---|
|  | 523 | return refType; | 
|---|
| [01aeade] | 524 | } // if | 
|---|
|  | 525 | } // if | 
|---|
|  | 526 | } // if | 
|---|
|  | 527 | } // if | 
|---|
|  | 528 | } // if | 
|---|
|  | 529 | } // if | 
|---|
| [1194734] | 530 | return 0; | 
|---|
| [01aeade] | 531 | } | 
|---|
| [9799ec8] | 532 |  | 
|---|
| [c2ad3c9] | 533 | /// Returns T if the given declaration is (*?=?)(T *, T) for some TypeInstType T (return not checked, but maybe should be), NULL otherwise | 
|---|
|  | 534 | TypeInstType *isTypeInstAssignment( DeclarationWithType *decl ) { | 
|---|
|  | 535 | return decl->get_name() == "?=?" ? isTypeInstPtrValFn( decl ) : 0; | 
|---|
|  | 536 | } | 
|---|
| [70a06f6] | 537 |  | 
|---|
| [c2ad3c9] | 538 | /// Returns T if the given declaration is (*?{})(T *) for some TypeInstType T (return not checked, but maybe should be), NULL otherwise | 
|---|
|  | 539 | TypeInstType *isTypeInstCtor( DeclarationWithType *decl ) { | 
|---|
|  | 540 | return decl->get_name() == "?{}" ? isTypeInstPtrFn( decl ) : 0; | 
|---|
|  | 541 | } | 
|---|
|  | 542 |  | 
|---|
|  | 543 | /// Returns T if the given declaration is (*?{})(T *, T) for some TypeInstType T (return not checked, but maybe should be), NULL otherwise | 
|---|
|  | 544 | TypeInstType *isTypeInstCopy( DeclarationWithType *decl ) { | 
|---|
|  | 545 | return decl->get_name() == "?{}" ? isTypeInstPtrValFn( decl ) : 0; | 
|---|
|  | 546 | } | 
|---|
|  | 547 |  | 
|---|
|  | 548 | /// Returns T if the given declaration is (*^?{})(T *) for some TypeInstType T (return not checked, but maybe should be), NULL otherwise | 
|---|
|  | 549 | TypeInstType *isTypeInstDtor( DeclarationWithType *decl ) { | 
|---|
|  | 550 | return decl->get_name() == "^?{}" ? isTypeInstPtrFn( decl ) : 0; | 
|---|
|  | 551 | } | 
|---|
|  | 552 |  | 
|---|
|  | 553 | /// Returns T if the given declaration is a function with parameters (T*, T) for some type T, where neither parameter is cv-qualified, | 
|---|
|  | 554 | /// NULL otherwise | 
|---|
|  | 555 | Type *isNoCvPtrFn( DeclarationWithType *decl ) { | 
|---|
|  | 556 | if ( FunctionType *funType = getFunctionType( decl->get_type() ) ) { | 
|---|
|  | 557 | if ( funType->get_parameters().size() == 1 ) { | 
|---|
|  | 558 | Type::Qualifiers defaultQualifiers; | 
|---|
|  | 559 | Type *paramType = funType->get_parameters().front()->get_type(); | 
|---|
|  | 560 | if ( paramType->get_qualifiers() != defaultQualifiers ) return 0; | 
|---|
|  | 561 |  | 
|---|
|  | 562 | if ( PointerType *pointerType = dynamic_cast< PointerType* >( paramType ) ) { | 
|---|
|  | 563 | Type *baseType = pointerType->get_base(); | 
|---|
|  | 564 | if ( baseType->get_qualifiers() == defaultQualifiers ) { | 
|---|
|  | 565 | return baseType; | 
|---|
|  | 566 | } // if | 
|---|
|  | 567 | } // if | 
|---|
|  | 568 | } // if | 
|---|
|  | 569 | } // if | 
|---|
|  | 570 | return 0; | 
|---|
|  | 571 | } | 
|---|
| [9799ec8] | 572 |  | 
|---|
| [c2ad3c9] | 573 | /// Returns T if the given declaration is a function with parameters (T*, T) for some type T, where neither parameter is cv-qualified, | 
|---|
|  | 574 | /// NULL otherwise | 
|---|
|  | 575 | Type *isNoCvPtrValFn( DeclarationWithType *decl ) { | 
|---|
|  | 576 | if ( FunctionType *funType = getFunctionType( decl->get_type() ) ) { | 
|---|
|  | 577 | if ( funType->get_parameters().size() == 2 ) { | 
|---|
|  | 578 | Type::Qualifiers defaultQualifiers; | 
|---|
|  | 579 | Type *paramType1 = funType->get_parameters().front()->get_type(); | 
|---|
|  | 580 | if ( paramType1->get_qualifiers() != defaultQualifiers ) return 0; | 
|---|
|  | 581 | Type *paramType2 = funType->get_parameters().back()->get_type(); | 
|---|
|  | 582 | if ( paramType2->get_qualifiers() != defaultQualifiers ) return 0; | 
|---|
|  | 583 |  | 
|---|
|  | 584 | if ( PointerType *pointerType = dynamic_cast< PointerType* >( paramType1 ) ) { | 
|---|
|  | 585 | Type *baseType1 = pointerType->get_base(); | 
|---|
|  | 586 | if ( baseType1->get_qualifiers() != defaultQualifiers ) return 0; | 
|---|
|  | 587 | SymTab::Indexer dummy; | 
|---|
|  | 588 | if ( ResolvExpr::typesCompatible( baseType1, paramType2, dummy ) ) { | 
|---|
|  | 589 | return baseType1; | 
|---|
| [dc12481] | 590 | } // if | 
|---|
|  | 591 | } // if | 
|---|
|  | 592 | } // if | 
|---|
|  | 593 | } // if | 
|---|
|  | 594 | return 0; | 
|---|
|  | 595 | } | 
|---|
| [01aeade] | 596 |  | 
|---|
| [c2ad3c9] | 597 | /// returns T if the given declaration is: (*?=?)(T *, T) for some type T (return not checked, but maybe should be), NULL otherwise | 
|---|
|  | 598 | /// Only picks assignments where neither parameter is cv-qualified | 
|---|
|  | 599 | Type *isAssignment( DeclarationWithType *decl ) { | 
|---|
|  | 600 | return decl->get_name() == "?=?" ? isNoCvPtrValFn( decl ) : 0; | 
|---|
|  | 601 | } | 
|---|
|  | 602 |  | 
|---|
|  | 603 | /// returns T if the given declaration is: (*?{})(T *) for some type T, NULL otherwise | 
|---|
|  | 604 | /// Only picks ctors where the parameter is not cv-qualified | 
|---|
|  | 605 | Type *isCtor( DeclarationWithType *decl ) { | 
|---|
|  | 606 | return decl->get_name() == "?{}" ? isNoCvPtrFn( decl ) : 0; | 
|---|
|  | 607 | } | 
|---|
|  | 608 |  | 
|---|
|  | 609 | /// returns T if the given declaration is: (*?{})(T *, T) for some type T (return not checked, but maybe should be), NULL otherwise | 
|---|
|  | 610 | /// Only picks copy constructors where neither parameter is cv-qualified | 
|---|
|  | 611 | Type *isCopy( DeclarationWithType *decl ) { | 
|---|
|  | 612 | return decl->get_name() == "?{}" ? isNoCvPtrValFn( decl ) : 0; | 
|---|
|  | 613 | } | 
|---|
|  | 614 |  | 
|---|
|  | 615 | /// returns T if the given declaration is: (*?{})(T *) for some type T, NULL otherwise | 
|---|
|  | 616 | /// Only picks ctors where the parameter is not cv-qualified | 
|---|
|  | 617 | Type *isDtor( DeclarationWithType *decl ) { | 
|---|
|  | 618 | return decl->get_name() == "^?{}" ? isNoCvPtrFn( decl ) : 0; | 
|---|
|  | 619 | } | 
|---|
|  | 620 |  | 
|---|
| [8c49c0e] | 621 | void Pass1::findTypeOps( const Type::ForallList &forall ) { | 
|---|
| [ed1065c] | 622 | // what if a nested function uses an assignment operator? | 
|---|
|  | 623 | // assignOps.clear(); | 
|---|
| [8c49c0e] | 624 | for ( Type::ForallList::const_iterator i = forall.begin(); i != forall.end(); ++i ) { | 
|---|
| [01aeade] | 625 | for ( std::list< DeclarationWithType *>::const_iterator assert = (*i)->get_assertions().begin(); assert != (*i)->get_assertions().end(); ++assert ) { | 
|---|
|  | 626 | std::string typeName; | 
|---|
| [dc12481] | 627 | if ( TypeInstType *typeInst = isTypeInstAssignment( *assert ) ) { | 
|---|
| [1194734] | 628 | assignOps[ typeInst->get_name() ] = *assert; | 
|---|
| [c2ad3c9] | 629 | } else if ( TypeInstType *typeInst = isTypeInstCtor( *assert ) ) { | 
|---|
|  | 630 | ctorOps[ typeInst->get_name() ] = *assert; | 
|---|
|  | 631 | } else if ( TypeInstType *typeInst = isTypeInstCopy( *assert ) ) { | 
|---|
|  | 632 | copyOps[ typeInst->get_name() ] = *assert; | 
|---|
|  | 633 | } else if ( TypeInstType *typeInst = isTypeInstDtor( *assert ) ) { | 
|---|
|  | 634 | dtorOps[ typeInst->get_name() ] = *assert; | 
|---|
| [01aeade] | 635 | } // if | 
|---|
|  | 636 | } // for | 
|---|
|  | 637 | } // for | 
|---|
|  | 638 | } | 
|---|
|  | 639 |  | 
|---|
| [82dd287] | 640 | DeclarationWithType *Pass1::mutate( FunctionDecl *functionDecl ) { | 
|---|
| [89173242] | 641 | // if this is a assignment function, put it in the map for this scope | 
|---|
| [c2ad3c9] | 642 | if ( Type *paramType = isAssignment( functionDecl ) ) { | 
|---|
|  | 643 | if ( ! dynamic_cast< TypeInstType* >( paramType ) ) { | 
|---|
|  | 644 | scopedAssignOps.insert( paramType, functionDecl ); | 
|---|
|  | 645 | } | 
|---|
|  | 646 | } else if ( Type *paramType = isCtor( functionDecl ) ) { | 
|---|
|  | 647 | if ( ! dynamic_cast< TypeInstType* >( paramType ) ) { | 
|---|
|  | 648 | scopedCtorOps.insert( paramType, functionDecl ); | 
|---|
|  | 649 | } | 
|---|
|  | 650 | } else if ( Type *paramType = isCopy( functionDecl ) ) { | 
|---|
|  | 651 | if ( ! dynamic_cast< TypeInstType* >( paramType ) ) { | 
|---|
|  | 652 | scopedCopyOps.insert( paramType, functionDecl ); | 
|---|
|  | 653 | } | 
|---|
|  | 654 | } else if ( Type *paramType = isDtor( functionDecl ) ) { | 
|---|
|  | 655 | if ( ! dynamic_cast< TypeInstType* >( paramType ) ) { | 
|---|
|  | 656 | scopedDtorOps.insert( paramType, functionDecl ); | 
|---|
| [1194734] | 657 | } | 
|---|
|  | 658 | } | 
|---|
| [b4cd03b7] | 659 |  | 
|---|
| [e56cfdb0] | 660 | if ( functionDecl->get_statements() ) {         // empty routine body ? | 
|---|
|  | 661 | doBeginScope(); | 
|---|
| [4b8f918] | 662 | scopeTyVars.beginScope(); | 
|---|
| [63c0dbf] | 663 | assignOps.beginScope(); | 
|---|
| [c2ad3c9] | 664 | ctorOps.beginScope(); | 
|---|
|  | 665 | copyOps.beginScope(); | 
|---|
|  | 666 | dtorOps.beginScope(); | 
|---|
| [9799ec8] | 667 |  | 
|---|
| [01aeade] | 668 | DeclarationWithType *oldRetval = retval; | 
|---|
|  | 669 | bool oldUseRetval = useRetval; | 
|---|
| [e56cfdb0] | 670 |  | 
|---|
|  | 671 | // process polymorphic return value | 
|---|
| [01aeade] | 672 | retval = 0; | 
|---|
| [3bb195cb] | 673 | if ( isDynRet( functionDecl->get_functionType() ) && functionDecl->get_linkage() == LinkageSpec::Cforall ) { | 
|---|
| [01aeade] | 674 | retval = functionDecl->get_functionType()->get_returnVals().front(); | 
|---|
| [ae63a18] | 675 |  | 
|---|
| [01aeade] | 676 | // give names to unnamed return values | 
|---|
|  | 677 | if ( retval->get_name() == "" ) { | 
|---|
|  | 678 | retval->set_name( "_retparm" ); | 
|---|
|  | 679 | retval->set_linkage( LinkageSpec::C ); | 
|---|
|  | 680 | } // if | 
|---|
|  | 681 | } // if | 
|---|
| [ae63a18] | 682 |  | 
|---|
| [e56cfdb0] | 683 | FunctionType *functionType = functionDecl->get_functionType(); | 
|---|
| [01aeade] | 684 | makeTyVarMap( functionDecl->get_functionType(), scopeTyVars ); | 
|---|
| [c2ad3c9] | 685 | findTypeOps( functionDecl->get_functionType()->get_forall() ); | 
|---|
| [e56cfdb0] | 686 |  | 
|---|
|  | 687 | std::list< DeclarationWithType *> ¶mList = functionType->get_parameters(); | 
|---|
|  | 688 | std::list< FunctionType *> functions; | 
|---|
| [8c49c0e] | 689 | for ( Type::ForallList::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) { | 
|---|
| [e56cfdb0] | 690 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) { | 
|---|
|  | 691 | findFunction( (*assert)->get_type(), functions, scopeTyVars, needsAdapter ); | 
|---|
|  | 692 | } // for | 
|---|
|  | 693 | } // for | 
|---|
|  | 694 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) { | 
|---|
|  | 695 | findFunction( (*arg)->get_type(), functions, scopeTyVars, needsAdapter ); | 
|---|
|  | 696 | } // for | 
|---|
| [b4cd03b7] | 697 |  | 
|---|
| [e56cfdb0] | 698 | for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) { | 
|---|
| [bdf1954] | 699 | std::string mangleName = mangleAdapterName( *funType, scopeTyVars ); | 
|---|
| [e56cfdb0] | 700 | if ( adapters.find( mangleName ) == adapters.end() ) { | 
|---|
|  | 701 | std::string adapterName = makeAdapterName( mangleName ); | 
|---|
|  | 702 | adapters.insert( std::pair< std::string, DeclarationWithType *>( mangleName, new ObjectDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) ) ); | 
|---|
|  | 703 | } // if | 
|---|
|  | 704 | } // for | 
|---|
|  | 705 |  | 
|---|
| [01aeade] | 706 | functionDecl->set_statements( functionDecl->get_statements()->acceptMutator( *this ) ); | 
|---|
| [ae63a18] | 707 |  | 
|---|
| [6f49cdf] | 708 | scopeTyVars.endScope(); | 
|---|
| [63c0dbf] | 709 | assignOps.endScope(); | 
|---|
| [c2ad3c9] | 710 | ctorOps.endScope(); | 
|---|
|  | 711 | copyOps.endScope(); | 
|---|
|  | 712 | dtorOps.endScope(); | 
|---|
| [01aeade] | 713 | retval = oldRetval; | 
|---|
|  | 714 | useRetval = oldUseRetval; | 
|---|
| [e56cfdb0] | 715 | doEndScope(); | 
|---|
| [01aeade] | 716 | } // if | 
|---|
|  | 717 | return functionDecl; | 
|---|
|  | 718 | } | 
|---|
| [6c3744e] | 719 |  | 
|---|
| [01aeade] | 720 | TypeDecl *Pass1::mutate( TypeDecl *typeDecl ) { | 
|---|
| [2c57025] | 721 | addToTyVarMap( typeDecl, scopeTyVars ); | 
|---|
| [01aeade] | 722 | return Mutator::mutate( typeDecl ); | 
|---|
|  | 723 | } | 
|---|
| [6c3744e] | 724 |  | 
|---|
| [01aeade] | 725 | Expression *Pass1::mutate( CommaExpr *commaExpr ) { | 
|---|
|  | 726 | bool oldUseRetval = useRetval; | 
|---|
|  | 727 | useRetval = false; | 
|---|
|  | 728 | commaExpr->set_arg1( maybeMutate( commaExpr->get_arg1(), *this ) ); | 
|---|
|  | 729 | useRetval = oldUseRetval; | 
|---|
|  | 730 | commaExpr->set_arg2( maybeMutate( commaExpr->get_arg2(), *this ) ); | 
|---|
|  | 731 | return commaExpr; | 
|---|
|  | 732 | } | 
|---|
| [6c3744e] | 733 |  | 
|---|
| [01aeade] | 734 | Expression *Pass1::mutate( ConditionalExpr *condExpr ) { | 
|---|
|  | 735 | bool oldUseRetval = useRetval; | 
|---|
|  | 736 | useRetval = false; | 
|---|
|  | 737 | condExpr->set_arg1( maybeMutate( condExpr->get_arg1(), *this ) ); | 
|---|
|  | 738 | useRetval = oldUseRetval; | 
|---|
|  | 739 | condExpr->set_arg2( maybeMutate( condExpr->get_arg2(), *this ) ); | 
|---|
|  | 740 | condExpr->set_arg3( maybeMutate( condExpr->get_arg3(), *this ) ); | 
|---|
|  | 741 | return condExpr; | 
|---|
| [6c3744e] | 742 |  | 
|---|
| [01aeade] | 743 | } | 
|---|
| [6c3744e] | 744 |  | 
|---|
| [5c52b06] | 745 | void Pass1::passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes ) { | 
|---|
| [4b8f918] | 746 | Type *polyType = isPolyType( parmType, exprTyVars ); | 
|---|
|  | 747 | if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) { | 
|---|
|  | 748 | std::string typeName = mangleType( polyType ); | 
|---|
| [adc6781] | 749 | if ( seenTypes.count( typeName ) ) return; | 
|---|
| [5c52b06] | 750 |  | 
|---|
|  | 751 | arg = appExpr->get_args().insert( arg, new SizeofExpr( argBaseType->clone() ) ); | 
|---|
|  | 752 | arg++; | 
|---|
|  | 753 | arg = appExpr->get_args().insert( arg, new AlignofExpr( argBaseType->clone() ) ); | 
|---|
|  | 754 | arg++; | 
|---|
| [4b8f918] | 755 | if ( dynamic_cast< StructInstType* >( polyType ) ) { | 
|---|
| [5c52b06] | 756 | if ( StructInstType *argBaseStructType = dynamic_cast< StructInstType* >( argBaseType ) ) { | 
|---|
| [89173242] | 757 | // zero-length arrays are forbidden by C, so don't pass offset for empty struct | 
|---|
|  | 758 | if ( ! argBaseStructType->get_baseStruct()->get_members().empty() ) { | 
|---|
| [d75038c] | 759 | arg = appExpr->get_args().insert( arg, new OffsetPackExpr( argBaseStructType->clone() ) ); | 
|---|
| [89173242] | 760 | arg++; | 
|---|
|  | 761 | } | 
|---|
| [5c52b06] | 762 | } else { | 
|---|
|  | 763 | throw SemanticError( "Cannot pass non-struct type for generic struct" ); | 
|---|
|  | 764 | } | 
|---|
|  | 765 | } | 
|---|
|  | 766 |  | 
|---|
| [adc6781] | 767 | seenTypes.insert( typeName ); | 
|---|
| [5c52b06] | 768 | } | 
|---|
|  | 769 | } | 
|---|
|  | 770 |  | 
|---|
| [d9fa60a] | 771 | void Pass1::passTypeVars( ApplicationExpr *appExpr, Type *polyRetType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) { | 
|---|
| [7754cde] | 772 | // pass size/align for type variables | 
|---|
| [01aeade] | 773 | for ( TyVarMap::const_iterator tyParm = exprTyVars.begin(); tyParm != exprTyVars.end(); ++tyParm ) { | 
|---|
|  | 774 | ResolvExpr::EqvClass eqvClass; | 
|---|
|  | 775 | assert( env ); | 
|---|
| [2c57025] | 776 | if ( tyParm->second.isComplete ) { | 
|---|
| [01aeade] | 777 | Type *concrete = env->lookup( tyParm->first ); | 
|---|
|  | 778 | if ( concrete ) { | 
|---|
|  | 779 | arg = appExpr->get_args().insert( arg, new SizeofExpr( concrete->clone() ) ); | 
|---|
|  | 780 | arg++; | 
|---|
| [db0b3ce] | 781 | arg = appExpr->get_args().insert( arg, new AlignofExpr( concrete->clone() ) ); | 
|---|
|  | 782 | arg++; | 
|---|
| [01aeade] | 783 | } else { | 
|---|
| [ea5daeb] | 784 | // xxx - should this be an assertion? | 
|---|
| [540de412] | 785 | throw SemanticError( "unbound type variable: " + tyParm->first + " in application ", appExpr ); | 
|---|
| [01aeade] | 786 | } // if | 
|---|
|  | 787 | } // if | 
|---|
|  | 788 | } // for | 
|---|
| [7754cde] | 789 |  | 
|---|
|  | 790 | // add size/align for generic types to parameter list | 
|---|
| [906e24d] | 791 | if ( ! appExpr->get_function()->has_result() ) return; | 
|---|
|  | 792 | FunctionType *funcType = getFunctionType( appExpr->get_function()->get_result() ); | 
|---|
| [7754cde] | 793 | assert( funcType ); | 
|---|
| [ae63a18] | 794 |  | 
|---|
| [7754cde] | 795 | std::list< DeclarationWithType* >::const_iterator fnParm = funcType->get_parameters().begin(); | 
|---|
|  | 796 | std::list< Expression* >::const_iterator fnArg = arg; | 
|---|
| [ea5daeb] | 797 | std::set< std::string > seenTypes; ///< names for generic types we've seen | 
|---|
| [7754cde] | 798 |  | 
|---|
| [5c52b06] | 799 | // a polymorphic return type may need to be added to the argument list | 
|---|
|  | 800 | if ( polyRetType ) { | 
|---|
|  | 801 | Type *concRetType = replaceWithConcrete( appExpr, polyRetType ); | 
|---|
|  | 802 | passArgTypeVars( appExpr, polyRetType, concRetType, arg, exprTyVars, seenTypes ); | 
|---|
|  | 803 | } | 
|---|
| [70a06f6] | 804 |  | 
|---|
| [5c52b06] | 805 | // add type information args for presently unseen types in parameter list | 
|---|
|  | 806 | for ( ; fnParm != funcType->get_parameters().end() && fnArg != appExpr->get_args().end(); ++fnParm, ++fnArg ) { | 
|---|
|  | 807 | VariableExpr *fnArgBase = getBaseVar( *fnArg ); | 
|---|
| [906e24d] | 808 | if ( ! fnArgBase ) continue; // xxx - previously had check for non-empty fnArgBase results | 
|---|
|  | 809 | passArgTypeVars( appExpr, (*fnParm)->get_type(), fnArgBase->get_result(), arg, exprTyVars, seenTypes ); | 
|---|
| [7754cde] | 810 | } | 
|---|
| [01aeade] | 811 | } | 
|---|
| [6c3744e] | 812 |  | 
|---|
| [01aeade] | 813 | ObjectDecl *Pass1::makeTemporary( Type *type ) { | 
|---|
| [68cd1ce] | 814 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, type, 0 ); | 
|---|
| [01aeade] | 815 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) ); | 
|---|
|  | 816 | return newObj; | 
|---|
|  | 817 | } | 
|---|
| [6c3744e] | 818 |  | 
|---|
| [01aeade] | 819 | Expression *Pass1::addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg ) { | 
|---|
| [cf16f94] | 820 | // Create temporary to hold return value of polymorphic function and produce that temporary as a result | 
|---|
|  | 821 | // using a comma expression.  Possibly change comma expression into statement expression "{}" for multiple | 
|---|
|  | 822 | // return values. | 
|---|
| [d9fa60a] | 823 | assert( retType ); | 
|---|
| [cf16f94] | 824 | ObjectDecl *newObj = makeTemporary( retType->clone() ); | 
|---|
|  | 825 | Expression *paramExpr = new VariableExpr( newObj ); | 
|---|
| [5c52b06] | 826 |  | 
|---|
|  | 827 | // If the type of the temporary is not polymorphic, box temporary by taking its address; | 
|---|
|  | 828 | // otherwise the temporary is already boxed and can be used directly. | 
|---|
| [5f6c42c] | 829 | if ( ! isPolyType( newObj->get_type(), scopeTyVars, env ) ) { | 
|---|
| [cf16f94] | 830 | paramExpr = new AddressExpr( paramExpr ); | 
|---|
| [01aeade] | 831 | } // if | 
|---|
| [cf16f94] | 832 | arg = appExpr->get_args().insert( arg, paramExpr ); // add argument to function call | 
|---|
|  | 833 | arg++; | 
|---|
|  | 834 | // Build a comma expression to call the function and emulate a normal return. | 
|---|
|  | 835 | CommaExpr *commaExpr = new CommaExpr( appExpr, new VariableExpr( newObj ) ); | 
|---|
|  | 836 | commaExpr->set_env( appExpr->get_env() ); | 
|---|
|  | 837 | appExpr->set_env( 0 ); | 
|---|
|  | 838 | return commaExpr; | 
|---|
| [01aeade] | 839 | } | 
|---|
| [6c3744e] | 840 |  | 
|---|
| [48ca586] | 841 | void Pass1::replaceParametersWithConcrete( ApplicationExpr *appExpr, std::list< Expression* >& params ) { | 
|---|
|  | 842 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) { | 
|---|
|  | 843 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); | 
|---|
|  | 844 | assert(paramType && "Aggregate parameters should be type expressions"); | 
|---|
|  | 845 | paramType->set_type( replaceWithConcrete( appExpr, paramType->get_type(), false ) ); | 
|---|
|  | 846 | } | 
|---|
|  | 847 | } | 
|---|
| [b4cd03b7] | 848 |  | 
|---|
| [48ca586] | 849 | Type *Pass1::replaceWithConcrete( ApplicationExpr *appExpr, Type *type, bool doClone ) { | 
|---|
|  | 850 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) { | 
|---|
|  | 851 | Type *concrete = env->lookup( typeInst->get_name() ); | 
|---|
|  | 852 | if ( concrete == 0 ) { | 
|---|
|  | 853 | throw SemanticError( "Unbound type variable " + typeInst->get_name() + " in ", appExpr ); | 
|---|
|  | 854 | } // if | 
|---|
|  | 855 | return concrete; | 
|---|
|  | 856 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { | 
|---|
|  | 857 | if ( doClone ) { | 
|---|
|  | 858 | structType = structType->clone(); | 
|---|
|  | 859 | } | 
|---|
|  | 860 | replaceParametersWithConcrete( appExpr, structType->get_parameters() ); | 
|---|
|  | 861 | return structType; | 
|---|
|  | 862 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { | 
|---|
|  | 863 | if ( doClone ) { | 
|---|
|  | 864 | unionType = unionType->clone(); | 
|---|
|  | 865 | } | 
|---|
|  | 866 | replaceParametersWithConcrete( appExpr, unionType->get_parameters() ); | 
|---|
|  | 867 | return unionType; | 
|---|
|  | 868 | } | 
|---|
|  | 869 | return type; | 
|---|
|  | 870 | } | 
|---|
|  | 871 |  | 
|---|
| [d9fa60a] | 872 | Expression *Pass1::addDynRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *dynType, std::list< Expression *>::iterator &arg ) { | 
|---|
| [01aeade] | 873 | assert( env ); | 
|---|
| [3bb195cb] | 874 | Type *concrete = replaceWithConcrete( appExpr, dynType ); | 
|---|
| [70a06f6] | 875 | // add out-parameter for return value | 
|---|
| [01aeade] | 876 | return addRetParam( appExpr, function, concrete, arg ); | 
|---|
|  | 877 | } | 
|---|
| [6c3744e] | 878 |  | 
|---|
| [01aeade] | 879 | Expression *Pass1::applyAdapter( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ) { | 
|---|
|  | 880 | Expression *ret = appExpr; | 
|---|
| [3bb195cb] | 881 | //                      if ( ! function->get_returnVals().empty() && isPolyType( function->get_returnVals().front()->get_type(), tyVars ) ) { | 
|---|
|  | 882 | if ( isDynRet( function, tyVars ) ) { | 
|---|
| [01aeade] | 883 | ret = addRetParam( appExpr, function, function->get_returnVals().front()->get_type(), arg ); | 
|---|
|  | 884 | } // if | 
|---|
| [bdf1954] | 885 | std::string mangleName = mangleAdapterName( function, tyVars ); | 
|---|
| [01aeade] | 886 | std::string adapterName = makeAdapterName( mangleName ); | 
|---|
| [b1a6d6b] | 887 |  | 
|---|
| [b4cd03b7] | 888 | // cast adaptee to void (*)(), since it may have any type inside a polymorphic function | 
|---|
|  | 889 | Type * adapteeType = new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) ); | 
|---|
|  | 890 | appExpr->get_args().push_front( new CastExpr( appExpr->get_function(), adapteeType ) ); | 
|---|
| [906e24d] | 891 | appExpr->set_function( new NameExpr( adapterName ) ); // xxx - result is never set on NameExpr | 
|---|
| [ae63a18] | 892 |  | 
|---|
| [01aeade] | 893 | return ret; | 
|---|
|  | 894 | } | 
|---|
| [6c3744e] | 895 |  | 
|---|
| [01aeade] | 896 | void Pass1::boxParam( Type *param, Expression *&arg, const TyVarMap &exprTyVars ) { | 
|---|
| [906e24d] | 897 | assert( arg->has_result() ); | 
|---|
| [1cced28] | 898 | if ( isPolyType( param, exprTyVars ) ) { | 
|---|
| [906e24d] | 899 | if ( isPolyType( arg->get_result() ) ) { | 
|---|
| [9407ed8] | 900 | // if the argument's type is polymorphic, we don't need to box again! | 
|---|
| [01aeade] | 901 | return; | 
|---|
| [906e24d] | 902 | } else if ( arg->get_result()->get_isLvalue() ) { | 
|---|
| [b3ab8f0] | 903 | // VariableExpr and MemberExpr are lvalues; need to check this isn't coming from the second arg of a comma expression though (not an lvalue) | 
|---|
| [fea7ca7] | 904 | // xxx - need to test that this code is still reachable | 
|---|
| [b3ab8f0] | 905 | if ( CommaExpr *commaArg = dynamic_cast< CommaExpr* >( arg ) ) { | 
|---|
| [bc1ab61] | 906 | commaArg->set_arg2( new AddressExpr( commaArg->get_arg2() ) ); | 
|---|
| [b3ab8f0] | 907 | } else { | 
|---|
|  | 908 | arg = new AddressExpr( arg ); | 
|---|
|  | 909 | } | 
|---|
| [01aeade] | 910 | } else { | 
|---|
| [bd85400] | 911 | // use type computed in unification to declare boxed variables | 
|---|
|  | 912 | Type * newType = param->clone(); | 
|---|
|  | 913 | if ( env ) env->apply( newType ); | 
|---|
|  | 914 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, newType, 0 ); | 
|---|
| [f6d7e0f] | 915 | newObj->get_type()->get_qualifiers() = Type::Qualifiers(); // TODO: is this right??? | 
|---|
| [01aeade] | 916 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) ); | 
|---|
|  | 917 | UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) ); | 
|---|
|  | 918 | assign->get_args().push_back( new VariableExpr( newObj ) ); | 
|---|
|  | 919 | assign->get_args().push_back( arg ); | 
|---|
|  | 920 | stmtsToAdd.push_back( new ExprStmt( noLabels, assign ) ); | 
|---|
|  | 921 | arg = new AddressExpr( new VariableExpr( newObj ) ); | 
|---|
|  | 922 | } // if | 
|---|
|  | 923 | } // if | 
|---|
|  | 924 | } | 
|---|
| [6c3744e] | 925 |  | 
|---|
| [b4cd03b7] | 926 | /// cast parameters to polymorphic functions so that types are replaced with | 
|---|
|  | 927 | /// void * if they are type parameters in the formal type. | 
|---|
|  | 928 | /// this gets rid of warnings from gcc. | 
|---|
| [01aeade] | 929 | void addCast( Expression *&actual, Type *formal, const TyVarMap &tyVars ) { | 
|---|
| [ea5daeb] | 930 | if ( getFunctionType( formal ) ) { | 
|---|
|  | 931 | Type * newType = formal->clone(); | 
|---|
| [b4cd03b7] | 932 | newType = ScrubTyVars::scrub( newType, tyVars ); | 
|---|
| [01aeade] | 933 | actual = new CastExpr( actual, newType ); | 
|---|
|  | 934 | } // if | 
|---|
|  | 935 | } | 
|---|
| [6c3744e] | 936 |  | 
|---|
| [01aeade] | 937 | void Pass1::boxParams( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) { | 
|---|
|  | 938 | for ( std::list< DeclarationWithType *>::const_iterator param = function->get_parameters().begin(); param != function->get_parameters().end(); ++param, ++arg ) { | 
|---|
|  | 939 | assert( arg != appExpr->get_args().end() ); | 
|---|
|  | 940 | addCast( *arg, (*param)->get_type(), exprTyVars ); | 
|---|
|  | 941 | boxParam( (*param)->get_type(), *arg, exprTyVars ); | 
|---|
|  | 942 | } // for | 
|---|
|  | 943 | } | 
|---|
| [6c3744e] | 944 |  | 
|---|
| [01aeade] | 945 | void Pass1::addInferredParams( ApplicationExpr *appExpr, FunctionType *functionType, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ) { | 
|---|
|  | 946 | std::list< Expression *>::iterator cur = arg; | 
|---|
| [8c49c0e] | 947 | for ( Type::ForallList::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) { | 
|---|
| [01aeade] | 948 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) { | 
|---|
|  | 949 | InferredParams::const_iterator inferParam = appExpr->get_inferParams().find( (*assert)->get_uniqueId() ); | 
|---|
| [698664b3] | 950 | assert( inferParam != appExpr->get_inferParams().end() && "NOTE: Explicit casts of polymorphic functions to compatible monomorphic functions are currently unsupported" ); | 
|---|
| [01aeade] | 951 | Expression *newExpr = inferParam->second.expr->clone(); | 
|---|
|  | 952 | addCast( newExpr, (*assert)->get_type(), tyVars ); | 
|---|
|  | 953 | boxParam( (*assert)->get_type(), newExpr, tyVars ); | 
|---|
|  | 954 | appExpr->get_args().insert( cur, newExpr ); | 
|---|
|  | 955 | } // for | 
|---|
|  | 956 | } // for | 
|---|
|  | 957 | } | 
|---|
| [6c3744e] | 958 |  | 
|---|
| [01aeade] | 959 | void makeRetParm( FunctionType *funcType ) { | 
|---|
|  | 960 | DeclarationWithType *retParm = funcType->get_returnVals().front(); | 
|---|
| [6c3744e] | 961 |  | 
|---|
| [01aeade] | 962 | // make a new parameter that is a pointer to the type of the old return value | 
|---|
|  | 963 | retParm->set_type( new PointerType( Type::Qualifiers(), retParm->get_type() ) ); | 
|---|
|  | 964 | funcType->get_parameters().push_front( retParm ); | 
|---|
| [6c3744e] | 965 |  | 
|---|
| [01aeade] | 966 | // we don't need the return value any more | 
|---|
|  | 967 | funcType->get_returnVals().clear(); | 
|---|
|  | 968 | } | 
|---|
| [6c3744e] | 969 |  | 
|---|
| [01aeade] | 970 | FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars ) { | 
|---|
|  | 971 | // actually make the adapter type | 
|---|
|  | 972 | FunctionType *adapter = adaptee->clone(); | 
|---|
| [3bb195cb] | 973 | //                      if ( ! adapter->get_returnVals().empty() && isPolyType( adapter->get_returnVals().front()->get_type(), tyVars ) ) { | 
|---|
|  | 974 | if ( isDynRet( adapter, tyVars ) ) { | 
|---|
| [01aeade] | 975 | makeRetParm( adapter ); | 
|---|
|  | 976 | } // if | 
|---|
| [68cd1ce] | 977 | adapter->get_parameters().push_front( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) ), 0 ) ); | 
|---|
| [01aeade] | 978 | return adapter; | 
|---|
|  | 979 | } | 
|---|
| [6c3744e] | 980 |  | 
|---|
| [01aeade] | 981 | Expression *makeAdapterArg( DeclarationWithType *param, DeclarationWithType *arg, DeclarationWithType *realParam, const TyVarMap &tyVars ) { | 
|---|
|  | 982 | assert( param ); | 
|---|
|  | 983 | assert( arg ); | 
|---|
| [ffad73a] | 984 | if ( isPolyType( realParam->get_type(), tyVars ) ) { | 
|---|
| [30aeb27] | 985 | if ( ! isPolyType( arg->get_type() ) ) { | 
|---|
| [e56cfdb0] | 986 | UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); | 
|---|
|  | 987 | deref->get_args().push_back( new CastExpr( new VariableExpr( param ), new PointerType( Type::Qualifiers(), arg->get_type()->clone() ) ) ); | 
|---|
| [906e24d] | 988 | deref->set_result( arg->get_type()->clone() ); | 
|---|
| [e56cfdb0] | 989 | return deref; | 
|---|
|  | 990 | } // if | 
|---|
| [01aeade] | 991 | } // if | 
|---|
|  | 992 | return new VariableExpr( param ); | 
|---|
|  | 993 | } | 
|---|
| [6c3744e] | 994 |  | 
|---|
| [01aeade] | 995 | void addAdapterParams( ApplicationExpr *adapteeApp, std::list< DeclarationWithType *>::iterator arg, std::list< DeclarationWithType *>::iterator param, std::list< DeclarationWithType *>::iterator paramEnd, std::list< DeclarationWithType *>::iterator realParam, const TyVarMap &tyVars ) { | 
|---|
|  | 996 | UniqueName paramNamer( "_p" ); | 
|---|
|  | 997 | for ( ; param != paramEnd; ++param, ++arg, ++realParam ) { | 
|---|
|  | 998 | if ( (*param)->get_name() == "" ) { | 
|---|
|  | 999 | (*param)->set_name( paramNamer.newName() ); | 
|---|
|  | 1000 | (*param)->set_linkage( LinkageSpec::C ); | 
|---|
|  | 1001 | } // if | 
|---|
|  | 1002 | adapteeApp->get_args().push_back( makeAdapterArg( *param, *arg, *realParam, tyVars ) ); | 
|---|
|  | 1003 | } // for | 
|---|
|  | 1004 | } | 
|---|
| [6c3744e] | 1005 |  | 
|---|
| [01aeade] | 1006 | FunctionDecl *Pass1::makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ) { | 
|---|
|  | 1007 | FunctionType *adapterType = makeAdapterType( adaptee, tyVars ); | 
|---|
|  | 1008 | adapterType = ScrubTyVars::scrub( adapterType, tyVars ); | 
|---|
|  | 1009 | DeclarationWithType *adapteeDecl = adapterType->get_parameters().front(); | 
|---|
|  | 1010 | adapteeDecl->set_name( "_adaptee" ); | 
|---|
|  | 1011 | ApplicationExpr *adapteeApp = new ApplicationExpr( new CastExpr( new VariableExpr( adapteeDecl ), new PointerType( Type::Qualifiers(), realType ) ) ); | 
|---|
|  | 1012 | Statement *bodyStmt; | 
|---|
| [ae63a18] | 1013 |  | 
|---|
| [8c49c0e] | 1014 | Type::ForallList::iterator tyArg = realType->get_forall().begin(); | 
|---|
|  | 1015 | Type::ForallList::iterator tyParam = adapterType->get_forall().begin(); | 
|---|
|  | 1016 | Type::ForallList::iterator realTyParam = adaptee->get_forall().begin(); | 
|---|
| [01aeade] | 1017 | for ( ; tyParam != adapterType->get_forall().end(); ++tyArg, ++tyParam, ++realTyParam ) { | 
|---|
|  | 1018 | assert( tyArg != realType->get_forall().end() ); | 
|---|
|  | 1019 | std::list< DeclarationWithType *>::iterator assertArg = (*tyArg)->get_assertions().begin(); | 
|---|
|  | 1020 | std::list< DeclarationWithType *>::iterator assertParam = (*tyParam)->get_assertions().begin(); | 
|---|
|  | 1021 | std::list< DeclarationWithType *>::iterator realAssertParam = (*realTyParam)->get_assertions().begin(); | 
|---|
|  | 1022 | for ( ; assertParam != (*tyParam)->get_assertions().end(); ++assertArg, ++assertParam, ++realAssertParam ) { | 
|---|
|  | 1023 | assert( assertArg != (*tyArg)->get_assertions().end() ); | 
|---|
|  | 1024 | adapteeApp->get_args().push_back( makeAdapterArg( *assertParam, *assertArg, *realAssertParam, tyVars ) ); | 
|---|
|  | 1025 | } // for | 
|---|
|  | 1026 | } // for | 
|---|
| [ae63a18] | 1027 |  | 
|---|
| [01aeade] | 1028 | std::list< DeclarationWithType *>::iterator arg = realType->get_parameters().begin(); | 
|---|
|  | 1029 | std::list< DeclarationWithType *>::iterator param = adapterType->get_parameters().begin(); | 
|---|
|  | 1030 | std::list< DeclarationWithType *>::iterator realParam = adaptee->get_parameters().begin(); | 
|---|
| [cc3528f] | 1031 | param++;                // skip adaptee parameter in the adapter type | 
|---|
| [01aeade] | 1032 | if ( realType->get_returnVals().empty() ) { | 
|---|
| [cc3528f] | 1033 | // void return | 
|---|
| [01aeade] | 1034 | addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars ); | 
|---|
|  | 1035 | bodyStmt = new ExprStmt( noLabels, adapteeApp ); | 
|---|
| [3bb195cb] | 1036 | //                      } else if ( isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) { | 
|---|
|  | 1037 | } else if ( isDynType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) { | 
|---|
| [cc3528f] | 1038 | // return type T | 
|---|
| [01aeade] | 1039 | if ( (*param)->get_name() == "" ) { | 
|---|
|  | 1040 | (*param)->set_name( "_ret" ); | 
|---|
|  | 1041 | (*param)->set_linkage( LinkageSpec::C ); | 
|---|
|  | 1042 | } // if | 
|---|
|  | 1043 | UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) ); | 
|---|
|  | 1044 | UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); | 
|---|
|  | 1045 | deref->get_args().push_back( new CastExpr( new VariableExpr( *param++ ), new PointerType( Type::Qualifiers(), realType->get_returnVals().front()->get_type()->clone() ) ) ); | 
|---|
|  | 1046 | assign->get_args().push_back( deref ); | 
|---|
|  | 1047 | addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars ); | 
|---|
|  | 1048 | assign->get_args().push_back( adapteeApp ); | 
|---|
|  | 1049 | bodyStmt = new ExprStmt( noLabels, assign ); | 
|---|
|  | 1050 | } else { | 
|---|
|  | 1051 | // adapter for a function that returns a monomorphic value | 
|---|
|  | 1052 | addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars ); | 
|---|
|  | 1053 | bodyStmt = new ReturnStmt( noLabels, adapteeApp ); | 
|---|
|  | 1054 | } // if | 
|---|
|  | 1055 | CompoundStmt *adapterBody = new CompoundStmt( noLabels ); | 
|---|
|  | 1056 | adapterBody->get_kids().push_back( bodyStmt ); | 
|---|
|  | 1057 | std::string adapterName = makeAdapterName( mangleName ); | 
|---|
| [de62360d] | 1058 | return new FunctionDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, adapterType, adapterBody, false, false ); | 
|---|
| [01aeade] | 1059 | } | 
|---|
| [6c3744e] | 1060 |  | 
|---|
| [c29d9ce] | 1061 | void Pass1::passAdapters( ApplicationExpr * appExpr, FunctionType * functionType, const TyVarMap & exprTyVars ) { | 
|---|
| [e497c1d] | 1062 | // collect a list of function types passed as parameters or implicit parameters (assertions) | 
|---|
| [01aeade] | 1063 | std::list< DeclarationWithType *> ¶mList = functionType->get_parameters(); | 
|---|
|  | 1064 | std::list< FunctionType *> functions; | 
|---|
| [8c49c0e] | 1065 | for ( Type::ForallList::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) { | 
|---|
| [01aeade] | 1066 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) { | 
|---|
|  | 1067 | findFunction( (*assert)->get_type(), functions, exprTyVars, needsAdapter ); | 
|---|
|  | 1068 | } // for | 
|---|
|  | 1069 | } // for | 
|---|
|  | 1070 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) { | 
|---|
|  | 1071 | findFunction( (*arg)->get_type(), functions, exprTyVars, needsAdapter ); | 
|---|
|  | 1072 | } // for | 
|---|
| [e497c1d] | 1073 |  | 
|---|
| [e56cfdb0] | 1074 | // parameter function types for which an appropriate adapter has been generated.  we cannot use the types | 
|---|
|  | 1075 | // after applying substitutions, since two different parameter types may be unified to the same type | 
|---|
| [01aeade] | 1076 | std::set< std::string > adaptersDone; | 
|---|
| [e497c1d] | 1077 |  | 
|---|
| [01aeade] | 1078 | for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) { | 
|---|
| [c29d9ce] | 1079 | FunctionType *originalFunction = (*funType)->clone(); | 
|---|
| [01aeade] | 1080 | FunctionType *realFunction = (*funType)->clone(); | 
|---|
|  | 1081 | std::string mangleName = SymTab::Mangler::mangle( realFunction ); | 
|---|
| [e497c1d] | 1082 |  | 
|---|
| [e56cfdb0] | 1083 | // only attempt to create an adapter or pass one as a parameter if we haven't already done so for this | 
|---|
|  | 1084 | // pre-substitution parameter function type. | 
|---|
| [01aeade] | 1085 | if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) { | 
|---|
| [e497c1d] | 1086 | adaptersDone.insert( adaptersDone.begin(), mangleName ); | 
|---|
| [ae63a18] | 1087 |  | 
|---|
| [e56cfdb0] | 1088 | // apply substitution to type variables to figure out what the adapter's type should look like | 
|---|
| [e497c1d] | 1089 | assert( env ); | 
|---|
|  | 1090 | env->apply( realFunction ); | 
|---|
| [ae63a18] | 1091 | mangleName = SymTab::Mangler::mangle( realFunction ); | 
|---|
| [bdf1954] | 1092 | mangleName += makePolyMonoSuffix( originalFunction, exprTyVars ); | 
|---|
| [e497c1d] | 1093 |  | 
|---|
| [6635c74] | 1094 | typedef ScopedMap< std::string, DeclarationWithType* >::iterator AdapterIter; | 
|---|
|  | 1095 | AdapterIter adapter = adapters.find( mangleName ); | 
|---|
| [e56cfdb0] | 1096 | if ( adapter == adapters.end() ) { | 
|---|
|  | 1097 | // adapter has not been created yet in the current scope, so define it | 
|---|
|  | 1098 | FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars ); | 
|---|
| [6635c74] | 1099 | std::pair< AdapterIter, bool > answer = adapters.insert( std::pair< std::string, DeclarationWithType *>( mangleName, newAdapter ) ); | 
|---|
|  | 1100 | adapter = answer.first; | 
|---|
| [e56cfdb0] | 1101 | stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) ); | 
|---|
| [c29d9ce] | 1102 | } // if | 
|---|
| [e56cfdb0] | 1103 | assert( adapter != adapters.end() ); | 
|---|
|  | 1104 |  | 
|---|
|  | 1105 | // add the appropriate adapter as a parameter | 
|---|
|  | 1106 | appExpr->get_args().push_front( new VariableExpr( adapter->second ) ); | 
|---|
| [01aeade] | 1107 | } // if | 
|---|
|  | 1108 | } // for | 
|---|
| [e56cfdb0] | 1109 | } // passAdapters | 
|---|
| [6c3744e] | 1110 |  | 
|---|
| [78dd0da] | 1111 | Expression *makeIncrDecrExpr( ApplicationExpr *appExpr, Type *polyType, bool isIncr ) { | 
|---|
| [01aeade] | 1112 | NameExpr *opExpr; | 
|---|
|  | 1113 | if ( isIncr ) { | 
|---|
|  | 1114 | opExpr = new NameExpr( "?+=?" ); | 
|---|
|  | 1115 | } else { | 
|---|
|  | 1116 | opExpr = new NameExpr( "?-=?" ); | 
|---|
| [6c3744e] | 1117 | } // if | 
|---|
| [01aeade] | 1118 | UntypedExpr *addAssign = new UntypedExpr( opExpr ); | 
|---|
|  | 1119 | if ( AddressExpr *address = dynamic_cast< AddressExpr *>( appExpr->get_args().front() ) ) { | 
|---|
|  | 1120 | addAssign->get_args().push_back( address->get_arg() ); | 
|---|
|  | 1121 | } else { | 
|---|
|  | 1122 | addAssign->get_args().push_back( appExpr->get_args().front() ); | 
|---|
| [6c3744e] | 1123 | } // if | 
|---|
| [adc6781] | 1124 | addAssign->get_args().push_back( new NameExpr( sizeofName( mangleType( polyType ) ) ) ); | 
|---|
| [906e24d] | 1125 | addAssign->set_result( appExpr->get_result()->clone() ); | 
|---|
| [01aeade] | 1126 | if ( appExpr->get_env() ) { | 
|---|
|  | 1127 | addAssign->set_env( appExpr->get_env() ); | 
|---|
| [6c3744e] | 1128 | appExpr->set_env( 0 ); | 
|---|
|  | 1129 | } // if | 
|---|
| [01aeade] | 1130 | appExpr->get_args().clear(); | 
|---|
|  | 1131 | delete appExpr; | 
|---|
|  | 1132 | return addAssign; | 
|---|
|  | 1133 | } | 
|---|
|  | 1134 |  | 
|---|
|  | 1135 | Expression *Pass1::handleIntrinsics( ApplicationExpr *appExpr ) { | 
|---|
|  | 1136 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr *>( appExpr->get_function() ) ) { | 
|---|
|  | 1137 | if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic ) { | 
|---|
|  | 1138 | if ( varExpr->get_var()->get_name() == "?[?]" ) { | 
|---|
| [906e24d] | 1139 | assert( appExpr->has_result() ); | 
|---|
| [01aeade] | 1140 | assert( appExpr->get_args().size() == 2 ); | 
|---|
| [906e24d] | 1141 | Type *baseType1 = isPolyPtr( appExpr->get_args().front()->get_result(), scopeTyVars, env ); | 
|---|
|  | 1142 | Type *baseType2 = isPolyPtr( appExpr->get_args().back()->get_result(), scopeTyVars, env ); | 
|---|
| [ae63a18] | 1143 | assert( ! baseType1 || ! baseType2 ); // the arguments cannot both be polymorphic pointers | 
|---|
| [01aeade] | 1144 | UntypedExpr *ret = 0; | 
|---|
| [ae63a18] | 1145 | if ( baseType1 || baseType2 ) { // one of the arguments is a polymorphic pointer | 
|---|
| [01aeade] | 1146 | ret = new UntypedExpr( new NameExpr( "?+?" ) ); | 
|---|
|  | 1147 | } // if | 
|---|
| [ffad73a] | 1148 | if ( baseType1 ) { | 
|---|
| [01aeade] | 1149 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); | 
|---|
|  | 1150 | multiply->get_args().push_back( appExpr->get_args().back() ); | 
|---|
| [adc6781] | 1151 | multiply->get_args().push_back( new SizeofExpr( baseType1->clone() ) ); | 
|---|
| [01aeade] | 1152 | ret->get_args().push_back( appExpr->get_args().front() ); | 
|---|
|  | 1153 | ret->get_args().push_back( multiply ); | 
|---|
| [ffad73a] | 1154 | } else if ( baseType2 ) { | 
|---|
| [01aeade] | 1155 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); | 
|---|
|  | 1156 | multiply->get_args().push_back( appExpr->get_args().front() ); | 
|---|
| [adc6781] | 1157 | multiply->get_args().push_back( new SizeofExpr( baseType2->clone() ) ); | 
|---|
| [01aeade] | 1158 | ret->get_args().push_back( multiply ); | 
|---|
|  | 1159 | ret->get_args().push_back( appExpr->get_args().back() ); | 
|---|
|  | 1160 | } // if | 
|---|
| [ffad73a] | 1161 | if ( baseType1 || baseType2 ) { | 
|---|
| [906e24d] | 1162 | ret->set_result( appExpr->get_result()->clone() ); | 
|---|
| [01aeade] | 1163 | if ( appExpr->get_env() ) { | 
|---|
|  | 1164 | ret->set_env( appExpr->get_env() ); | 
|---|
|  | 1165 | appExpr->set_env( 0 ); | 
|---|
|  | 1166 | } // if | 
|---|
|  | 1167 | appExpr->get_args().clear(); | 
|---|
|  | 1168 | delete appExpr; | 
|---|
|  | 1169 | return ret; | 
|---|
|  | 1170 | } // if | 
|---|
|  | 1171 | } else if ( varExpr->get_var()->get_name() == "*?" ) { | 
|---|
| [906e24d] | 1172 | assert( appExpr->has_result() ); | 
|---|
| [01aeade] | 1173 | assert( ! appExpr->get_args().empty() ); | 
|---|
| [906e24d] | 1174 | if ( isPolyType( appExpr->get_result(), scopeTyVars, env ) ) { | 
|---|
| [01aeade] | 1175 | Expression *ret = appExpr->get_args().front(); | 
|---|
| [906e24d] | 1176 | delete ret->get_result(); | 
|---|
|  | 1177 | ret->set_result( appExpr->get_result()->clone() ); | 
|---|
| [01aeade] | 1178 | if ( appExpr->get_env() ) { | 
|---|
|  | 1179 | ret->set_env( appExpr->get_env() ); | 
|---|
|  | 1180 | appExpr->set_env( 0 ); | 
|---|
|  | 1181 | } // if | 
|---|
|  | 1182 | appExpr->get_args().clear(); | 
|---|
|  | 1183 | delete appExpr; | 
|---|
|  | 1184 | return ret; | 
|---|
|  | 1185 | } // if | 
|---|
|  | 1186 | } else if ( varExpr->get_var()->get_name() == "?++" || varExpr->get_var()->get_name() == "?--" ) { | 
|---|
| [906e24d] | 1187 | assert( appExpr->has_result() ); | 
|---|
| [01aeade] | 1188 | assert( appExpr->get_args().size() == 1 ); | 
|---|
| [906e24d] | 1189 | if ( Type *baseType = isPolyPtr( appExpr->get_result(), scopeTyVars, env ) ) { | 
|---|
|  | 1190 | Type *tempType = appExpr->get_result()->clone(); | 
|---|
| [01aeade] | 1191 | if ( env ) { | 
|---|
|  | 1192 | env->apply( tempType ); | 
|---|
|  | 1193 | } // if | 
|---|
|  | 1194 | ObjectDecl *newObj = makeTemporary( tempType ); | 
|---|
|  | 1195 | VariableExpr *tempExpr = new VariableExpr( newObj ); | 
|---|
|  | 1196 | UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) ); | 
|---|
|  | 1197 | assignExpr->get_args().push_back( tempExpr->clone() ); | 
|---|
|  | 1198 | if ( AddressExpr *address = dynamic_cast< AddressExpr *>( appExpr->get_args().front() ) ) { | 
|---|
|  | 1199 | assignExpr->get_args().push_back( address->get_arg()->clone() ); | 
|---|
|  | 1200 | } else { | 
|---|
|  | 1201 | assignExpr->get_args().push_back( appExpr->get_args().front()->clone() ); | 
|---|
|  | 1202 | } // if | 
|---|
| [ffad73a] | 1203 | CommaExpr *firstComma = new CommaExpr( assignExpr, makeIncrDecrExpr( appExpr, baseType, varExpr->get_var()->get_name() == "?++" ) ); | 
|---|
| [01aeade] | 1204 | return new CommaExpr( firstComma, tempExpr ); | 
|---|
|  | 1205 | } // if | 
|---|
|  | 1206 | } else if ( varExpr->get_var()->get_name() == "++?" || varExpr->get_var()->get_name() == "--?" ) { | 
|---|
| [906e24d] | 1207 | assert( appExpr->has_result() ); | 
|---|
| [01aeade] | 1208 | assert( appExpr->get_args().size() == 1 ); | 
|---|
| [906e24d] | 1209 | if ( Type *baseType = isPolyPtr( appExpr->get_result(), scopeTyVars, env ) ) { | 
|---|
| [ffad73a] | 1210 | return makeIncrDecrExpr( appExpr, baseType, varExpr->get_var()->get_name() == "++?" ); | 
|---|
| [01aeade] | 1211 | } // if | 
|---|
|  | 1212 | } else if ( varExpr->get_var()->get_name() == "?+?" || varExpr->get_var()->get_name() == "?-?" ) { | 
|---|
| [906e24d] | 1213 | assert( appExpr->has_result() ); | 
|---|
| [01aeade] | 1214 | assert( appExpr->get_args().size() == 2 ); | 
|---|
| [906e24d] | 1215 | Type *baseType1 = isPolyPtr( appExpr->get_args().front()->get_result(), scopeTyVars, env ); | 
|---|
|  | 1216 | Type *baseType2 = isPolyPtr( appExpr->get_args().back()->get_result(), scopeTyVars, env ); | 
|---|
| [ffad73a] | 1217 | if ( baseType1 && baseType2 ) { | 
|---|
| [01aeade] | 1218 | UntypedExpr *divide = new UntypedExpr( new NameExpr( "?/?" ) ); | 
|---|
|  | 1219 | divide->get_args().push_back( appExpr ); | 
|---|
| [adc6781] | 1220 | divide->get_args().push_back( new SizeofExpr( baseType1->clone() ) ); | 
|---|
| [906e24d] | 1221 | divide->set_result( appExpr->get_result()->clone() ); | 
|---|
| [01aeade] | 1222 | if ( appExpr->get_env() ) { | 
|---|
|  | 1223 | divide->set_env( appExpr->get_env() ); | 
|---|
|  | 1224 | appExpr->set_env( 0 ); | 
|---|
|  | 1225 | } // if | 
|---|
|  | 1226 | return divide; | 
|---|
| [ffad73a] | 1227 | } else if ( baseType1 ) { | 
|---|
| [01aeade] | 1228 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); | 
|---|
|  | 1229 | multiply->get_args().push_back( appExpr->get_args().back() ); | 
|---|
| [adc6781] | 1230 | multiply->get_args().push_back( new SizeofExpr( baseType1->clone() ) ); | 
|---|
| [01aeade] | 1231 | appExpr->get_args().back() = multiply; | 
|---|
| [ffad73a] | 1232 | } else if ( baseType2 ) { | 
|---|
| [01aeade] | 1233 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); | 
|---|
|  | 1234 | multiply->get_args().push_back( appExpr->get_args().front() ); | 
|---|
| [adc6781] | 1235 | multiply->get_args().push_back( new SizeofExpr( baseType2->clone() ) ); | 
|---|
| [01aeade] | 1236 | appExpr->get_args().front() = multiply; | 
|---|
|  | 1237 | } // if | 
|---|
|  | 1238 | } else if ( varExpr->get_var()->get_name() == "?+=?" || varExpr->get_var()->get_name() == "?-=?" ) { | 
|---|
| [906e24d] | 1239 | assert( appExpr->has_result() ); | 
|---|
| [01aeade] | 1240 | assert( appExpr->get_args().size() == 2 ); | 
|---|
| [906e24d] | 1241 | Type *baseType = isPolyPtr( appExpr->get_result(), scopeTyVars, env ); | 
|---|
| [ffad73a] | 1242 | if ( baseType ) { | 
|---|
| [01aeade] | 1243 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); | 
|---|
|  | 1244 | multiply->get_args().push_back( appExpr->get_args().back() ); | 
|---|
| [adc6781] | 1245 | multiply->get_args().push_back( new SizeofExpr( baseType->clone() ) ); | 
|---|
| [01aeade] | 1246 | appExpr->get_args().back() = multiply; | 
|---|
|  | 1247 | } // if | 
|---|
|  | 1248 | } // if | 
|---|
|  | 1249 | return appExpr; | 
|---|
|  | 1250 | } // if | 
|---|
| [6c3744e] | 1251 | } // if | 
|---|
| [01aeade] | 1252 | return 0; | 
|---|
|  | 1253 | } | 
|---|
| [6c3744e] | 1254 |  | 
|---|
| [01aeade] | 1255 | Expression *Pass1::mutate( ApplicationExpr *appExpr ) { | 
|---|
| [e56cfdb0] | 1256 | // std::cerr << "mutate appExpr: "; | 
|---|
|  | 1257 | // for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) { | 
|---|
|  | 1258 | //      std::cerr << i->first << " "; | 
|---|
|  | 1259 | // } | 
|---|
|  | 1260 | // std::cerr << "\n"; | 
|---|
| [01aeade] | 1261 | bool oldUseRetval = useRetval; | 
|---|
|  | 1262 | useRetval = false; | 
|---|
|  | 1263 | appExpr->get_function()->acceptMutator( *this ); | 
|---|
|  | 1264 | mutateAll( appExpr->get_args(), *this ); | 
|---|
|  | 1265 | useRetval = oldUseRetval; | 
|---|
| [ae63a18] | 1266 |  | 
|---|
| [906e24d] | 1267 | assert( appExpr->get_function()->has_result() ); | 
|---|
|  | 1268 | PointerType *pointer = safe_dynamic_cast< PointerType *>( appExpr->get_function()->get_result() ); | 
|---|
|  | 1269 | FunctionType *function = safe_dynamic_cast< FunctionType *>( pointer->get_base() ); | 
|---|
| [ae63a18] | 1270 |  | 
|---|
| [01aeade] | 1271 | if ( Expression *newExpr = handleIntrinsics( appExpr ) ) { | 
|---|
|  | 1272 | return newExpr; | 
|---|
|  | 1273 | } // if | 
|---|
| [ae63a18] | 1274 |  | 
|---|
| [01aeade] | 1275 | Expression *ret = appExpr; | 
|---|
| [ae63a18] | 1276 |  | 
|---|
| [01aeade] | 1277 | std::list< Expression *>::iterator arg = appExpr->get_args().begin(); | 
|---|
|  | 1278 | std::list< Expression *>::iterator paramBegin = appExpr->get_args().begin(); | 
|---|
| [ae63a18] | 1279 |  | 
|---|
| [2c57025] | 1280 | TyVarMap exprTyVars( TypeDecl::Data{} ); | 
|---|
| [5c52b06] | 1281 | makeTyVarMap( function, exprTyVars ); | 
|---|
| [3bb195cb] | 1282 | ReferenceToType *dynRetType = isDynRet( function, exprTyVars ); | 
|---|
| [d9fa60a] | 1283 | Type *concRetType = appExpr->get_result();// ?: dynRetType; // xxx - is concRetType a good name? | 
|---|
| [5c52b06] | 1284 |  | 
|---|
| [3bb195cb] | 1285 | if ( dynRetType ) { | 
|---|
| [33a7b6d] | 1286 | ret = addDynRetParam( appExpr, function, concRetType, arg ); // xxx - used to use dynRetType instead of concRetType | 
|---|
| [01aeade] | 1287 | } else if ( needsAdapter( function, scopeTyVars ) ) { | 
|---|
| [e56cfdb0] | 1288 | // std::cerr << "needs adapter: "; | 
|---|
| [2e3a379] | 1289 | // printTyVarMap( std::cerr, scopeTyVars ); | 
|---|
|  | 1290 | // std::cerr << *env << std::endl; | 
|---|
| [01aeade] | 1291 | // change the application so it calls the adapter rather than the passed function | 
|---|
|  | 1292 | ret = applyAdapter( appExpr, function, arg, scopeTyVars ); | 
|---|
|  | 1293 | } // if | 
|---|
|  | 1294 | arg = appExpr->get_args().begin(); | 
|---|
| [ae63a18] | 1295 |  | 
|---|
| [33a7b6d] | 1296 | passTypeVars( appExpr, concRetType, arg, exprTyVars ); // xxx - used to use dynRetType instead of concRetType | 
|---|
| [01aeade] | 1297 | addInferredParams( appExpr, function, arg, exprTyVars ); | 
|---|
| [51b73452] | 1298 |  | 
|---|
| [01aeade] | 1299 | arg = paramBegin; | 
|---|
| [ae63a18] | 1300 |  | 
|---|
| [01aeade] | 1301 | boxParams( appExpr, function, arg, exprTyVars ); | 
|---|
|  | 1302 | passAdapters( appExpr, function, exprTyVars ); | 
|---|
| [6c3744e] | 1303 |  | 
|---|
| [01aeade] | 1304 | return ret; | 
|---|
|  | 1305 | } | 
|---|
| [6c3744e] | 1306 |  | 
|---|
| [01aeade] | 1307 | Expression *Pass1::mutate( UntypedExpr *expr ) { | 
|---|
| [906e24d] | 1308 | if ( expr->has_result() && isPolyType( expr->get_result(), scopeTyVars, env ) ) { | 
|---|
| [01aeade] | 1309 | if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->get_function() ) ) { | 
|---|
|  | 1310 | if ( name->get_name() == "*?" ) { | 
|---|
|  | 1311 | Expression *ret = expr->get_args().front(); | 
|---|
|  | 1312 | expr->get_args().clear(); | 
|---|
|  | 1313 | delete expr; | 
|---|
|  | 1314 | return ret->acceptMutator( *this ); | 
|---|
|  | 1315 | } // if | 
|---|
|  | 1316 | } // if | 
|---|
|  | 1317 | } // if | 
|---|
|  | 1318 | return PolyMutator::mutate( expr ); | 
|---|
|  | 1319 | } | 
|---|
| [6c3744e] | 1320 |  | 
|---|
| [01aeade] | 1321 | Expression *Pass1::mutate( AddressExpr *addrExpr ) { | 
|---|
| [906e24d] | 1322 | assert( addrExpr->get_arg()->has_result() && ! addrExpr->get_arg()->get_result()->isVoid() ); | 
|---|
| [cf16f94] | 1323 |  | 
|---|
|  | 1324 | bool needs = false; | 
|---|
|  | 1325 | if ( UntypedExpr *expr = dynamic_cast< UntypedExpr *>( addrExpr->get_arg() ) ) { | 
|---|
| [906e24d] | 1326 | if ( expr->has_result() && isPolyType( expr->get_result(), scopeTyVars, env ) ) { | 
|---|
| [cf16f94] | 1327 | if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->get_function() ) ) { | 
|---|
|  | 1328 | if ( name->get_name() == "*?" ) { | 
|---|
|  | 1329 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr->get_args().front() ) ) { | 
|---|
| [906e24d] | 1330 | assert( appExpr->get_function()->has_result() ); | 
|---|
|  | 1331 | PointerType *pointer = safe_dynamic_cast< PointerType *>( appExpr->get_function()->get_result() ); | 
|---|
|  | 1332 | FunctionType *function = safe_dynamic_cast< FunctionType *>( pointer->get_base() ); | 
|---|
| [cf16f94] | 1333 | needs = needsAdapter( function, scopeTyVars ); | 
|---|
|  | 1334 | } // if | 
|---|
|  | 1335 | } // if | 
|---|
|  | 1336 | } // if | 
|---|
|  | 1337 | } // if | 
|---|
|  | 1338 | } // if | 
|---|
| [fea7ca7] | 1339 | // isPolyType check needs to happen before mutating addrExpr arg, so pull it forward | 
|---|
|  | 1340 | // out of the if condition. | 
|---|
| [906e24d] | 1341 | bool polytype = isPolyType( addrExpr->get_arg()->get_result(), scopeTyVars, env ); | 
|---|
| [01aeade] | 1342 | addrExpr->set_arg( mutateExpression( addrExpr->get_arg() ) ); | 
|---|
| [fea7ca7] | 1343 | if ( polytype || needs ) { | 
|---|
| [01aeade] | 1344 | Expression *ret = addrExpr->get_arg(); | 
|---|
| [906e24d] | 1345 | delete ret->get_result(); | 
|---|
|  | 1346 | ret->set_result( addrExpr->get_result()->clone() ); | 
|---|
| [01aeade] | 1347 | addrExpr->set_arg( 0 ); | 
|---|
|  | 1348 | delete addrExpr; | 
|---|
|  | 1349 | return ret; | 
|---|
|  | 1350 | } else { | 
|---|
|  | 1351 | return addrExpr; | 
|---|
|  | 1352 | } // if | 
|---|
|  | 1353 | } | 
|---|
| [6c3744e] | 1354 |  | 
|---|
| [b10c9959] | 1355 | /// Wraps a function declaration in a new pointer-to-function variable expression | 
|---|
|  | 1356 | VariableExpr *wrapFunctionDecl( DeclarationWithType *functionDecl ) { | 
|---|
|  | 1357 | // line below cloned from FixFunction.cc | 
|---|
| [9799ec8] | 1358 | // xxx - functionObj is never added to a list of declarations... | 
|---|
| [33a7b6d] | 1359 | // alternatively, this function could return a new VariableExpr( functionDecl ) and change the result type of the new expression | 
|---|
| [b10c9959] | 1360 | ObjectDecl *functionObj = new ObjectDecl( functionDecl->get_name(), functionDecl->get_storageClass(), functionDecl->get_linkage(), 0, | 
|---|
|  | 1361 | new PointerType( Type::Qualifiers(), functionDecl->get_type()->clone() ), 0 ); | 
|---|
|  | 1362 | functionObj->set_mangleName( functionDecl->get_mangleName() ); | 
|---|
| [9799ec8] | 1363 | functionObj->set_scopeLevel( functionDecl->get_scopeLevel() ); | 
|---|
| [b10c9959] | 1364 | return new VariableExpr( functionObj ); | 
|---|
|  | 1365 | } | 
|---|
| [70a06f6] | 1366 |  | 
|---|
| [c2ad3c9] | 1367 | /// Finds the operation declaration for a given type in one of the two maps | 
|---|
|  | 1368 | DeclarationWithType* findOpForType( Type *formalType, const ScopedMap< std::string, DeclarationWithType* >& ops, ResolvExpr::TypeMap< DeclarationWithType >& scopedOps ) { | 
|---|
|  | 1369 | if ( TypeInstType *formalTypeInstType = dynamic_cast< TypeInstType* >( formalType ) ) { | 
|---|
|  | 1370 | ScopedMap< std::string, DeclarationWithType *>::const_iterator opIt = ops.find( formalTypeInstType->get_name() ); | 
|---|
|  | 1371 | return opIt == ops.end() ? 0 : opIt->second; | 
|---|
|  | 1372 | } else { | 
|---|
|  | 1373 | return scopedOps.find( formalType ); | 
|---|
|  | 1374 | } | 
|---|
|  | 1375 | } | 
|---|
|  | 1376 |  | 
|---|
|  | 1377 | /// Adds an assertion parameter to the application expression for the actual assertion declaration valued with the assert op | 
|---|
|  | 1378 | void addAssertionFor( ApplicationExpr *appExpr, DeclarationWithType *actualDecl, DeclarationWithType *assertOp ) { | 
|---|
|  | 1379 | appExpr->get_inferParams()[ actualDecl->get_uniqueId() ] | 
|---|
|  | 1380 | = ParamEntry( assertOp->get_uniqueId(), assertOp->get_type()->clone(), actualDecl->get_type()->clone(), wrapFunctionDecl( assertOp ) ); | 
|---|
|  | 1381 | } | 
|---|
| [9799ec8] | 1382 |  | 
|---|
| [cf16f94] | 1383 | Statement * Pass1::mutate( ReturnStmt *returnStmt ) { | 
|---|
|  | 1384 | if ( retval && returnStmt->get_expr() ) { | 
|---|
| [906e24d] | 1385 | assert( returnStmt->get_expr()->has_result() && ! returnStmt->get_expr()->get_result()->isVoid() ); | 
|---|
| [cf16f94] | 1386 | // ***** Code Removal ***** After introducing a temporary variable for all return expressions, the following code appears superfluous. | 
|---|
|  | 1387 | // if ( returnStmt->get_expr()->get_results().front()->get_isLvalue() ) { | 
|---|
| [ae63a18] | 1388 | // by this point, a cast expr on a polymorphic return value is redundant | 
|---|
| [cf16f94] | 1389 | while ( CastExpr *castExpr = dynamic_cast< CastExpr *>( returnStmt->get_expr() ) ) { | 
|---|
|  | 1390 | returnStmt->set_expr( castExpr->get_arg() ); | 
|---|
|  | 1391 | returnStmt->get_expr()->set_env( castExpr->get_env() ); | 
|---|
|  | 1392 | castExpr->set_env( 0 ); | 
|---|
|  | 1393 | castExpr->set_arg( 0 ); | 
|---|
|  | 1394 | delete castExpr; | 
|---|
| [5f6c42c] | 1395 | } //while | 
|---|
| [1194734] | 1396 |  | 
|---|
|  | 1397 | // find assignment operator for (polymorphic) return type | 
|---|
| [b10c9959] | 1398 | ApplicationExpr *assignExpr = 0; | 
|---|
| [1194734] | 1399 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( retval->get_type() ) ) { | 
|---|
| [b10c9959] | 1400 | // find assignment operator for type variable | 
|---|
| [63c0dbf] | 1401 | ScopedMap< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() ); | 
|---|
| [1194734] | 1402 | if ( assignIter == assignOps.end() ) { | 
|---|
|  | 1403 | throw SemanticError( "Attempt to return dtype or ftype object in ", returnStmt->get_expr() ); | 
|---|
|  | 1404 | } // if | 
|---|
| [b10c9959] | 1405 | assignExpr = new ApplicationExpr( new VariableExpr( assignIter->second ) ); | 
|---|
| [1194734] | 1406 | } else if ( ReferenceToType *refType = dynamic_cast< ReferenceToType *>( retval->get_type() ) ) { | 
|---|
| [b10c9959] | 1407 | // find assignment operator for generic type | 
|---|
| [dc12481] | 1408 | DeclarationWithType *functionDecl = scopedAssignOps.find( refType ); | 
|---|
|  | 1409 | if ( ! functionDecl ) { | 
|---|
| [1194734] | 1410 | throw SemanticError( "Attempt to return dtype or ftype generic object in ", returnStmt->get_expr() ); | 
|---|
|  | 1411 | } | 
|---|
| [b10c9959] | 1412 |  | 
|---|
|  | 1413 | // wrap it up in an application expression | 
|---|
|  | 1414 | assignExpr = new ApplicationExpr( wrapFunctionDecl( functionDecl ) ); | 
|---|
|  | 1415 | assignExpr->set_env( env->clone() ); | 
|---|
|  | 1416 |  | 
|---|
|  | 1417 | // find each of its needed secondary assignment operators | 
|---|
|  | 1418 | std::list< Expression* > &tyParams = refType->get_parameters(); | 
|---|
| [8c49c0e] | 1419 | Type::ForallList &forallParams = functionDecl->get_type()->get_forall(); | 
|---|
| [b10c9959] | 1420 | std::list< Expression* >::const_iterator tyIt = tyParams.begin(); | 
|---|
| [8c49c0e] | 1421 | Type::ForallList::const_iterator forallIt = forallParams.begin(); | 
|---|
| [b10c9959] | 1422 | for ( ; tyIt != tyParams.end() && forallIt != forallParams.end(); ++tyIt, ++forallIt ) { | 
|---|
| [ae7014e] | 1423 | // Add appropriate mapping to assignment expression environment | 
|---|
| [b10c9959] | 1424 | TypeExpr *formalTypeExpr = dynamic_cast< TypeExpr* >( *tyIt ); | 
|---|
|  | 1425 | assert( formalTypeExpr && "type parameters must be type expressions" ); | 
|---|
|  | 1426 | Type *formalType = formalTypeExpr->get_type(); | 
|---|
| [ae7014e] | 1427 | assignExpr->get_env()->add( (*forallIt)->get_name(), formalType ); | 
|---|
|  | 1428 |  | 
|---|
| [c2ad3c9] | 1429 | // skip non-otype parameters (ftype/dtype) | 
|---|
| [2c57025] | 1430 | // xxx - should this check whether the type is complete instead? | 
|---|
| [ae7014e] | 1431 | if ( (*forallIt)->get_kind() != TypeDecl::Any ) continue; | 
|---|
|  | 1432 |  | 
|---|
| [c2ad3c9] | 1433 | // find otype operators for formal type | 
|---|
|  | 1434 | DeclarationWithType *assertAssign = findOpForType( formalType, assignOps, scopedAssignOps ); | 
|---|
|  | 1435 | if ( ! assertAssign ) throw SemanticError( "No assignment operation found for ", formalType ); | 
|---|
|  | 1436 |  | 
|---|
|  | 1437 | DeclarationWithType *assertCtor = findOpForType( formalType, ctorOps, scopedCtorOps ); | 
|---|
|  | 1438 | if ( ! assertCtor ) throw SemanticError( "No default constructor found for ", formalType ); | 
|---|
| [b10c9959] | 1439 |  | 
|---|
| [c2ad3c9] | 1440 | DeclarationWithType *assertCopy = findOpForType( formalType, copyOps, scopedCopyOps ); | 
|---|
|  | 1441 | if ( ! assertCopy ) throw SemanticError( "No copy constructor found for ", formalType ); | 
|---|
|  | 1442 |  | 
|---|
|  | 1443 | DeclarationWithType *assertDtor = findOpForType( formalType, dtorOps, scopedDtorOps ); | 
|---|
|  | 1444 | if ( ! assertDtor ) throw SemanticError( "No destructor found for ", formalType ); | 
|---|
| [9799ec8] | 1445 |  | 
|---|
| [c2ad3c9] | 1446 | // add inferred parameters for otype operators to assignment expression | 
|---|
|  | 1447 | // NOTE: Code here assumes that first four assertions are assign op, ctor, copy ctor, dtor, in that order | 
|---|
| [ae7014e] | 1448 | std::list< DeclarationWithType* > &asserts = (*forallIt)->get_assertions(); | 
|---|
| [c2ad3c9] | 1449 | assert( asserts.size() >= 4 && "Type param needs otype operator assertions" ); | 
|---|
|  | 1450 |  | 
|---|
|  | 1451 | std::list< DeclarationWithType* >::iterator actualIt = asserts.begin(); | 
|---|
|  | 1452 | addAssertionFor( assignExpr, *actualIt, assertAssign ); | 
|---|
|  | 1453 | ++actualIt; | 
|---|
|  | 1454 | addAssertionFor( assignExpr, *actualIt, assertCtor ); | 
|---|
|  | 1455 | ++actualIt; | 
|---|
|  | 1456 | addAssertionFor( assignExpr, *actualIt, assertCopy ); | 
|---|
|  | 1457 | ++actualIt; | 
|---|
|  | 1458 | addAssertionFor( assignExpr, *actualIt, assertDtor ); | 
|---|
| [9799ec8] | 1459 |  | 
|---|
| [b10c9959] | 1460 | } | 
|---|
| [1194734] | 1461 | } | 
|---|
| [b10c9959] | 1462 | assert( assignExpr ); | 
|---|
| [1194734] | 1463 |  | 
|---|
|  | 1464 | // replace return statement with appropriate assignment to out parameter | 
|---|
| [cf16f94] | 1465 | Expression *retParm = new NameExpr( retval->get_name() ); | 
|---|
| [906e24d] | 1466 | retParm->set_result( new PointerType( Type::Qualifiers(), retval->get_type()->clone() ) ); | 
|---|
| [cf16f94] | 1467 | assignExpr->get_args().push_back( retParm ); | 
|---|
|  | 1468 | assignExpr->get_args().push_back( returnStmt->get_expr() ); | 
|---|
|  | 1469 | stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( assignExpr ) ) ); | 
|---|
|  | 1470 | // } else { | 
|---|
|  | 1471 | //      useRetval = true; | 
|---|
|  | 1472 | //      stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( returnStmt->get_expr() ) ) ); | 
|---|
|  | 1473 | //      useRetval = false; | 
|---|
|  | 1474 | // } // if | 
|---|
|  | 1475 | returnStmt->set_expr( 0 ); | 
|---|
| [01aeade] | 1476 | } else { | 
|---|
| [cf16f94] | 1477 | returnStmt->set_expr( mutateExpression( returnStmt->get_expr() ) ); | 
|---|
| [01aeade] | 1478 | } // if | 
|---|
| [cf16f94] | 1479 | return returnStmt; | 
|---|
| [01aeade] | 1480 | } | 
|---|
| [6c3744e] | 1481 |  | 
|---|
| [01aeade] | 1482 | Type * Pass1::mutate( PointerType *pointerType ) { | 
|---|
| [6f49cdf] | 1483 | scopeTyVars.beginScope(); | 
|---|
| [01aeade] | 1484 | makeTyVarMap( pointerType, scopeTyVars ); | 
|---|
| [ae63a18] | 1485 |  | 
|---|
| [01aeade] | 1486 | Type *ret = Mutator::mutate( pointerType ); | 
|---|
| [ae63a18] | 1487 |  | 
|---|
| [6f49cdf] | 1488 | scopeTyVars.endScope(); | 
|---|
| [01aeade] | 1489 | return ret; | 
|---|
|  | 1490 | } | 
|---|
| [6c3744e] | 1491 |  | 
|---|
| [01aeade] | 1492 | Type * Pass1::mutate( FunctionType *functionType ) { | 
|---|
| [6f49cdf] | 1493 | scopeTyVars.beginScope(); | 
|---|
| [01aeade] | 1494 | makeTyVarMap( functionType, scopeTyVars ); | 
|---|
| [ae63a18] | 1495 |  | 
|---|
| [01aeade] | 1496 | Type *ret = Mutator::mutate( functionType ); | 
|---|
| [ae63a18] | 1497 |  | 
|---|
| [6f49cdf] | 1498 | scopeTyVars.endScope(); | 
|---|
| [01aeade] | 1499 | return ret; | 
|---|
|  | 1500 | } | 
|---|
| [51b73452] | 1501 |  | 
|---|
| [01aeade] | 1502 | void Pass1::doBeginScope() { | 
|---|
| [6635c74] | 1503 | adapters.beginScope(); | 
|---|
| [1194734] | 1504 | scopedAssignOps.beginScope(); | 
|---|
| [c2ad3c9] | 1505 | scopedCtorOps.beginScope(); | 
|---|
|  | 1506 | scopedCopyOps.beginScope(); | 
|---|
|  | 1507 | scopedDtorOps.beginScope(); | 
|---|
| [01aeade] | 1508 | } | 
|---|
| [b1a6d6b] | 1509 |  | 
|---|
| [01aeade] | 1510 | void Pass1::doEndScope() { | 
|---|
| [6635c74] | 1511 | adapters.endScope(); | 
|---|
| [1194734] | 1512 | scopedAssignOps.endScope(); | 
|---|
| [c2ad3c9] | 1513 | scopedCtorOps.endScope(); | 
|---|
|  | 1514 | scopedCopyOps.endScope(); | 
|---|
|  | 1515 | scopedDtorOps.endScope(); | 
|---|
| [01aeade] | 1516 | } | 
|---|
| [51b73452] | 1517 |  | 
|---|
|  | 1518 | ////////////////////////////////////////// Pass2 //////////////////////////////////////////////////// | 
|---|
|  | 1519 |  | 
|---|
| [01aeade] | 1520 | void Pass2::addAdapters( FunctionType *functionType ) { | 
|---|
|  | 1521 | std::list< DeclarationWithType *> ¶mList = functionType->get_parameters(); | 
|---|
|  | 1522 | std::list< FunctionType *> functions; | 
|---|
|  | 1523 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) { | 
|---|
|  | 1524 | Type *orig = (*arg)->get_type(); | 
|---|
|  | 1525 | findAndReplaceFunction( orig, functions, scopeTyVars, needsAdapter ); | 
|---|
|  | 1526 | (*arg)->set_type( orig ); | 
|---|
|  | 1527 | } | 
|---|
|  | 1528 | std::set< std::string > adaptersDone; | 
|---|
|  | 1529 | for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) { | 
|---|
| [bdf1954] | 1530 | std::string mangleName = mangleAdapterName( *funType, scopeTyVars ); | 
|---|
| [01aeade] | 1531 | if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) { | 
|---|
|  | 1532 | std::string adapterName = makeAdapterName( mangleName ); | 
|---|
| [68cd1ce] | 1533 | paramList.push_front( new ObjectDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) ); | 
|---|
| [01aeade] | 1534 | adaptersDone.insert( adaptersDone.begin(), mangleName ); | 
|---|
|  | 1535 | } | 
|---|
|  | 1536 | } | 
|---|
| [5f6c42c] | 1537 | //  deleteAll( functions ); | 
|---|
| [01aeade] | 1538 | } | 
|---|
| [6c3744e] | 1539 |  | 
|---|
| [01aeade] | 1540 | template< typename DeclClass > | 
|---|
|  | 1541 | DeclClass * Pass2::handleDecl( DeclClass *decl, Type *type ) { | 
|---|
|  | 1542 | DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) ); | 
|---|
| [6c3744e] | 1543 |  | 
|---|
| [01aeade] | 1544 | return ret; | 
|---|
|  | 1545 | } | 
|---|
| [6c3744e] | 1546 |  | 
|---|
| [01aeade] | 1547 | DeclarationWithType * Pass2::mutate( FunctionDecl *functionDecl ) { | 
|---|
|  | 1548 | return handleDecl( functionDecl, functionDecl->get_functionType() ); | 
|---|
|  | 1549 | } | 
|---|
| [6c3744e] | 1550 |  | 
|---|
| [01aeade] | 1551 | ObjectDecl * Pass2::mutate( ObjectDecl *objectDecl ) { | 
|---|
|  | 1552 | return handleDecl( objectDecl, objectDecl->get_type() ); | 
|---|
|  | 1553 | } | 
|---|
| [6c3744e] | 1554 |  | 
|---|
| [01aeade] | 1555 | TypeDecl * Pass2::mutate( TypeDecl *typeDecl ) { | 
|---|
| [2c57025] | 1556 | addToTyVarMap( typeDecl, scopeTyVars ); | 
|---|
| [01aeade] | 1557 | if ( typeDecl->get_base() ) { | 
|---|
|  | 1558 | return handleDecl( typeDecl, typeDecl->get_base() ); | 
|---|
|  | 1559 | } else { | 
|---|
|  | 1560 | return Mutator::mutate( typeDecl ); | 
|---|
|  | 1561 | } | 
|---|
|  | 1562 | } | 
|---|
| [6c3744e] | 1563 |  | 
|---|
| [01aeade] | 1564 | TypedefDecl * Pass2::mutate( TypedefDecl *typedefDecl ) { | 
|---|
|  | 1565 | return handleDecl( typedefDecl, typedefDecl->get_base() ); | 
|---|
|  | 1566 | } | 
|---|
| [6c3744e] | 1567 |  | 
|---|
| [01aeade] | 1568 | Type * Pass2::mutate( PointerType *pointerType ) { | 
|---|
| [6f49cdf] | 1569 | scopeTyVars.beginScope(); | 
|---|
| [01aeade] | 1570 | makeTyVarMap( pointerType, scopeTyVars ); | 
|---|
| [ae63a18] | 1571 |  | 
|---|
| [01aeade] | 1572 | Type *ret = Mutator::mutate( pointerType ); | 
|---|
| [ae63a18] | 1573 |  | 
|---|
| [6f49cdf] | 1574 | scopeTyVars.endScope(); | 
|---|
| [01aeade] | 1575 | return ret; | 
|---|
|  | 1576 | } | 
|---|
| [6c3744e] | 1577 |  | 
|---|
| [01aeade] | 1578 | Type *Pass2::mutate( FunctionType *funcType ) { | 
|---|
| [6f49cdf] | 1579 | scopeTyVars.beginScope(); | 
|---|
| [01aeade] | 1580 | makeTyVarMap( funcType, scopeTyVars ); | 
|---|
| [7754cde] | 1581 |  | 
|---|
|  | 1582 | // move polymorphic return type to parameter list | 
|---|
| [3bb195cb] | 1583 | if ( isDynRet( funcType ) ) { | 
|---|
| [d9fa60a] | 1584 | ObjectDecl *ret = safe_dynamic_cast< ObjectDecl* >( funcType->get_returnVals().front() ); | 
|---|
| [01aeade] | 1585 | ret->set_type( new PointerType( Type::Qualifiers(), ret->get_type() ) ); | 
|---|
|  | 1586 | funcType->get_parameters().push_front( ret ); | 
|---|
|  | 1587 | funcType->get_returnVals().pop_front(); | 
|---|
| [d9fa60a] | 1588 | ret->set_init( nullptr ); // xxx - memory leak? | 
|---|
| [01aeade] | 1589 | } | 
|---|
| [7754cde] | 1590 |  | 
|---|
|  | 1591 | // add size/align and assertions for type parameters to parameter list | 
|---|
| [01aeade] | 1592 | std::list< DeclarationWithType *>::iterator last = funcType->get_parameters().begin(); | 
|---|
|  | 1593 | std::list< DeclarationWithType *> inferredParams; | 
|---|
| [78dd0da] | 1594 | ObjectDecl newObj( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0 ); | 
|---|
| [05d47278] | 1595 | ObjectDecl newPtr( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, | 
|---|
|  | 1596 | new PointerType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ), 0 ); | 
|---|
| [8c49c0e] | 1597 | for ( Type::ForallList::const_iterator tyParm = funcType->get_forall().begin(); tyParm != funcType->get_forall().end(); ++tyParm ) { | 
|---|
| [db0b3ce] | 1598 | ObjectDecl *sizeParm, *alignParm; | 
|---|
|  | 1599 | // add all size and alignment parameters to parameter list | 
|---|
| [2c57025] | 1600 | if ( (*tyParm)->isComplete() ) { | 
|---|
| [78dd0da] | 1601 | TypeInstType parmType( Type::Qualifiers(), (*tyParm)->get_name(), *tyParm ); | 
|---|
| [adc6781] | 1602 | std::string parmName = mangleType( &parmType ); | 
|---|
| [ae63a18] | 1603 |  | 
|---|
| [78dd0da] | 1604 | sizeParm = newObj.clone(); | 
|---|
| [adc6781] | 1605 | sizeParm->set_name( sizeofName( parmName ) ); | 
|---|
| [db0b3ce] | 1606 | last = funcType->get_parameters().insert( last, sizeParm ); | 
|---|
|  | 1607 | ++last; | 
|---|
| [78dd0da] | 1608 |  | 
|---|
|  | 1609 | alignParm = newObj.clone(); | 
|---|
| [adc6781] | 1610 | alignParm->set_name( alignofName( parmName ) ); | 
|---|
| [db0b3ce] | 1611 | last = funcType->get_parameters().insert( last, alignParm ); | 
|---|
| [01aeade] | 1612 | ++last; | 
|---|
|  | 1613 | } | 
|---|
| [e56cfdb0] | 1614 | // move all assertions into parameter list | 
|---|
| [01aeade] | 1615 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyParm)->get_assertions().begin(); assert != (*tyParm)->get_assertions().end(); ++assert ) { | 
|---|
| [f8b961b] | 1616 | //      *assert = (*assert)->acceptMutator( *this ); | 
|---|
| [01aeade] | 1617 | inferredParams.push_back( *assert ); | 
|---|
|  | 1618 | } | 
|---|
|  | 1619 | (*tyParm)->get_assertions().clear(); | 
|---|
|  | 1620 | } | 
|---|
| [7754cde] | 1621 |  | 
|---|
| [5c52b06] | 1622 | // add size/align for generic parameter types to parameter list | 
|---|
| [b18b0b5] | 1623 | std::set< std::string > seenTypes; // sizeofName for generic types we've seen | 
|---|
| [7754cde] | 1624 | for ( std::list< DeclarationWithType* >::const_iterator fnParm = last; fnParm != funcType->get_parameters().end(); ++fnParm ) { | 
|---|
| [4b8f918] | 1625 | Type *polyType = isPolyType( (*fnParm)->get_type(), scopeTyVars ); | 
|---|
|  | 1626 | if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) { | 
|---|
|  | 1627 | std::string typeName = mangleType( polyType ); | 
|---|
| [adc6781] | 1628 | if ( seenTypes.count( typeName ) ) continue; | 
|---|
| [ae63a18] | 1629 |  | 
|---|
| [05d47278] | 1630 | ObjectDecl *sizeParm, *alignParm, *offsetParm; | 
|---|
| [7754cde] | 1631 | sizeParm = newObj.clone(); | 
|---|
| [adc6781] | 1632 | sizeParm->set_name( sizeofName( typeName ) ); | 
|---|
| [7754cde] | 1633 | last = funcType->get_parameters().insert( last, sizeParm ); | 
|---|
|  | 1634 | ++last; | 
|---|
|  | 1635 |  | 
|---|
|  | 1636 | alignParm = newObj.clone(); | 
|---|
| [adc6781] | 1637 | alignParm->set_name( alignofName( typeName ) ); | 
|---|
| [7754cde] | 1638 | last = funcType->get_parameters().insert( last, alignParm ); | 
|---|
|  | 1639 | ++last; | 
|---|
|  | 1640 |  | 
|---|
| [4b8f918] | 1641 | if ( StructInstType *polyBaseStruct = dynamic_cast< StructInstType* >( polyType ) ) { | 
|---|
| [89173242] | 1642 | // NOTE zero-length arrays are illegal in C, so empty structs have no offset array | 
|---|
|  | 1643 | if ( ! polyBaseStruct->get_baseStruct()->get_members().empty() ) { | 
|---|
|  | 1644 | offsetParm = newPtr.clone(); | 
|---|
| [adc6781] | 1645 | offsetParm->set_name( offsetofName( typeName ) ); | 
|---|
| [89173242] | 1646 | last = funcType->get_parameters().insert( last, offsetParm ); | 
|---|
|  | 1647 | ++last; | 
|---|
|  | 1648 | } | 
|---|
| [05d47278] | 1649 | } | 
|---|
|  | 1650 |  | 
|---|
| [adc6781] | 1651 | seenTypes.insert( typeName ); | 
|---|
| [7754cde] | 1652 | } | 
|---|
|  | 1653 | } | 
|---|
|  | 1654 |  | 
|---|
|  | 1655 | // splice assertion parameters into parameter list | 
|---|
| [01aeade] | 1656 | funcType->get_parameters().splice( last, inferredParams ); | 
|---|
|  | 1657 | addAdapters( funcType ); | 
|---|
|  | 1658 | mutateAll( funcType->get_returnVals(), *this ); | 
|---|
|  | 1659 | mutateAll( funcType->get_parameters(), *this ); | 
|---|
| [ae63a18] | 1660 |  | 
|---|
| [6f49cdf] | 1661 | scopeTyVars.endScope(); | 
|---|
| [01aeade] | 1662 | return funcType; | 
|---|
|  | 1663 | } | 
|---|
| [51b73452] | 1664 |  | 
|---|
| [4b8f918] | 1665 | ////////////////////////////////////////// PolyGenericCalculator //////////////////////////////////////////////////// | 
|---|
| [51b73452] | 1666 |  | 
|---|
| [aa19ccf] | 1667 | void PolyGenericCalculator::beginTypeScope( Type *ty ) { | 
|---|
|  | 1668 | scopeTyVars.beginScope(); | 
|---|
|  | 1669 | makeTyVarMap( ty, scopeTyVars ); | 
|---|
|  | 1670 | } | 
|---|
|  | 1671 |  | 
|---|
|  | 1672 | void PolyGenericCalculator::endTypeScope() { | 
|---|
|  | 1673 | scopeTyVars.endScope(); | 
|---|
|  | 1674 | } | 
|---|
| [51b73452] | 1675 |  | 
|---|
| [01aeade] | 1676 | template< typename DeclClass > | 
|---|
| [8a34677] | 1677 | DeclClass * PolyGenericCalculator::handleDecl( DeclClass *decl, Type *type ) { | 
|---|
| [aa19ccf] | 1678 | beginTypeScope( type ); | 
|---|
| [1ba88a0] | 1679 | // knownLayouts.beginScope(); | 
|---|
|  | 1680 | // knownOffsets.beginScope(); | 
|---|
| [ae63a18] | 1681 |  | 
|---|
| [1ba88a0] | 1682 | DeclClass *ret = static_cast< DeclClass *>( Parent::mutate( decl ) ); | 
|---|
| [6c3744e] | 1683 |  | 
|---|
| [1ba88a0] | 1684 | // knownOffsets.endScope(); | 
|---|
|  | 1685 | // knownLayouts.endScope(); | 
|---|
| [aa19ccf] | 1686 | endTypeScope(); | 
|---|
| [01aeade] | 1687 | return ret; | 
|---|
|  | 1688 | } | 
|---|
| [6c3744e] | 1689 |  | 
|---|
| [8a34677] | 1690 | ObjectDecl * PolyGenericCalculator::mutate( ObjectDecl *objectDecl ) { | 
|---|
| [01aeade] | 1691 | return handleDecl( objectDecl, objectDecl->get_type() ); | 
|---|
|  | 1692 | } | 
|---|
| [6c3744e] | 1693 |  | 
|---|
| [8a34677] | 1694 | DeclarationWithType * PolyGenericCalculator::mutate( FunctionDecl *functionDecl ) { | 
|---|
| [1ba88a0] | 1695 | knownLayouts.beginScope(); | 
|---|
|  | 1696 | knownOffsets.beginScope(); | 
|---|
|  | 1697 |  | 
|---|
|  | 1698 | DeclarationWithType * decl = handleDecl( functionDecl, functionDecl->get_functionType() ); | 
|---|
|  | 1699 | knownOffsets.endScope(); | 
|---|
|  | 1700 | knownLayouts.endScope(); | 
|---|
|  | 1701 | return decl; | 
|---|
| [01aeade] | 1702 | } | 
|---|
| [6c3744e] | 1703 |  | 
|---|
| [8a34677] | 1704 | TypedefDecl * PolyGenericCalculator::mutate( TypedefDecl *typedefDecl ) { | 
|---|
| [01aeade] | 1705 | return handleDecl( typedefDecl, typedefDecl->get_base() ); | 
|---|
|  | 1706 | } | 
|---|
| [6c3744e] | 1707 |  | 
|---|
| [8a34677] | 1708 | TypeDecl * PolyGenericCalculator::mutate( TypeDecl *typeDecl ) { | 
|---|
| [2c57025] | 1709 | addToTyVarMap( typeDecl, scopeTyVars ); | 
|---|
| [1ba88a0] | 1710 | return Parent::mutate( typeDecl ); | 
|---|
| [01aeade] | 1711 | } | 
|---|
| [51b73452] | 1712 |  | 
|---|
| [8a34677] | 1713 | Type * PolyGenericCalculator::mutate( PointerType *pointerType ) { | 
|---|
| [aa19ccf] | 1714 | beginTypeScope( pointerType ); | 
|---|
| [ae63a18] | 1715 |  | 
|---|
| [1ba88a0] | 1716 | Type *ret = Parent::mutate( pointerType ); | 
|---|
| [ae63a18] | 1717 |  | 
|---|
| [aa19ccf] | 1718 | endTypeScope(); | 
|---|
| [01aeade] | 1719 | return ret; | 
|---|
|  | 1720 | } | 
|---|
| [6c3744e] | 1721 |  | 
|---|
| [8a34677] | 1722 | Type * PolyGenericCalculator::mutate( FunctionType *funcType ) { | 
|---|
| [aa19ccf] | 1723 | beginTypeScope( funcType ); | 
|---|
| [ae63a18] | 1724 |  | 
|---|
| [8a34677] | 1725 | // make sure that any type information passed into the function is accounted for | 
|---|
|  | 1726 | for ( std::list< DeclarationWithType* >::const_iterator fnParm = funcType->get_parameters().begin(); fnParm != funcType->get_parameters().end(); ++fnParm ) { | 
|---|
|  | 1727 | // condition here duplicates that in Pass2::mutate( FunctionType* ) | 
|---|
| [4b8f918] | 1728 | Type *polyType = isPolyType( (*fnParm)->get_type(), scopeTyVars ); | 
|---|
|  | 1729 | if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) { | 
|---|
|  | 1730 | knownLayouts.insert( mangleType( polyType ) ); | 
|---|
| [8a34677] | 1731 | } | 
|---|
|  | 1732 | } | 
|---|
| [70a06f6] | 1733 |  | 
|---|
| [1ba88a0] | 1734 | Type *ret = Parent::mutate( funcType ); | 
|---|
| [ae63a18] | 1735 |  | 
|---|
| [aa19ccf] | 1736 | endTypeScope(); | 
|---|
| [01aeade] | 1737 | return ret; | 
|---|
| [6c3744e] | 1738 | } | 
|---|
| [51b73452] | 1739 |  | 
|---|
| [8a34677] | 1740 | Statement *PolyGenericCalculator::mutate( DeclStmt *declStmt ) { | 
|---|
| [01aeade] | 1741 | if ( ObjectDecl *objectDecl = dynamic_cast< ObjectDecl *>( declStmt->get_decl() ) ) { | 
|---|
| [8a34677] | 1742 | if ( findGeneric( objectDecl->get_type() ) ) { | 
|---|
| [e01559c] | 1743 | // change initialization of a polymorphic value object | 
|---|
|  | 1744 | // to allocate storage with alloca | 
|---|
| [ffad73a] | 1745 | Type *declType = objectDecl->get_type(); | 
|---|
| [01aeade] | 1746 | UntypedExpr *alloc = new UntypedExpr( new NameExpr( "__builtin_alloca" ) ); | 
|---|
| [adc6781] | 1747 | alloc->get_args().push_back( new NameExpr( sizeofName( mangleType( declType ) ) ) ); | 
|---|
| [e01559c] | 1748 |  | 
|---|
|  | 1749 | delete objectDecl->get_init(); | 
|---|
|  | 1750 |  | 
|---|
|  | 1751 | std::list<Expression*> designators; | 
|---|
| [974906e2] | 1752 | objectDecl->set_init( new SingleInit( alloc, designators, false ) ); // not constructed | 
|---|
| [01aeade] | 1753 | } | 
|---|
|  | 1754 | } | 
|---|
| [1ba88a0] | 1755 | return Parent::mutate( declStmt ); | 
|---|
| [01aeade] | 1756 | } | 
|---|
| [05d47278] | 1757 |  | 
|---|
| [2a4b088] | 1758 | /// Finds the member in the base list that matches the given declaration; returns its index, or -1 if not present | 
|---|
|  | 1759 | long findMember( DeclarationWithType *memberDecl, std::list< Declaration* > &baseDecls ) { | 
|---|
|  | 1760 | long i = 0; | 
|---|
|  | 1761 | for(std::list< Declaration* >::const_iterator decl = baseDecls.begin(); decl != baseDecls.end(); ++decl, ++i ) { | 
|---|
|  | 1762 | if ( memberDecl->get_name() != (*decl)->get_name() ) continue; | 
|---|
|  | 1763 |  | 
|---|
|  | 1764 | if ( DeclarationWithType *declWithType = dynamic_cast< DeclarationWithType* >( *decl ) ) { | 
|---|
| [bed4d37c] | 1765 | if ( memberDecl->get_mangleName().empty() || declWithType->get_mangleName().empty() | 
|---|
|  | 1766 | || memberDecl->get_mangleName() == declWithType->get_mangleName() ) return i; | 
|---|
| [2a4b088] | 1767 | else continue; | 
|---|
|  | 1768 | } else return i; | 
|---|
|  | 1769 | } | 
|---|
|  | 1770 | return -1; | 
|---|
|  | 1771 | } | 
|---|
|  | 1772 |  | 
|---|
|  | 1773 | /// Returns an index expression into the offset array for a type | 
|---|
|  | 1774 | Expression *makeOffsetIndex( Type *objectType, long i ) { | 
|---|
|  | 1775 | std::stringstream offset_namer; | 
|---|
|  | 1776 | offset_namer << i; | 
|---|
|  | 1777 | ConstantExpr *fieldIndex = new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), offset_namer.str() ) ); | 
|---|
|  | 1778 | UntypedExpr *fieldOffset = new UntypedExpr( new NameExpr( "?[?]" ) ); | 
|---|
| [adc6781] | 1779 | fieldOffset->get_args().push_back( new NameExpr( offsetofName( mangleType( objectType ) ) ) ); | 
|---|
| [2a4b088] | 1780 | fieldOffset->get_args().push_back( fieldIndex ); | 
|---|
|  | 1781 | return fieldOffset; | 
|---|
|  | 1782 | } | 
|---|
|  | 1783 |  | 
|---|
|  | 1784 | /// Returns an expression dereferenced n times | 
|---|
|  | 1785 | Expression *makeDerefdVar( Expression *derefdVar, long n ) { | 
|---|
|  | 1786 | for ( int i = 1; i < n; ++i ) { | 
|---|
|  | 1787 | UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) ); | 
|---|
|  | 1788 | derefExpr->get_args().push_back( derefdVar ); | 
|---|
| [10a7775] | 1789 | // xxx - should set results on derefExpr | 
|---|
| [2a4b088] | 1790 | derefdVar = derefExpr; | 
|---|
|  | 1791 | } | 
|---|
|  | 1792 | return derefdVar; | 
|---|
|  | 1793 | } | 
|---|
| [d63eeb0] | 1794 |  | 
|---|
| [8a34677] | 1795 | Expression *PolyGenericCalculator::mutate( MemberExpr *memberExpr ) { | 
|---|
| [05d47278] | 1796 | // mutate, exiting early if no longer MemberExpr | 
|---|
| [1ba88a0] | 1797 | Expression *expr = Parent::mutate( memberExpr ); | 
|---|
| [05d47278] | 1798 | memberExpr = dynamic_cast< MemberExpr* >( expr ); | 
|---|
|  | 1799 | if ( ! memberExpr ) return expr; | 
|---|
|  | 1800 |  | 
|---|
|  | 1801 | // get declaration for base struct, exiting early if not found | 
|---|
| [8488c715] | 1802 | int varDepth; | 
|---|
|  | 1803 | VariableExpr *varExpr = getBaseVar( memberExpr->get_aggregate(), &varDepth ); | 
|---|
| [05d47278] | 1804 | if ( ! varExpr ) return memberExpr; | 
|---|
|  | 1805 | ObjectDecl *objectDecl = dynamic_cast< ObjectDecl* >( varExpr->get_var() ); | 
|---|
|  | 1806 | if ( ! objectDecl ) return memberExpr; | 
|---|
|  | 1807 |  | 
|---|
|  | 1808 | // only mutate member expressions for polymorphic types | 
|---|
| [8488c715] | 1809 | int tyDepth; | 
|---|
|  | 1810 | Type *objectType = hasPolyBase( objectDecl->get_type(), scopeTyVars, &tyDepth ); | 
|---|
| [05d47278] | 1811 | if ( ! objectType ) return memberExpr; | 
|---|
| [8a34677] | 1812 | findGeneric( objectType ); // ensure layout for this type is available | 
|---|
| [05d47278] | 1813 |  | 
|---|
| [ea5daeb] | 1814 | // replace member expression with dynamically-computed layout expression | 
|---|
| [4318107] | 1815 | Expression *newMemberExpr = 0; | 
|---|
| [05d47278] | 1816 | if ( StructInstType *structType = dynamic_cast< StructInstType* >( objectType ) ) { | 
|---|
| [2a4b088] | 1817 | // look up offset index | 
|---|
|  | 1818 | long i = findMember( memberExpr->get_member(), structType->get_baseStruct()->get_members() ); | 
|---|
|  | 1819 | if ( i == -1 ) return memberExpr; | 
|---|
| [05d47278] | 1820 |  | 
|---|
| [2a4b088] | 1821 | // replace member expression with pointer to base plus offset | 
|---|
|  | 1822 | UntypedExpr *fieldLoc = new UntypedExpr( new NameExpr( "?+?" ) ); | 
|---|
|  | 1823 | fieldLoc->get_args().push_back( makeDerefdVar( varExpr->clone(), varDepth ) ); | 
|---|
|  | 1824 | fieldLoc->get_args().push_back( makeOffsetIndex( objectType, i ) ); | 
|---|
| [4318107] | 1825 | newMemberExpr = fieldLoc; | 
|---|
| [98735ef] | 1826 | } else if ( dynamic_cast< UnionInstType* >( objectType ) ) { | 
|---|
| [2a4b088] | 1827 | // union members are all at offset zero, so build appropriately-dereferenced variable | 
|---|
| [4318107] | 1828 | newMemberExpr = makeDerefdVar( varExpr->clone(), varDepth ); | 
|---|
| [2a4b088] | 1829 | } else return memberExpr; | 
|---|
| [4318107] | 1830 | assert( newMemberExpr ); | 
|---|
|  | 1831 |  | 
|---|
| [4067aa8] | 1832 | Type *memberType = memberExpr->get_member()->get_type(); | 
|---|
|  | 1833 | if ( ! isPolyType( memberType, scopeTyVars ) ) { | 
|---|
|  | 1834 | // Not all members of a polymorphic type are themselves of polymorphic type; in this case the member expression should be wrapped and dereferenced to form an lvalue | 
|---|
|  | 1835 | CastExpr *ptrCastExpr = new CastExpr( newMemberExpr, new PointerType( Type::Qualifiers(), memberType->clone() ) ); | 
|---|
| [4318107] | 1836 | UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) ); | 
|---|
|  | 1837 | derefExpr->get_args().push_back( ptrCastExpr ); | 
|---|
|  | 1838 | newMemberExpr = derefExpr; | 
|---|
|  | 1839 | } | 
|---|
|  | 1840 |  | 
|---|
|  | 1841 | delete memberExpr; | 
|---|
|  | 1842 | return newMemberExpr; | 
|---|
| [2a4b088] | 1843 | } | 
|---|
| [05d47278] | 1844 |  | 
|---|
| [8a34677] | 1845 | ObjectDecl *PolyGenericCalculator::makeVar( const std::string &name, Type *type, Initializer *init ) { | 
|---|
|  | 1846 | ObjectDecl *newObj = new ObjectDecl( name, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, type, init ); | 
|---|
|  | 1847 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) ); | 
|---|
|  | 1848 | return newObj; | 
|---|
|  | 1849 | } | 
|---|
|  | 1850 |  | 
|---|
|  | 1851 | void PolyGenericCalculator::addOtypeParamsToLayoutCall( UntypedExpr *layoutCall, const std::list< Type* > &otypeParams ) { | 
|---|
|  | 1852 | for ( std::list< Type* >::const_iterator param = otypeParams.begin(); param != otypeParams.end(); ++param ) { | 
|---|
|  | 1853 | if ( findGeneric( *param ) ) { | 
|---|
|  | 1854 | // push size/align vars for a generic parameter back | 
|---|
| [adc6781] | 1855 | std::string paramName = mangleType( *param ); | 
|---|
|  | 1856 | layoutCall->get_args().push_back( new NameExpr( sizeofName( paramName ) ) ); | 
|---|
|  | 1857 | layoutCall->get_args().push_back( new NameExpr( alignofName( paramName ) ) ); | 
|---|
| [8a34677] | 1858 | } else { | 
|---|
|  | 1859 | layoutCall->get_args().push_back( new SizeofExpr( (*param)->clone() ) ); | 
|---|
|  | 1860 | layoutCall->get_args().push_back( new AlignofExpr( (*param)->clone() ) ); | 
|---|
|  | 1861 | } | 
|---|
|  | 1862 | } | 
|---|
|  | 1863 | } | 
|---|
|  | 1864 |  | 
|---|
|  | 1865 | /// returns true if any of the otype parameters have a dynamic layout and puts all otype parameters in the output list | 
|---|
|  | 1866 | bool findGenericParams( std::list< TypeDecl* > &baseParams, std::list< Expression* > &typeParams, std::list< Type* > &out ) { | 
|---|
|  | 1867 | bool hasDynamicLayout = false; | 
|---|
|  | 1868 |  | 
|---|
|  | 1869 | std::list< TypeDecl* >::const_iterator baseParam = baseParams.begin(); | 
|---|
|  | 1870 | std::list< Expression* >::const_iterator typeParam = typeParams.begin(); | 
|---|
|  | 1871 | for ( ; baseParam != baseParams.end() && typeParam != typeParams.end(); ++baseParam, ++typeParam ) { | 
|---|
|  | 1872 | // skip non-otype parameters | 
|---|
| [2c57025] | 1873 | if ( ! (*baseParam)->isComplete() ) continue; | 
|---|
| [8a34677] | 1874 | TypeExpr *typeExpr = dynamic_cast< TypeExpr* >( *typeParam ); | 
|---|
|  | 1875 | assert( typeExpr && "all otype parameters should be type expressions" ); | 
|---|
|  | 1876 |  | 
|---|
|  | 1877 | Type *type = typeExpr->get_type(); | 
|---|
|  | 1878 | out.push_back( type ); | 
|---|
|  | 1879 | if ( isPolyType( type ) ) hasDynamicLayout = true; | 
|---|
|  | 1880 | } | 
|---|
|  | 1881 | assert( baseParam == baseParams.end() && typeParam == typeParams.end() ); | 
|---|
|  | 1882 |  | 
|---|
|  | 1883 | return hasDynamicLayout; | 
|---|
|  | 1884 | } | 
|---|
|  | 1885 |  | 
|---|
|  | 1886 | bool PolyGenericCalculator::findGeneric( Type *ty ) { | 
|---|
| [c2ad3c9] | 1887 | ty = replaceTypeInst( ty, env ); | 
|---|
| [9799ec8] | 1888 |  | 
|---|
| [8a34677] | 1889 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( ty ) ) { | 
|---|
|  | 1890 | if ( scopeTyVars.find( typeInst->get_name() ) != scopeTyVars.end() ) { | 
|---|
|  | 1891 | // NOTE assumes here that getting put in the scopeTyVars included having the layout variables set | 
|---|
|  | 1892 | return true; | 
|---|
|  | 1893 | } | 
|---|
|  | 1894 | return false; | 
|---|
|  | 1895 | } else if ( StructInstType *structTy = dynamic_cast< StructInstType* >( ty ) ) { | 
|---|
|  | 1896 | // check if this type already has a layout generated for it | 
|---|
| [adc6781] | 1897 | std::string typeName = mangleType( ty ); | 
|---|
|  | 1898 | if ( knownLayouts.find( typeName ) != knownLayouts.end() ) return true; | 
|---|
| [8a34677] | 1899 |  | 
|---|
|  | 1900 | // check if any of the type parameters have dynamic layout; if none do, this type is (or will be) monomorphized | 
|---|
|  | 1901 | std::list< Type* > otypeParams; | 
|---|
|  | 1902 | if ( ! findGenericParams( *structTy->get_baseParameters(), structTy->get_parameters(), otypeParams ) ) return false; | 
|---|
|  | 1903 |  | 
|---|
|  | 1904 | // insert local variables for layout and generate call to layout function | 
|---|
| [adc6781] | 1905 | knownLayouts.insert( typeName );  // done early so as not to interfere with the later addition of parameters to the layout call | 
|---|
| [8a34677] | 1906 | Type *layoutType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); | 
|---|
|  | 1907 |  | 
|---|
|  | 1908 | int n_members = structTy->get_baseStruct()->get_members().size(); | 
|---|
|  | 1909 | if ( n_members == 0 ) { | 
|---|
|  | 1910 | // all empty structs have the same layout - size 1, align 1 | 
|---|
| [cc3528f] | 1911 | makeVar( sizeofName( typeName ), layoutType, new SingleInit( new ConstantExpr( Constant::from_ulong( (unsigned long)1 ) ) ) ); | 
|---|
|  | 1912 | makeVar( alignofName( typeName ), layoutType->clone(), new SingleInit( new ConstantExpr( Constant::from_ulong( (unsigned long)1 ) ) ) ); | 
|---|
| [8a34677] | 1913 | // NOTE zero-length arrays are forbidden in C, so empty structs have no offsetof array | 
|---|
|  | 1914 | } else { | 
|---|
| [adc6781] | 1915 | ObjectDecl *sizeVar = makeVar( sizeofName( typeName ), layoutType ); | 
|---|
|  | 1916 | ObjectDecl *alignVar = makeVar( alignofName( typeName ), layoutType->clone() ); | 
|---|
| [cb4c607] | 1917 | ObjectDecl *offsetVar = makeVar( offsetofName( typeName ), new ArrayType( Type::Qualifiers(), layoutType->clone(), new ConstantExpr( Constant::from_int( n_members ) ), false, false ) ); | 
|---|
| [8a34677] | 1918 |  | 
|---|
|  | 1919 | // generate call to layout function | 
|---|
| [adc6781] | 1920 | UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( layoutofName( structTy->get_baseStruct() ) ) ); | 
|---|
| [8a34677] | 1921 | layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( sizeVar ) ) ); | 
|---|
|  | 1922 | layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( alignVar ) ) ); | 
|---|
|  | 1923 | layoutCall->get_args().push_back( new VariableExpr( offsetVar ) ); | 
|---|
|  | 1924 | addOtypeParamsToLayoutCall( layoutCall, otypeParams ); | 
|---|
|  | 1925 |  | 
|---|
|  | 1926 | stmtsToAdd.push_back( new ExprStmt( noLabels, layoutCall ) ); | 
|---|
|  | 1927 | } | 
|---|
|  | 1928 |  | 
|---|
|  | 1929 | return true; | 
|---|
|  | 1930 | } else if ( UnionInstType *unionTy = dynamic_cast< UnionInstType* >( ty ) ) { | 
|---|
|  | 1931 | // check if this type already has a layout generated for it | 
|---|
| [adc6781] | 1932 | std::string typeName = mangleType( ty ); | 
|---|
|  | 1933 | if ( knownLayouts.find( typeName ) != knownLayouts.end() ) return true; | 
|---|
| [8a34677] | 1934 |  | 
|---|
|  | 1935 | // check if any of the type parameters have dynamic layout; if none do, this type is (or will be) monomorphized | 
|---|
|  | 1936 | std::list< Type* > otypeParams; | 
|---|
|  | 1937 | if ( ! findGenericParams( *unionTy->get_baseParameters(), unionTy->get_parameters(), otypeParams ) ) return false; | 
|---|
|  | 1938 |  | 
|---|
|  | 1939 | // insert local variables for layout and generate call to layout function | 
|---|
| [adc6781] | 1940 | knownLayouts.insert( typeName );  // done early so as not to interfere with the later addition of parameters to the layout call | 
|---|
| [8a34677] | 1941 | Type *layoutType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); | 
|---|
|  | 1942 |  | 
|---|
| [adc6781] | 1943 | ObjectDecl *sizeVar = makeVar( sizeofName( typeName ), layoutType ); | 
|---|
|  | 1944 | ObjectDecl *alignVar = makeVar( alignofName( typeName ), layoutType->clone() ); | 
|---|
| [8a34677] | 1945 |  | 
|---|
|  | 1946 | // generate call to layout function | 
|---|
| [adc6781] | 1947 | UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( layoutofName( unionTy->get_baseUnion() ) ) ); | 
|---|
| [8a34677] | 1948 | layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( sizeVar ) ) ); | 
|---|
|  | 1949 | layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( alignVar ) ) ); | 
|---|
|  | 1950 | addOtypeParamsToLayoutCall( layoutCall, otypeParams ); | 
|---|
|  | 1951 |  | 
|---|
|  | 1952 | stmtsToAdd.push_back( new ExprStmt( noLabels, layoutCall ) ); | 
|---|
|  | 1953 |  | 
|---|
|  | 1954 | return true; | 
|---|
|  | 1955 | } | 
|---|
|  | 1956 |  | 
|---|
|  | 1957 | return false; | 
|---|
|  | 1958 | } | 
|---|
|  | 1959 |  | 
|---|
|  | 1960 | Expression *PolyGenericCalculator::mutate( SizeofExpr *sizeofExpr ) { | 
|---|
|  | 1961 | Type *ty = sizeofExpr->get_type(); | 
|---|
|  | 1962 | if ( findGeneric( ty ) ) { | 
|---|
| [adc6781] | 1963 | Expression *ret = new NameExpr( sizeofName( mangleType( ty ) ) ); | 
|---|
| [8a34677] | 1964 | delete sizeofExpr; | 
|---|
|  | 1965 | return ret; | 
|---|
|  | 1966 | } | 
|---|
|  | 1967 | return sizeofExpr; | 
|---|
|  | 1968 | } | 
|---|
|  | 1969 |  | 
|---|
|  | 1970 | Expression *PolyGenericCalculator::mutate( AlignofExpr *alignofExpr ) { | 
|---|
|  | 1971 | Type *ty = alignofExpr->get_type(); | 
|---|
|  | 1972 | if ( findGeneric( ty ) ) { | 
|---|
| [adc6781] | 1973 | Expression *ret = new NameExpr( alignofName( mangleType( ty ) ) ); | 
|---|
| [8a34677] | 1974 | delete alignofExpr; | 
|---|
|  | 1975 | return ret; | 
|---|
|  | 1976 | } | 
|---|
|  | 1977 | return alignofExpr; | 
|---|
|  | 1978 | } | 
|---|
|  | 1979 |  | 
|---|
|  | 1980 | Expression *PolyGenericCalculator::mutate( OffsetofExpr *offsetofExpr ) { | 
|---|
| [2a4b088] | 1981 | // mutate, exiting early if no longer OffsetofExpr | 
|---|
| [1ba88a0] | 1982 | Expression *expr = Parent::mutate( offsetofExpr ); | 
|---|
| [2a4b088] | 1983 | offsetofExpr = dynamic_cast< OffsetofExpr* >( expr ); | 
|---|
|  | 1984 | if ( ! offsetofExpr ) return expr; | 
|---|
|  | 1985 |  | 
|---|
|  | 1986 | // only mutate expressions for polymorphic structs/unions | 
|---|
| [8a34677] | 1987 | Type *ty = offsetofExpr->get_type(); | 
|---|
|  | 1988 | if ( ! findGeneric( ty ) ) return offsetofExpr; | 
|---|
| [2a4b088] | 1989 |  | 
|---|
|  | 1990 | if ( StructInstType *structType = dynamic_cast< StructInstType* >( ty ) ) { | 
|---|
|  | 1991 | // replace offsetof expression by index into offset array | 
|---|
|  | 1992 | long i = findMember( offsetofExpr->get_member(), structType->get_baseStruct()->get_members() ); | 
|---|
|  | 1993 | if ( i == -1 ) return offsetofExpr; | 
|---|
|  | 1994 |  | 
|---|
|  | 1995 | Expression *offsetInd = makeOffsetIndex( ty, i ); | 
|---|
|  | 1996 | delete offsetofExpr; | 
|---|
|  | 1997 | return offsetInd; | 
|---|
| [5c52b06] | 1998 | } else if ( dynamic_cast< UnionInstType* >( ty ) ) { | 
|---|
| [2a4b088] | 1999 | // all union members are at offset zero | 
|---|
|  | 2000 | delete offsetofExpr; | 
|---|
| [ca35c51] | 2001 | return new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), "0" ) ); | 
|---|
| [2a4b088] | 2002 | } else return offsetofExpr; | 
|---|
| [05d47278] | 2003 | } | 
|---|
|  | 2004 |  | 
|---|
| [8a34677] | 2005 | Expression *PolyGenericCalculator::mutate( OffsetPackExpr *offsetPackExpr ) { | 
|---|
|  | 2006 | StructInstType *ty = offsetPackExpr->get_type(); | 
|---|
|  | 2007 |  | 
|---|
|  | 2008 | Expression *ret = 0; | 
|---|
|  | 2009 | if ( findGeneric( ty ) ) { | 
|---|
|  | 2010 | // pull offset back from generated type information | 
|---|
| [adc6781] | 2011 | ret = new NameExpr( offsetofName( mangleType( ty ) ) ); | 
|---|
| [8a34677] | 2012 | } else { | 
|---|
| [adc6781] | 2013 | std::string offsetName = offsetofName( mangleType( ty ) ); | 
|---|
| [8a34677] | 2014 | if ( knownOffsets.find( offsetName ) != knownOffsets.end() ) { | 
|---|
|  | 2015 | // use the already-generated offsets for this type | 
|---|
|  | 2016 | ret = new NameExpr( offsetName ); | 
|---|
|  | 2017 | } else { | 
|---|
|  | 2018 | knownOffsets.insert( offsetName ); | 
|---|
|  | 2019 |  | 
|---|
|  | 2020 | std::list< Declaration* > &baseMembers = ty->get_baseStruct()->get_members(); | 
|---|
|  | 2021 | Type *offsetType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); | 
|---|
|  | 2022 |  | 
|---|
|  | 2023 | // build initializer list for offset array | 
|---|
|  | 2024 | std::list< Initializer* > inits; | 
|---|
|  | 2025 | for ( std::list< Declaration* >::const_iterator member = baseMembers.begin(); member != baseMembers.end(); ++member ) { | 
|---|
|  | 2026 | DeclarationWithType *memberDecl; | 
|---|
|  | 2027 | if ( DeclarationWithType *origMember = dynamic_cast< DeclarationWithType* >( *member ) ) { | 
|---|
|  | 2028 | memberDecl = origMember->clone(); | 
|---|
|  | 2029 | } else { | 
|---|
|  | 2030 | memberDecl = new ObjectDecl( (*member)->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, offsetType->clone(), 0 ); | 
|---|
|  | 2031 | } | 
|---|
|  | 2032 | inits.push_back( new SingleInit( new OffsetofExpr( ty->clone(), memberDecl ) ) ); | 
|---|
|  | 2033 | } | 
|---|
|  | 2034 |  | 
|---|
|  | 2035 | // build the offset array and replace the pack with a reference to it | 
|---|
| [cb4c607] | 2036 | ObjectDecl *offsetArray = makeVar( offsetName, new ArrayType( Type::Qualifiers(), offsetType, new ConstantExpr( Constant::from_ulong( baseMembers.size() ) ), false, false ), | 
|---|
| [8a34677] | 2037 | new ListInit( inits ) ); | 
|---|
|  | 2038 | ret = new VariableExpr( offsetArray ); | 
|---|
|  | 2039 | } | 
|---|
|  | 2040 | } | 
|---|
|  | 2041 |  | 
|---|
|  | 2042 | delete offsetPackExpr; | 
|---|
|  | 2043 | return ret; | 
|---|
|  | 2044 | } | 
|---|
|  | 2045 |  | 
|---|
|  | 2046 | void PolyGenericCalculator::doBeginScope() { | 
|---|
|  | 2047 | knownLayouts.beginScope(); | 
|---|
|  | 2048 | knownOffsets.beginScope(); | 
|---|
|  | 2049 | } | 
|---|
|  | 2050 |  | 
|---|
|  | 2051 | void PolyGenericCalculator::doEndScope() { | 
|---|
|  | 2052 | knownLayouts.endScope(); | 
|---|
| [adc6781] | 2053 | knownOffsets.endScope(); | 
|---|
| [8a34677] | 2054 | } | 
|---|
|  | 2055 |  | 
|---|
| [05d47278] | 2056 | ////////////////////////////////////////// Pass3 //////////////////////////////////////////////////// | 
|---|
|  | 2057 |  | 
|---|
|  | 2058 | template< typename DeclClass > | 
|---|
|  | 2059 | DeclClass * Pass3::handleDecl( DeclClass *decl, Type *type ) { | 
|---|
| [6f49cdf] | 2060 | scopeTyVars.beginScope(); | 
|---|
| [05d47278] | 2061 | makeTyVarMap( type, scopeTyVars ); | 
|---|
|  | 2062 |  | 
|---|
|  | 2063 | DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) ); | 
|---|
| [f18a711] | 2064 | ScrubTyVars::scrub( decl, scopeTyVars ); | 
|---|
| [05d47278] | 2065 |  | 
|---|
| [6f49cdf] | 2066 | scopeTyVars.endScope(); | 
|---|
| [05d47278] | 2067 | return ret; | 
|---|
|  | 2068 | } | 
|---|
|  | 2069 |  | 
|---|
|  | 2070 | ObjectDecl * Pass3::mutate( ObjectDecl *objectDecl ) { | 
|---|
|  | 2071 | return handleDecl( objectDecl, objectDecl->get_type() ); | 
|---|
|  | 2072 | } | 
|---|
|  | 2073 |  | 
|---|
|  | 2074 | DeclarationWithType * Pass3::mutate( FunctionDecl *functionDecl ) { | 
|---|
|  | 2075 | return handleDecl( functionDecl, functionDecl->get_functionType() ); | 
|---|
|  | 2076 | } | 
|---|
|  | 2077 |  | 
|---|
|  | 2078 | TypedefDecl * Pass3::mutate( TypedefDecl *typedefDecl ) { | 
|---|
|  | 2079 | return handleDecl( typedefDecl, typedefDecl->get_base() ); | 
|---|
|  | 2080 | } | 
|---|
|  | 2081 |  | 
|---|
|  | 2082 | TypeDecl * Pass3::mutate( TypeDecl *typeDecl ) { | 
|---|
|  | 2083 | //   Initializer *init = 0; | 
|---|
|  | 2084 | //   std::list< Expression *> designators; | 
|---|
| [2c57025] | 2085 | //   addToTyVarMap( typeDecl, scopeTyVars ); | 
|---|
| [05d47278] | 2086 | //   if ( typeDecl->get_base() ) { | 
|---|
|  | 2087 | //     init = new SimpleInit( new SizeofExpr( handleDecl( typeDecl, typeDecl->get_base() ) ), designators ); | 
|---|
|  | 2088 | //   } | 
|---|
|  | 2089 | //   return new ObjectDecl( typeDecl->get_name(), Declaration::Extern, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::UnsignedInt ), init ); | 
|---|
|  | 2090 |  | 
|---|
| [2c57025] | 2091 | addToTyVarMap( typeDecl, scopeTyVars ); | 
|---|
| [05d47278] | 2092 | return Mutator::mutate( typeDecl ); | 
|---|
|  | 2093 | } | 
|---|
|  | 2094 |  | 
|---|
|  | 2095 | Type * Pass3::mutate( PointerType *pointerType ) { | 
|---|
| [6f49cdf] | 2096 | scopeTyVars.beginScope(); | 
|---|
| [05d47278] | 2097 | makeTyVarMap( pointerType, scopeTyVars ); | 
|---|
|  | 2098 |  | 
|---|
|  | 2099 | Type *ret = Mutator::mutate( pointerType ); | 
|---|
|  | 2100 |  | 
|---|
| [6f49cdf] | 2101 | scopeTyVars.endScope(); | 
|---|
| [05d47278] | 2102 | return ret; | 
|---|
|  | 2103 | } | 
|---|
|  | 2104 |  | 
|---|
|  | 2105 | Type * Pass3::mutate( FunctionType *functionType ) { | 
|---|
| [6f49cdf] | 2106 | scopeTyVars.beginScope(); | 
|---|
| [05d47278] | 2107 | makeTyVarMap( functionType, scopeTyVars ); | 
|---|
|  | 2108 |  | 
|---|
|  | 2109 | Type *ret = Mutator::mutate( functionType ); | 
|---|
|  | 2110 |  | 
|---|
| [6f49cdf] | 2111 | scopeTyVars.endScope(); | 
|---|
| [05d47278] | 2112 | return ret; | 
|---|
|  | 2113 | } | 
|---|
| [01aeade] | 2114 | } // anonymous namespace | 
|---|
| [51b73452] | 2115 | } // namespace GenPoly | 
|---|
| [01aeade] | 2116 |  | 
|---|
| [51587aa] | 2117 | // Local Variables: // | 
|---|
|  | 2118 | // tab-width: 4 // | 
|---|
|  | 2119 | // mode: c++ // | 
|---|
|  | 2120 | // compile-command: "make install" // | 
|---|
|  | 2121 | // End: // | 
|---|