source: src/GenPoly/Box.cc@ a6dd5b0

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since a6dd5b0 was ea5daeb, checked in by Aaron Moss <a3moss@…>, 9 years ago

Move generic instantiation earlier

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