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