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