1 | //
|
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
|
---|
3 | //
|
---|
4 | // The contents of this file are covered under the licence agreement in the
|
---|
5 | // file "LICENCE" distributed with Cforall.
|
---|
6 | //
|
---|
7 | // Box.cc --
|
---|
8 | //
|
---|
9 | // Author : Richard C. Bilson
|
---|
10 | // Created On : Mon May 18 07:44:20 2015
|
---|
11 | // Last Modified By : Rob Schluntz
|
---|
12 | // Last Modified On : Fri Feb 05 12:23:10 2016
|
---|
13 | // Update Count : 280
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include <set>
|
---|
17 | #include <stack>
|
---|
18 | #include <string>
|
---|
19 | #include <iterator>
|
---|
20 | #include <algorithm>
|
---|
21 | #include <cassert>
|
---|
22 |
|
---|
23 | #include "Box.h"
|
---|
24 | #include "InstantiateGeneric.h"
|
---|
25 | #include "PolyMutator.h"
|
---|
26 | #include "FindFunction.h"
|
---|
27 | #include "ScopedMap.h"
|
---|
28 | #include "ScrubTyVars.h"
|
---|
29 |
|
---|
30 | #include "Parser/ParseNode.h"
|
---|
31 |
|
---|
32 | #include "SynTree/Constant.h"
|
---|
33 | #include "SynTree/Type.h"
|
---|
34 | #include "SynTree/Expression.h"
|
---|
35 | #include "SynTree/Initializer.h"
|
---|
36 | #include "SynTree/Statement.h"
|
---|
37 | #include "SynTree/Mutator.h"
|
---|
38 |
|
---|
39 | #include "ResolvExpr/TypeEnvironment.h"
|
---|
40 |
|
---|
41 | #include "SymTab/Mangler.h"
|
---|
42 |
|
---|
43 | #include "Common/SemanticError.h"
|
---|
44 | #include "Common/UniqueName.h"
|
---|
45 | #include "Common/utility.h"
|
---|
46 |
|
---|
47 | #include <ext/functional> // temporary
|
---|
48 |
|
---|
49 | namespace GenPoly {
|
---|
50 | namespace {
|
---|
51 | const std::list<Label> noLabels;
|
---|
52 |
|
---|
53 | FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars );
|
---|
54 |
|
---|
55 | /// 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
|
---|
56 | class Pass1 : public PolyMutator {
|
---|
57 | public:
|
---|
58 | Pass1();
|
---|
59 | virtual Expression *mutate( ApplicationExpr *appExpr );
|
---|
60 | virtual Expression *mutate( AddressExpr *addrExpr );
|
---|
61 | virtual Expression *mutate( UntypedExpr *expr );
|
---|
62 | virtual DeclarationWithType* mutate( FunctionDecl *functionDecl );
|
---|
63 | virtual TypeDecl *mutate( TypeDecl *typeDecl );
|
---|
64 | virtual Expression *mutate( CommaExpr *commaExpr );
|
---|
65 | virtual Expression *mutate( ConditionalExpr *condExpr );
|
---|
66 | virtual Statement * mutate( ReturnStmt *returnStmt );
|
---|
67 | virtual Type *mutate( PointerType *pointerType );
|
---|
68 | virtual Type * mutate( FunctionType *functionType );
|
---|
69 |
|
---|
70 | virtual void doBeginScope();
|
---|
71 | virtual void doEndScope();
|
---|
72 | private:
|
---|
73 | /// Makes a new temporary array holding the offsets of the fields of `type`, and returns a new variable expression referencing it
|
---|
74 | Expression *makeOffsetArray( StructInstType *type );
|
---|
75 | /// passes extra type parameters into a polymorphic function application
|
---|
76 | void passTypeVars( ApplicationExpr *appExpr, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars );
|
---|
77 | /// wraps a function application with a new temporary for the out-parameter return value
|
---|
78 | Expression *addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg );
|
---|
79 | /// Replaces all the type parameters of a generic type with their concrete equivalents under the current environment
|
---|
80 | void replaceParametersWithConcrete( ApplicationExpr *appExpr, std::list< Expression* >& params );
|
---|
81 | /// Replaces a polymorphic type with its concrete equivalant under the current environment (returns itself if concrete).
|
---|
82 | /// If `doClone` is set to false, will not clone interior types
|
---|
83 | Type *replaceWithConcrete( ApplicationExpr *appExpr, Type *type, bool doClone = true );
|
---|
84 | /// wraps a function application returning a polymorphic type with a new temporary for the out-parameter return value
|
---|
85 | Expression *addPolyRetParam( ApplicationExpr *appExpr, FunctionType *function, ReferenceToType *polyType, std::list< Expression *>::iterator &arg );
|
---|
86 | Expression *applyAdapter( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars );
|
---|
87 | void boxParam( Type *formal, Expression *&arg, const TyVarMap &exprTyVars );
|
---|
88 | void boxParams( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars );
|
---|
89 | void addInferredParams( ApplicationExpr *appExpr, FunctionType *functionType, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars );
|
---|
90 | /// Stores assignment operators from assertion list in local map of assignment operations
|
---|
91 | void findAssignOps( const std::list< TypeDecl *> &forall );
|
---|
92 | void passAdapters( ApplicationExpr *appExpr, FunctionType *functionType, const TyVarMap &exprTyVars );
|
---|
93 | FunctionDecl *makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars );
|
---|
94 | /// Replaces intrinsic operator functions with their arithmetic desugaring
|
---|
95 | Expression *handleIntrinsics( ApplicationExpr *appExpr );
|
---|
96 | /// Inserts a new temporary variable into the current scope with an auto-generated name
|
---|
97 | ObjectDecl *makeTemporary( Type *type );
|
---|
98 |
|
---|
99 | typedef std::map< std::string, DeclarationWithType *> AdapterMap;
|
---|
100 | std::map< std::string, DeclarationWithType *> assignOps;
|
---|
101 | ScopedMap< std::string, DeclarationWithType *> scopedAssignOps;
|
---|
102 | std::stack< AdapterMap > adapters;
|
---|
103 | DeclarationWithType *retval;
|
---|
104 | bool useRetval;
|
---|
105 | UniqueName tempNamer;
|
---|
106 | };
|
---|
107 |
|
---|
108 | /// Moves polymorphic returns in function types to pointer-type parameters, adds type size and assertion parameters to parameter lists as well
|
---|
109 | class Pass2 : public PolyMutator {
|
---|
110 | public:
|
---|
111 | template< typename DeclClass >
|
---|
112 | DeclClass *handleDecl( DeclClass *decl, Type *type );
|
---|
113 | virtual DeclarationWithType *mutate( FunctionDecl *functionDecl );
|
---|
114 | virtual ObjectDecl *mutate( ObjectDecl *objectDecl );
|
---|
115 | virtual TypeDecl *mutate( TypeDecl *typeDecl );
|
---|
116 | virtual TypedefDecl *mutate( TypedefDecl *typedefDecl );
|
---|
117 | virtual Type *mutate( PointerType *pointerType );
|
---|
118 | virtual Type *mutate( FunctionType *funcType );
|
---|
119 | private:
|
---|
120 | void addAdapters( FunctionType *functionType );
|
---|
121 |
|
---|
122 | std::map< UniqueId, std::string > adapterName;
|
---|
123 | };
|
---|
124 |
|
---|
125 | /// Replaces member expressions for polymorphic types with calculated add-field-offset-and-dereference;
|
---|
126 | /// also fixes offsetof expressions.
|
---|
127 | class MemberExprFixer : public PolyMutator {
|
---|
128 | public:
|
---|
129 | template< typename DeclClass >
|
---|
130 | DeclClass *handleDecl( DeclClass *decl, Type *type );
|
---|
131 | virtual DeclarationWithType *mutate( FunctionDecl *functionDecl );
|
---|
132 | virtual ObjectDecl *mutate( ObjectDecl *objectDecl );
|
---|
133 | virtual TypedefDecl *mutate( TypedefDecl *objectDecl );
|
---|
134 | virtual TypeDecl *mutate( TypeDecl *objectDecl );
|
---|
135 | virtual Statement *mutate( DeclStmt *declStmt );
|
---|
136 | virtual Type *mutate( PointerType *pointerType );
|
---|
137 | virtual Type *mutate( FunctionType *funcType );
|
---|
138 | virtual Expression *mutate( MemberExpr *memberExpr );
|
---|
139 | virtual Expression *mutate( OffsetofExpr *offsetofExpr );
|
---|
140 | };
|
---|
141 |
|
---|
142 | /// 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
|
---|
143 | class Pass3 : public PolyMutator {
|
---|
144 | public:
|
---|
145 | template< typename DeclClass >
|
---|
146 | DeclClass *handleDecl( DeclClass *decl, Type *type );
|
---|
147 | virtual DeclarationWithType *mutate( FunctionDecl *functionDecl );
|
---|
148 | virtual ObjectDecl *mutate( ObjectDecl *objectDecl );
|
---|
149 | virtual TypedefDecl *mutate( TypedefDecl *objectDecl );
|
---|
150 | virtual TypeDecl *mutate( TypeDecl *objectDecl );
|
---|
151 | virtual Type *mutate( PointerType *pointerType );
|
---|
152 | virtual Type *mutate( FunctionType *funcType );
|
---|
153 | private:
|
---|
154 | };
|
---|
155 |
|
---|
156 | } // anonymous namespace
|
---|
157 |
|
---|
158 | void printAllNotBuiltin( const std::list< Declaration *>& translationUnit, std::ostream &os ) {
|
---|
159 | for ( std::list< Declaration *>::const_iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) {
|
---|
160 | if ( ! LinkageSpec::isBuiltin( (*i)->get_linkage() ) ) {
|
---|
161 | (*i)->print( os );
|
---|
162 | os << std::endl;
|
---|
163 | } // if
|
---|
164 | } // for
|
---|
165 | }
|
---|
166 |
|
---|
167 | /// version of mutateAll with special handling for translation unit so you can check the end of the prelude when debugging
|
---|
168 | template< typename MutatorType >
|
---|
169 | inline void mutateTranslationUnit( std::list< Declaration* > &translationUnit, MutatorType &mutator ) {
|
---|
170 | bool seenIntrinsic = false;
|
---|
171 | SemanticError errors;
|
---|
172 | for ( typename std::list< Declaration* >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) {
|
---|
173 | try {
|
---|
174 | if ( *i ) {
|
---|
175 | if ( (*i)->get_linkage() == LinkageSpec::Intrinsic ) {
|
---|
176 | seenIntrinsic = true;
|
---|
177 | } else if ( seenIntrinsic ) {
|
---|
178 | seenIntrinsic = false; // break on this line when debugging for end of prelude
|
---|
179 | }
|
---|
180 |
|
---|
181 | *i = dynamic_cast< Declaration* >( (*i)->acceptMutator( mutator ) );
|
---|
182 | assert( *i );
|
---|
183 | } // if
|
---|
184 | } catch( SemanticError &e ) {
|
---|
185 | errors.append( e );
|
---|
186 | } // try
|
---|
187 | } // for
|
---|
188 | if ( ! errors.isEmpty() ) {
|
---|
189 | throw errors;
|
---|
190 | } // if
|
---|
191 | }
|
---|
192 |
|
---|
193 | void box( std::list< Declaration *>& translationUnit ) {
|
---|
194 | Pass1 pass1;
|
---|
195 | Pass2 pass2;
|
---|
196 | MemberExprFixer memberFixer;
|
---|
197 | Pass3 pass3;
|
---|
198 | mutateTranslationUnit/*All*/( translationUnit, pass1 );
|
---|
199 | mutateTranslationUnit/*All*/( translationUnit, pass2 );
|
---|
200 | instantiateGeneric( translationUnit );
|
---|
201 | mutateTranslationUnit/*All*/( translationUnit, memberFixer );
|
---|
202 | mutateTranslationUnit/*All*/( translationUnit, pass3 );
|
---|
203 | }
|
---|
204 |
|
---|
205 | ////////////////////////////////////////// Pass1 ////////////////////////////////////////////////////
|
---|
206 |
|
---|
207 | namespace {
|
---|
208 | std::string makePolyMonoSuffix( FunctionType * function, const TyVarMap &tyVars ) {
|
---|
209 | std::stringstream name;
|
---|
210 |
|
---|
211 | // NOTE: this function previously used isPolyObj, which failed to produce
|
---|
212 | // the correct thing in some situations. It's not clear to me why this wasn't working.
|
---|
213 |
|
---|
214 | // if the return type or a parameter type involved polymorphic types, then the adapter will need
|
---|
215 | // to take those polymorphic types as pointers. Therefore, there can be two different functions
|
---|
216 | // with the same mangled name, so we need to further mangle the names.
|
---|
217 | for ( std::list< DeclarationWithType *>::iterator retval = function->get_returnVals().begin(); retval != function->get_returnVals().end(); ++retval ) {
|
---|
218 | if ( isPolyType( (*retval)->get_type(), tyVars ) ) {
|
---|
219 | name << "P";
|
---|
220 | } else {
|
---|
221 | name << "M";
|
---|
222 | }
|
---|
223 | }
|
---|
224 | name << "_";
|
---|
225 | std::list< DeclarationWithType *> ¶mList = function->get_parameters();
|
---|
226 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) {
|
---|
227 | if ( isPolyType( (*arg)->get_type(), tyVars ) ) {
|
---|
228 | name << "P";
|
---|
229 | } else {
|
---|
230 | name << "M";
|
---|
231 | }
|
---|
232 | } // for
|
---|
233 | return name.str();
|
---|
234 | }
|
---|
235 |
|
---|
236 | std::string mangleAdapterName( FunctionType * function, const TyVarMap &tyVars ) {
|
---|
237 | return SymTab::Mangler::mangle( function ) + makePolyMonoSuffix( function, tyVars );
|
---|
238 | }
|
---|
239 |
|
---|
240 | std::string makeAdapterName( const std::string &mangleName ) {
|
---|
241 | return "_adapter" + mangleName;
|
---|
242 | }
|
---|
243 |
|
---|
244 | Pass1::Pass1() : useRetval( false ), tempNamer( "_temp" ) {
|
---|
245 | adapters.push(AdapterMap());
|
---|
246 | }
|
---|
247 |
|
---|
248 | /// returns T if the given declaration is: (*?=?)(T *, T) for some T (return not checked, but maybe should be), NULL otherwise
|
---|
249 | ReferenceToType *isAssignment( DeclarationWithType *decl ) {
|
---|
250 | if ( decl->get_name() == "?=?" ) {
|
---|
251 | if ( FunctionType *funType = getFunctionType( decl->get_type() ) ) {
|
---|
252 | if ( funType->get_parameters().size() == 2 ) {
|
---|
253 | if ( PointerType *pointer = dynamic_cast< PointerType *>( funType->get_parameters().front()->get_type() ) ) {
|
---|
254 | if ( ReferenceToType *refType = dynamic_cast< ReferenceToType *>( pointer->get_base() ) ) {
|
---|
255 | if ( ReferenceToType *refType2 = dynamic_cast< ReferenceToType *>( funType->get_parameters().back()->get_type() ) ) {
|
---|
256 | if ( refType->get_name() == refType2->get_name() ) {
|
---|
257 | return refType;
|
---|
258 | } // if
|
---|
259 | } // if
|
---|
260 | } // if
|
---|
261 | } // if
|
---|
262 | } // if
|
---|
263 | } // if
|
---|
264 | } // if
|
---|
265 | return 0;
|
---|
266 | }
|
---|
267 |
|
---|
268 | void Pass1::findAssignOps( const std::list< TypeDecl *> &forall ) {
|
---|
269 | // what if a nested function uses an assignment operator?
|
---|
270 | // assignOps.clear();
|
---|
271 | for ( std::list< TypeDecl *>::const_iterator i = forall.begin(); i != forall.end(); ++i ) {
|
---|
272 | for ( std::list< DeclarationWithType *>::const_iterator assert = (*i)->get_assertions().begin(); assert != (*i)->get_assertions().end(); ++assert ) {
|
---|
273 | std::string typeName;
|
---|
274 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( isAssignment( *assert ) ) ) {
|
---|
275 | assignOps[ typeInst->get_name() ] = *assert;
|
---|
276 | } // if
|
---|
277 | } // for
|
---|
278 | } // for
|
---|
279 | }
|
---|
280 |
|
---|
281 | DeclarationWithType *Pass1::mutate( FunctionDecl *functionDecl ) {
|
---|
282 | // if this is a polymorphic assignment function, put it in the map for this scope
|
---|
283 | if ( ReferenceToType *refType = isAssignment( functionDecl ) ) {
|
---|
284 | if ( ! dynamic_cast< TypeInstType* >( refType ) ) {
|
---|
285 | scopedAssignOps.insert( refType->get_name(), functionDecl );
|
---|
286 | }
|
---|
287 | }
|
---|
288 |
|
---|
289 | if ( functionDecl->get_statements() ) { // empty routine body ?
|
---|
290 | doBeginScope();
|
---|
291 | TyVarMap oldtyVars = scopeTyVars;
|
---|
292 | std::map< std::string, DeclarationWithType *> oldassignOps = assignOps;
|
---|
293 | DeclarationWithType *oldRetval = retval;
|
---|
294 | bool oldUseRetval = useRetval;
|
---|
295 |
|
---|
296 | // process polymorphic return value
|
---|
297 | retval = 0;
|
---|
298 | if ( isPolyRet( functionDecl->get_functionType() ) && functionDecl->get_linkage() == LinkageSpec::Cforall ) {
|
---|
299 | retval = functionDecl->get_functionType()->get_returnVals().front();
|
---|
300 |
|
---|
301 | // give names to unnamed return values
|
---|
302 | if ( retval->get_name() == "" ) {
|
---|
303 | retval->set_name( "_retparm" );
|
---|
304 | retval->set_linkage( LinkageSpec::C );
|
---|
305 | } // if
|
---|
306 | } // if
|
---|
307 |
|
---|
308 | FunctionType *functionType = functionDecl->get_functionType();
|
---|
309 | makeTyVarMap( functionDecl->get_functionType(), scopeTyVars );
|
---|
310 | findAssignOps( functionDecl->get_functionType()->get_forall() );
|
---|
311 |
|
---|
312 | std::list< DeclarationWithType *> ¶mList = functionType->get_parameters();
|
---|
313 | std::list< FunctionType *> functions;
|
---|
314 | for ( std::list< TypeDecl *>::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) {
|
---|
315 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) {
|
---|
316 | findFunction( (*assert)->get_type(), functions, scopeTyVars, needsAdapter );
|
---|
317 | } // for
|
---|
318 | } // for
|
---|
319 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) {
|
---|
320 | findFunction( (*arg)->get_type(), functions, scopeTyVars, needsAdapter );
|
---|
321 | } // for
|
---|
322 |
|
---|
323 | AdapterMap & adapters = Pass1::adapters.top();
|
---|
324 | for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
|
---|
325 | std::string mangleName = mangleAdapterName( *funType, scopeTyVars );
|
---|
326 | if ( adapters.find( mangleName ) == adapters.end() ) {
|
---|
327 | std::string adapterName = makeAdapterName( mangleName );
|
---|
328 | adapters.insert( std::pair< std::string, DeclarationWithType *>( mangleName, new ObjectDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) ) );
|
---|
329 | } // if
|
---|
330 | } // for
|
---|
331 |
|
---|
332 | functionDecl->set_statements( functionDecl->get_statements()->acceptMutator( *this ) );
|
---|
333 |
|
---|
334 | scopeTyVars = oldtyVars;
|
---|
335 | assignOps = oldassignOps;
|
---|
336 | // std::cerr << "end FunctionDecl: ";
|
---|
337 | // for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
|
---|
338 | // std::cerr << i->first << " ";
|
---|
339 | // }
|
---|
340 | // std::cerr << "\n";
|
---|
341 | retval = oldRetval;
|
---|
342 | useRetval = oldUseRetval;
|
---|
343 | doEndScope();
|
---|
344 | } // if
|
---|
345 | return functionDecl;
|
---|
346 | }
|
---|
347 |
|
---|
348 | TypeDecl *Pass1::mutate( TypeDecl *typeDecl ) {
|
---|
349 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
|
---|
350 | return Mutator::mutate( typeDecl );
|
---|
351 | }
|
---|
352 |
|
---|
353 | Expression *Pass1::mutate( CommaExpr *commaExpr ) {
|
---|
354 | bool oldUseRetval = useRetval;
|
---|
355 | useRetval = false;
|
---|
356 | commaExpr->set_arg1( maybeMutate( commaExpr->get_arg1(), *this ) );
|
---|
357 | useRetval = oldUseRetval;
|
---|
358 | commaExpr->set_arg2( maybeMutate( commaExpr->get_arg2(), *this ) );
|
---|
359 | return commaExpr;
|
---|
360 | }
|
---|
361 |
|
---|
362 | Expression *Pass1::mutate( ConditionalExpr *condExpr ) {
|
---|
363 | bool oldUseRetval = useRetval;
|
---|
364 | useRetval = false;
|
---|
365 | condExpr->set_arg1( maybeMutate( condExpr->get_arg1(), *this ) );
|
---|
366 | useRetval = oldUseRetval;
|
---|
367 | condExpr->set_arg2( maybeMutate( condExpr->get_arg2(), *this ) );
|
---|
368 | condExpr->set_arg3( maybeMutate( condExpr->get_arg3(), *this ) );
|
---|
369 | return condExpr;
|
---|
370 |
|
---|
371 | }
|
---|
372 |
|
---|
373 | Expression *Pass1::makeOffsetArray( StructInstType *ty ) {
|
---|
374 | std::list< Declaration* > &baseMembers = ty->get_baseStruct()->get_members();
|
---|
375 |
|
---|
376 | // make a new temporary array
|
---|
377 | Type *offsetType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
|
---|
378 | std::stringstream lenGen;
|
---|
379 | lenGen << baseMembers.size();
|
---|
380 | ConstantExpr *lenExpr = new ConstantExpr( Constant( offsetType->clone(), lenGen.str() ) );
|
---|
381 | ObjectDecl *arrayTemp = makeTemporary( new ArrayType( Type::Qualifiers(), offsetType, lenExpr, false, false ) );
|
---|
382 |
|
---|
383 | // build initializer list for temporary
|
---|
384 | std::list< Initializer* > inits;
|
---|
385 | for ( std::list< Declaration* >::const_iterator member = baseMembers.begin(); member != baseMembers.end(); ++member ) {
|
---|
386 | DeclarationWithType *memberDecl;
|
---|
387 | if ( DeclarationWithType *origMember = dynamic_cast< DeclarationWithType* >( *member ) ) {
|
---|
388 | memberDecl = origMember->clone();
|
---|
389 | } else {
|
---|
390 | memberDecl = new ObjectDecl( (*member)->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, offsetType->clone(), 0 );
|
---|
391 | }
|
---|
392 | inits.push_back( new SingleInit( new OffsetofExpr( ty->clone(), memberDecl ) ) );
|
---|
393 | }
|
---|
394 | arrayTemp->set_init( new ListInit( inits ) );
|
---|
395 |
|
---|
396 | // return variable pointing to temporary
|
---|
397 | return new VariableExpr( arrayTemp );
|
---|
398 | }
|
---|
399 |
|
---|
400 | void Pass1::passTypeVars( ApplicationExpr *appExpr, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) {
|
---|
401 | // pass size/align for type variables
|
---|
402 | for ( TyVarMap::const_iterator tyParm = exprTyVars.begin(); tyParm != exprTyVars.end(); ++tyParm ) {
|
---|
403 | ResolvExpr::EqvClass eqvClass;
|
---|
404 | assert( env );
|
---|
405 | if ( tyParm->second == TypeDecl::Any ) {
|
---|
406 | Type *concrete = env->lookup( tyParm->first );
|
---|
407 | if ( concrete ) {
|
---|
408 | arg = appExpr->get_args().insert( arg, new SizeofExpr( concrete->clone() ) );
|
---|
409 | arg++;
|
---|
410 | arg = appExpr->get_args().insert( arg, new AlignofExpr( concrete->clone() ) );
|
---|
411 | arg++;
|
---|
412 | } else {
|
---|
413 | throw SemanticError( "unbound type variable in application ", appExpr );
|
---|
414 | } // if
|
---|
415 | } // if
|
---|
416 | } // for
|
---|
417 |
|
---|
418 | // add size/align for generic types to parameter list
|
---|
419 | if ( appExpr->get_function()->get_results().empty() ) return;
|
---|
420 | FunctionType *funcType = getFunctionType( appExpr->get_function()->get_results().front() );
|
---|
421 | assert( funcType );
|
---|
422 |
|
---|
423 | std::list< DeclarationWithType* >::const_iterator fnParm = funcType->get_parameters().begin();
|
---|
424 | std::list< Expression* >::const_iterator fnArg = arg;
|
---|
425 | std::set< std::string > seenTypes; //< names for generic types we've seen
|
---|
426 | for ( ; fnParm != funcType->get_parameters().end() && fnArg != appExpr->get_args().end(); ++fnParm, ++fnArg ) {
|
---|
427 | Type *polyBase = hasPolyBase( (*fnParm)->get_type(), exprTyVars );
|
---|
428 | if ( polyBase && ! dynamic_cast< TypeInstType* >( polyBase ) ) {
|
---|
429 | std::string sizeName = sizeofName( polyBase );
|
---|
430 | if ( seenTypes.count( sizeName ) ) continue;
|
---|
431 |
|
---|
432 | VariableExpr *fnArgBase = getBaseVar( *fnArg );
|
---|
433 | assert( fnArgBase && ! fnArgBase->get_results().empty() );
|
---|
434 | Type *argBaseType = fnArgBase->get_results().front();
|
---|
435 | arg = appExpr->get_args().insert( arg, new SizeofExpr( argBaseType->clone() ) );
|
---|
436 | arg++;
|
---|
437 | arg = appExpr->get_args().insert( arg, new AlignofExpr( argBaseType->clone() ) );
|
---|
438 | arg++;
|
---|
439 | if ( dynamic_cast< StructInstType* >( polyBase ) ) {
|
---|
440 | if ( StructInstType *argBaseStructType = dynamic_cast< StructInstType* >( argBaseType ) ) {
|
---|
441 | arg = appExpr->get_args().insert( arg, makeOffsetArray( argBaseStructType ) );
|
---|
442 | arg++;
|
---|
443 | } else {
|
---|
444 | throw SemanticError( "Cannot pass non-struct type for generic struct" );
|
---|
445 | }
|
---|
446 | }
|
---|
447 |
|
---|
448 | seenTypes.insert( sizeName );
|
---|
449 | }
|
---|
450 | }
|
---|
451 | }
|
---|
452 |
|
---|
453 | ObjectDecl *Pass1::makeTemporary( Type *type ) {
|
---|
454 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, type, 0 );
|
---|
455 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
|
---|
456 | return newObj;
|
---|
457 | }
|
---|
458 |
|
---|
459 | Expression *Pass1::addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg ) {
|
---|
460 | // ***** Code Removal ***** After introducing a temporary variable for all return expressions, the following code appears superfluous.
|
---|
461 | // if ( useRetval ) {
|
---|
462 | // assert( retval );
|
---|
463 | // arg = appExpr->get_args().insert( arg, new VariableExpr( retval ) );
|
---|
464 | // arg++;
|
---|
465 | // } else {
|
---|
466 |
|
---|
467 | // Create temporary to hold return value of polymorphic function and produce that temporary as a result
|
---|
468 | // using a comma expression. Possibly change comma expression into statement expression "{}" for multiple
|
---|
469 | // return values.
|
---|
470 | ObjectDecl *newObj = makeTemporary( retType->clone() );
|
---|
471 | Expression *paramExpr = new VariableExpr( newObj );
|
---|
472 | // If the type of the temporary is not polymorphic, box temporary by taking its address; otherwise the
|
---|
473 | // temporary is already boxed and can be used directly.
|
---|
474 | if ( ! isPolyType( newObj->get_type(), scopeTyVars, env ) ) {
|
---|
475 | paramExpr = new AddressExpr( paramExpr );
|
---|
476 | } // if
|
---|
477 | arg = appExpr->get_args().insert( arg, paramExpr ); // add argument to function call
|
---|
478 | arg++;
|
---|
479 | // Build a comma expression to call the function and emulate a normal return.
|
---|
480 | CommaExpr *commaExpr = new CommaExpr( appExpr, new VariableExpr( newObj ) );
|
---|
481 | commaExpr->set_env( appExpr->get_env() );
|
---|
482 | appExpr->set_env( 0 );
|
---|
483 | return commaExpr;
|
---|
484 | // } // if
|
---|
485 | // return appExpr;
|
---|
486 | }
|
---|
487 |
|
---|
488 | void Pass1::replaceParametersWithConcrete( ApplicationExpr *appExpr, std::list< Expression* >& params ) {
|
---|
489 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
|
---|
490 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
|
---|
491 | assert(paramType && "Aggregate parameters should be type expressions");
|
---|
492 | paramType->set_type( replaceWithConcrete( appExpr, paramType->get_type(), false ) );
|
---|
493 | }
|
---|
494 | }
|
---|
495 |
|
---|
496 | Type *Pass1::replaceWithConcrete( ApplicationExpr *appExpr, Type *type, bool doClone ) {
|
---|
497 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
|
---|
498 | Type *concrete = env->lookup( typeInst->get_name() );
|
---|
499 | if ( concrete == 0 ) {
|
---|
500 | throw SemanticError( "Unbound type variable " + typeInst->get_name() + " in ", appExpr );
|
---|
501 | } // if
|
---|
502 | return concrete;
|
---|
503 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
|
---|
504 | if ( doClone ) {
|
---|
505 | structType = structType->clone();
|
---|
506 | }
|
---|
507 | replaceParametersWithConcrete( appExpr, structType->get_parameters() );
|
---|
508 | return structType;
|
---|
509 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
|
---|
510 | if ( doClone ) {
|
---|
511 | unionType = unionType->clone();
|
---|
512 | }
|
---|
513 | replaceParametersWithConcrete( appExpr, unionType->get_parameters() );
|
---|
514 | return unionType;
|
---|
515 | }
|
---|
516 | return type;
|
---|
517 | }
|
---|
518 |
|
---|
519 | Expression *Pass1::addPolyRetParam( ApplicationExpr *appExpr, FunctionType *function, ReferenceToType *polyType, std::list< Expression *>::iterator &arg ) {
|
---|
520 | assert( env );
|
---|
521 | Type *concrete = replaceWithConcrete( appExpr, polyType );
|
---|
522 | return addRetParam( appExpr, function, concrete, arg );
|
---|
523 | }
|
---|
524 |
|
---|
525 | Expression *Pass1::applyAdapter( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ) {
|
---|
526 | Expression *ret = appExpr;
|
---|
527 | if ( ! function->get_returnVals().empty() && isPolyType( function->get_returnVals().front()->get_type(), tyVars ) ) {
|
---|
528 | ret = addRetParam( appExpr, function, function->get_returnVals().front()->get_type(), arg );
|
---|
529 | } // if
|
---|
530 | std::string mangleName = mangleAdapterName( function, tyVars );
|
---|
531 | std::string adapterName = makeAdapterName( mangleName );
|
---|
532 |
|
---|
533 | // cast adaptee to void (*)(), since it may have any type inside a polymorphic function
|
---|
534 | Type * adapteeType = new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) );
|
---|
535 | appExpr->get_args().push_front( new CastExpr( appExpr->get_function(), adapteeType ) );
|
---|
536 | appExpr->set_function( new NameExpr( adapterName ) );
|
---|
537 |
|
---|
538 | return ret;
|
---|
539 | }
|
---|
540 |
|
---|
541 | void Pass1::boxParam( Type *param, Expression *&arg, const TyVarMap &exprTyVars ) {
|
---|
542 | assert( ! arg->get_results().empty() );
|
---|
543 | if ( isPolyType( param, exprTyVars ) ) {
|
---|
544 | if ( dynamic_cast< TypeInstType *>( arg->get_results().front() ) ) {
|
---|
545 | // if the argument's type is a type parameter, we don't need to box again!
|
---|
546 | return;
|
---|
547 | } else if ( arg->get_results().front()->get_isLvalue() ) {
|
---|
548 | // VariableExpr and MemberExpr are lvalues
|
---|
549 | arg = new AddressExpr( arg );
|
---|
550 | } else {
|
---|
551 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, arg->get_results().front()->clone(), 0 );
|
---|
552 | newObj->get_type()->get_qualifiers() = Type::Qualifiers(); // TODO: is this right???
|
---|
553 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
|
---|
554 | UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
|
---|
555 | assign->get_args().push_back( new VariableExpr( newObj ) );
|
---|
556 | assign->get_args().push_back( arg );
|
---|
557 | stmtsToAdd.push_back( new ExprStmt( noLabels, assign ) );
|
---|
558 | arg = new AddressExpr( new VariableExpr( newObj ) );
|
---|
559 | } // if
|
---|
560 | } // if
|
---|
561 | }
|
---|
562 |
|
---|
563 | /// cast parameters to polymorphic functions so that types are replaced with
|
---|
564 | /// void * if they are type parameters in the formal type.
|
---|
565 | /// this gets rid of warnings from gcc.
|
---|
566 | void addCast( Expression *&actual, Type *formal, const TyVarMap &tyVars ) {
|
---|
567 | Type * newType = formal->clone();
|
---|
568 | if ( getFunctionType( newType ) ) {
|
---|
569 | newType = ScrubTyVars::scrub( newType, tyVars );
|
---|
570 | actual = new CastExpr( actual, newType );
|
---|
571 | } // if
|
---|
572 | }
|
---|
573 |
|
---|
574 | void Pass1::boxParams( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) {
|
---|
575 | for ( std::list< DeclarationWithType *>::const_iterator param = function->get_parameters().begin(); param != function->get_parameters().end(); ++param, ++arg ) {
|
---|
576 | assert( arg != appExpr->get_args().end() );
|
---|
577 | addCast( *arg, (*param)->get_type(), exprTyVars );
|
---|
578 | boxParam( (*param)->get_type(), *arg, exprTyVars );
|
---|
579 | } // for
|
---|
580 | }
|
---|
581 |
|
---|
582 | void Pass1::addInferredParams( ApplicationExpr *appExpr, FunctionType *functionType, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ) {
|
---|
583 | std::list< Expression *>::iterator cur = arg;
|
---|
584 | for ( std::list< TypeDecl *>::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) {
|
---|
585 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) {
|
---|
586 | InferredParams::const_iterator inferParam = appExpr->get_inferParams().find( (*assert)->get_uniqueId() );
|
---|
587 | assert( inferParam != appExpr->get_inferParams().end() && "NOTE: Explicit casts of polymorphic functions to compatible monomorphic functions are currently unsupported" );
|
---|
588 | Expression *newExpr = inferParam->second.expr->clone();
|
---|
589 | addCast( newExpr, (*assert)->get_type(), tyVars );
|
---|
590 | boxParam( (*assert)->get_type(), newExpr, tyVars );
|
---|
591 | appExpr->get_args().insert( cur, newExpr );
|
---|
592 | } // for
|
---|
593 | } // for
|
---|
594 | }
|
---|
595 |
|
---|
596 | void makeRetParm( FunctionType *funcType ) {
|
---|
597 | DeclarationWithType *retParm = funcType->get_returnVals().front();
|
---|
598 |
|
---|
599 | // make a new parameter that is a pointer to the type of the old return value
|
---|
600 | retParm->set_type( new PointerType( Type::Qualifiers(), retParm->get_type() ) );
|
---|
601 | funcType->get_parameters().push_front( retParm );
|
---|
602 |
|
---|
603 | // we don't need the return value any more
|
---|
604 | funcType->get_returnVals().clear();
|
---|
605 | }
|
---|
606 |
|
---|
607 | FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars ) {
|
---|
608 | // actually make the adapter type
|
---|
609 | FunctionType *adapter = adaptee->clone();
|
---|
610 | if ( ! adapter->get_returnVals().empty() && isPolyType( adapter->get_returnVals().front()->get_type(), tyVars ) ) {
|
---|
611 | makeRetParm( adapter );
|
---|
612 | } // if
|
---|
613 | adapter->get_parameters().push_front( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) ), 0 ) );
|
---|
614 | return adapter;
|
---|
615 | }
|
---|
616 |
|
---|
617 | Expression *makeAdapterArg( DeclarationWithType *param, DeclarationWithType *arg, DeclarationWithType *realParam, const TyVarMap &tyVars ) {
|
---|
618 | assert( param );
|
---|
619 | assert( arg );
|
---|
620 | if ( isPolyType( realParam->get_type(), tyVars ) ) {
|
---|
621 | if ( dynamic_cast<TypeInstType *>(arg->get_type()) == NULL ) {
|
---|
622 | UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
|
---|
623 | deref->get_args().push_back( new CastExpr( new VariableExpr( param ), new PointerType( Type::Qualifiers(), arg->get_type()->clone() ) ) );
|
---|
624 | deref->get_results().push_back( arg->get_type()->clone() );
|
---|
625 | return deref;
|
---|
626 | } // if
|
---|
627 | } // if
|
---|
628 | return new VariableExpr( param );
|
---|
629 | }
|
---|
630 |
|
---|
631 | 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 ) {
|
---|
632 | UniqueName paramNamer( "_p" );
|
---|
633 | for ( ; param != paramEnd; ++param, ++arg, ++realParam ) {
|
---|
634 | if ( (*param)->get_name() == "" ) {
|
---|
635 | (*param)->set_name( paramNamer.newName() );
|
---|
636 | (*param)->set_linkage( LinkageSpec::C );
|
---|
637 | } // if
|
---|
638 | adapteeApp->get_args().push_back( makeAdapterArg( *param, *arg, *realParam, tyVars ) );
|
---|
639 | } // for
|
---|
640 | }
|
---|
641 |
|
---|
642 |
|
---|
643 |
|
---|
644 | FunctionDecl *Pass1::makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ) {
|
---|
645 | FunctionType *adapterType = makeAdapterType( adaptee, tyVars );
|
---|
646 | adapterType = ScrubTyVars::scrub( adapterType, tyVars );
|
---|
647 | DeclarationWithType *adapteeDecl = adapterType->get_parameters().front();
|
---|
648 | adapteeDecl->set_name( "_adaptee" );
|
---|
649 | ApplicationExpr *adapteeApp = new ApplicationExpr( new CastExpr( new VariableExpr( adapteeDecl ), new PointerType( Type::Qualifiers(), realType ) ) );
|
---|
650 | Statement *bodyStmt;
|
---|
651 |
|
---|
652 | std::list< TypeDecl *>::iterator tyArg = realType->get_forall().begin();
|
---|
653 | std::list< TypeDecl *>::iterator tyParam = adapterType->get_forall().begin();
|
---|
654 | std::list< TypeDecl *>::iterator realTyParam = adaptee->get_forall().begin();
|
---|
655 | for ( ; tyParam != adapterType->get_forall().end(); ++tyArg, ++tyParam, ++realTyParam ) {
|
---|
656 | assert( tyArg != realType->get_forall().end() );
|
---|
657 | std::list< DeclarationWithType *>::iterator assertArg = (*tyArg)->get_assertions().begin();
|
---|
658 | std::list< DeclarationWithType *>::iterator assertParam = (*tyParam)->get_assertions().begin();
|
---|
659 | std::list< DeclarationWithType *>::iterator realAssertParam = (*realTyParam)->get_assertions().begin();
|
---|
660 | for ( ; assertParam != (*tyParam)->get_assertions().end(); ++assertArg, ++assertParam, ++realAssertParam ) {
|
---|
661 | assert( assertArg != (*tyArg)->get_assertions().end() );
|
---|
662 | adapteeApp->get_args().push_back( makeAdapterArg( *assertParam, *assertArg, *realAssertParam, tyVars ) );
|
---|
663 | } // for
|
---|
664 | } // for
|
---|
665 |
|
---|
666 | std::list< DeclarationWithType *>::iterator arg = realType->get_parameters().begin();
|
---|
667 | std::list< DeclarationWithType *>::iterator param = adapterType->get_parameters().begin();
|
---|
668 | std::list< DeclarationWithType *>::iterator realParam = adaptee->get_parameters().begin();
|
---|
669 | param++; // skip adaptee parameter
|
---|
670 | if ( realType->get_returnVals().empty() ) {
|
---|
671 | addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars );
|
---|
672 | bodyStmt = new ExprStmt( noLabels, adapteeApp );
|
---|
673 | } else if ( isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
|
---|
674 | if ( (*param)->get_name() == "" ) {
|
---|
675 | (*param)->set_name( "_ret" );
|
---|
676 | (*param)->set_linkage( LinkageSpec::C );
|
---|
677 | } // if
|
---|
678 | UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
|
---|
679 | UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
|
---|
680 | deref->get_args().push_back( new CastExpr( new VariableExpr( *param++ ), new PointerType( Type::Qualifiers(), realType->get_returnVals().front()->get_type()->clone() ) ) );
|
---|
681 | assign->get_args().push_back( deref );
|
---|
682 | addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars );
|
---|
683 | assign->get_args().push_back( adapteeApp );
|
---|
684 | bodyStmt = new ExprStmt( noLabels, assign );
|
---|
685 | } else {
|
---|
686 | // adapter for a function that returns a monomorphic value
|
---|
687 | addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars );
|
---|
688 | bodyStmt = new ReturnStmt( noLabels, adapteeApp );
|
---|
689 | } // if
|
---|
690 | CompoundStmt *adapterBody = new CompoundStmt( noLabels );
|
---|
691 | adapterBody->get_kids().push_back( bodyStmt );
|
---|
692 | std::string adapterName = makeAdapterName( mangleName );
|
---|
693 | return new FunctionDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, adapterType, adapterBody, false, false );
|
---|
694 | }
|
---|
695 |
|
---|
696 | void Pass1::passAdapters( ApplicationExpr * appExpr, FunctionType * functionType, const TyVarMap & exprTyVars ) {
|
---|
697 | // collect a list of function types passed as parameters or implicit parameters (assertions)
|
---|
698 | std::list< DeclarationWithType *> ¶mList = functionType->get_parameters();
|
---|
699 | std::list< FunctionType *> functions;
|
---|
700 | for ( std::list< TypeDecl *>::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) {
|
---|
701 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) {
|
---|
702 | findFunction( (*assert)->get_type(), functions, exprTyVars, needsAdapter );
|
---|
703 | } // for
|
---|
704 | } // for
|
---|
705 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) {
|
---|
706 | findFunction( (*arg)->get_type(), functions, exprTyVars, needsAdapter );
|
---|
707 | } // for
|
---|
708 |
|
---|
709 | // parameter function types for which an appropriate adapter has been generated. we cannot use the types
|
---|
710 | // after applying substitutions, since two different parameter types may be unified to the same type
|
---|
711 | std::set< std::string > adaptersDone;
|
---|
712 |
|
---|
713 | for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
|
---|
714 | FunctionType *originalFunction = (*funType)->clone();
|
---|
715 | FunctionType *realFunction = (*funType)->clone();
|
---|
716 | std::string mangleName = SymTab::Mangler::mangle( realFunction );
|
---|
717 |
|
---|
718 | // only attempt to create an adapter or pass one as a parameter if we haven't already done so for this
|
---|
719 | // pre-substitution parameter function type.
|
---|
720 | if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
|
---|
721 | adaptersDone.insert( adaptersDone.begin(), mangleName );
|
---|
722 |
|
---|
723 | // apply substitution to type variables to figure out what the adapter's type should look like
|
---|
724 | assert( env );
|
---|
725 | env->apply( realFunction );
|
---|
726 | mangleName = SymTab::Mangler::mangle( realFunction );
|
---|
727 | mangleName += makePolyMonoSuffix( originalFunction, exprTyVars );
|
---|
728 |
|
---|
729 | AdapterMap & adapters = Pass1::adapters.top();
|
---|
730 | AdapterMap::iterator adapter = adapters.find( mangleName );
|
---|
731 | if ( adapter == adapters.end() ) {
|
---|
732 | // adapter has not been created yet in the current scope, so define it
|
---|
733 | FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars );
|
---|
734 | adapter = adapters.insert( adapters.begin(), std::pair< std::string, DeclarationWithType *>( mangleName, newAdapter ) );
|
---|
735 | stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) );
|
---|
736 | } // if
|
---|
737 | assert( adapter != adapters.end() );
|
---|
738 |
|
---|
739 | // add the appropriate adapter as a parameter
|
---|
740 | appExpr->get_args().push_front( new VariableExpr( adapter->second ) );
|
---|
741 | } // if
|
---|
742 | } // for
|
---|
743 | } // passAdapters
|
---|
744 |
|
---|
745 | Expression *makeIncrDecrExpr( ApplicationExpr *appExpr, Type *polyType, bool isIncr ) {
|
---|
746 | NameExpr *opExpr;
|
---|
747 | if ( isIncr ) {
|
---|
748 | opExpr = new NameExpr( "?+=?" );
|
---|
749 | } else {
|
---|
750 | opExpr = new NameExpr( "?-=?" );
|
---|
751 | } // if
|
---|
752 | UntypedExpr *addAssign = new UntypedExpr( opExpr );
|
---|
753 | if ( AddressExpr *address = dynamic_cast< AddressExpr *>( appExpr->get_args().front() ) ) {
|
---|
754 | addAssign->get_args().push_back( address->get_arg() );
|
---|
755 | } else {
|
---|
756 | addAssign->get_args().push_back( appExpr->get_args().front() );
|
---|
757 | } // if
|
---|
758 | addAssign->get_args().push_back( new NameExpr( sizeofName( polyType ) ) );
|
---|
759 | addAssign->get_results().front() = appExpr->get_results().front()->clone();
|
---|
760 | if ( appExpr->get_env() ) {
|
---|
761 | addAssign->set_env( appExpr->get_env() );
|
---|
762 | appExpr->set_env( 0 );
|
---|
763 | } // if
|
---|
764 | appExpr->get_args().clear();
|
---|
765 | delete appExpr;
|
---|
766 | return addAssign;
|
---|
767 | }
|
---|
768 |
|
---|
769 | Expression *Pass1::handleIntrinsics( ApplicationExpr *appExpr ) {
|
---|
770 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr *>( appExpr->get_function() ) ) {
|
---|
771 | if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic ) {
|
---|
772 | if ( varExpr->get_var()->get_name() == "?[?]" ) {
|
---|
773 | assert( ! appExpr->get_results().empty() );
|
---|
774 | assert( appExpr->get_args().size() == 2 );
|
---|
775 | Type *baseType1 = isPolyPtr( appExpr->get_args().front()->get_results().front(), scopeTyVars, env );
|
---|
776 | Type *baseType2 = isPolyPtr( appExpr->get_args().back()->get_results().front(), scopeTyVars, env );
|
---|
777 | assert( ! baseType1 || ! baseType2 ); // the arguments cannot both be polymorphic pointers
|
---|
778 | UntypedExpr *ret = 0;
|
---|
779 | if ( baseType1 || baseType2 ) { // one of the arguments is a polymorphic pointer
|
---|
780 | ret = new UntypedExpr( new NameExpr( "?+?" ) );
|
---|
781 | } // if
|
---|
782 | if ( baseType1 ) {
|
---|
783 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
|
---|
784 | multiply->get_args().push_back( appExpr->get_args().back() );
|
---|
785 | multiply->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) );
|
---|
786 | ret->get_args().push_back( appExpr->get_args().front() );
|
---|
787 | ret->get_args().push_back( multiply );
|
---|
788 | } else if ( baseType2 ) {
|
---|
789 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
|
---|
790 | multiply->get_args().push_back( appExpr->get_args().front() );
|
---|
791 | multiply->get_args().push_back( new NameExpr( sizeofName( baseType2 ) ) );
|
---|
792 | ret->get_args().push_back( multiply );
|
---|
793 | ret->get_args().push_back( appExpr->get_args().back() );
|
---|
794 | } // if
|
---|
795 | if ( baseType1 || baseType2 ) {
|
---|
796 | ret->get_results().push_front( appExpr->get_results().front()->clone() );
|
---|
797 | if ( appExpr->get_env() ) {
|
---|
798 | ret->set_env( appExpr->get_env() );
|
---|
799 | appExpr->set_env( 0 );
|
---|
800 | } // if
|
---|
801 | appExpr->get_args().clear();
|
---|
802 | delete appExpr;
|
---|
803 | return ret;
|
---|
804 | } // if
|
---|
805 | } else if ( varExpr->get_var()->get_name() == "*?" ) {
|
---|
806 | assert( ! appExpr->get_results().empty() );
|
---|
807 | assert( ! appExpr->get_args().empty() );
|
---|
808 | if ( isPolyType( appExpr->get_results().front(), scopeTyVars, env ) ) {
|
---|
809 | Expression *ret = appExpr->get_args().front();
|
---|
810 | delete ret->get_results().front();
|
---|
811 | ret->get_results().front() = appExpr->get_results().front()->clone();
|
---|
812 | if ( appExpr->get_env() ) {
|
---|
813 | ret->set_env( appExpr->get_env() );
|
---|
814 | appExpr->set_env( 0 );
|
---|
815 | } // if
|
---|
816 | appExpr->get_args().clear();
|
---|
817 | delete appExpr;
|
---|
818 | return ret;
|
---|
819 | } // if
|
---|
820 | } else if ( varExpr->get_var()->get_name() == "?++" || varExpr->get_var()->get_name() == "?--" ) {
|
---|
821 | assert( ! appExpr->get_results().empty() );
|
---|
822 | assert( appExpr->get_args().size() == 1 );
|
---|
823 | if ( Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env ) ) {
|
---|
824 | Type *tempType = appExpr->get_results().front()->clone();
|
---|
825 | if ( env ) {
|
---|
826 | env->apply( tempType );
|
---|
827 | } // if
|
---|
828 | ObjectDecl *newObj = makeTemporary( tempType );
|
---|
829 | VariableExpr *tempExpr = new VariableExpr( newObj );
|
---|
830 | UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) );
|
---|
831 | assignExpr->get_args().push_back( tempExpr->clone() );
|
---|
832 | if ( AddressExpr *address = dynamic_cast< AddressExpr *>( appExpr->get_args().front() ) ) {
|
---|
833 | assignExpr->get_args().push_back( address->get_arg()->clone() );
|
---|
834 | } else {
|
---|
835 | assignExpr->get_args().push_back( appExpr->get_args().front()->clone() );
|
---|
836 | } // if
|
---|
837 | CommaExpr *firstComma = new CommaExpr( assignExpr, makeIncrDecrExpr( appExpr, baseType, varExpr->get_var()->get_name() == "?++" ) );
|
---|
838 | return new CommaExpr( firstComma, tempExpr );
|
---|
839 | } // if
|
---|
840 | } else if ( varExpr->get_var()->get_name() == "++?" || varExpr->get_var()->get_name() == "--?" ) {
|
---|
841 | assert( ! appExpr->get_results().empty() );
|
---|
842 | assert( appExpr->get_args().size() == 1 );
|
---|
843 | if ( Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env ) ) {
|
---|
844 | return makeIncrDecrExpr( appExpr, baseType, varExpr->get_var()->get_name() == "++?" );
|
---|
845 | } // if
|
---|
846 | } else if ( varExpr->get_var()->get_name() == "?+?" || varExpr->get_var()->get_name() == "?-?" ) {
|
---|
847 | assert( ! appExpr->get_results().empty() );
|
---|
848 | assert( appExpr->get_args().size() == 2 );
|
---|
849 | Type *baseType1 = isPolyPtr( appExpr->get_args().front()->get_results().front(), scopeTyVars, env );
|
---|
850 | Type *baseType2 = isPolyPtr( appExpr->get_args().back()->get_results().front(), scopeTyVars, env );
|
---|
851 | if ( baseType1 && baseType2 ) {
|
---|
852 | UntypedExpr *divide = new UntypedExpr( new NameExpr( "?/?" ) );
|
---|
853 | divide->get_args().push_back( appExpr );
|
---|
854 | divide->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) );
|
---|
855 | divide->get_results().push_front( appExpr->get_results().front()->clone() );
|
---|
856 | if ( appExpr->get_env() ) {
|
---|
857 | divide->set_env( appExpr->get_env() );
|
---|
858 | appExpr->set_env( 0 );
|
---|
859 | } // if
|
---|
860 | return divide;
|
---|
861 | } else if ( baseType1 ) {
|
---|
862 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
|
---|
863 | multiply->get_args().push_back( appExpr->get_args().back() );
|
---|
864 | multiply->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) );
|
---|
865 | appExpr->get_args().back() = multiply;
|
---|
866 | } else if ( baseType2 ) {
|
---|
867 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
|
---|
868 | multiply->get_args().push_back( appExpr->get_args().front() );
|
---|
869 | multiply->get_args().push_back( new NameExpr( sizeofName( baseType2 ) ) );
|
---|
870 | appExpr->get_args().front() = multiply;
|
---|
871 | } // if
|
---|
872 | } else if ( varExpr->get_var()->get_name() == "?+=?" || varExpr->get_var()->get_name() == "?-=?" ) {
|
---|
873 | assert( ! appExpr->get_results().empty() );
|
---|
874 | assert( appExpr->get_args().size() == 2 );
|
---|
875 | Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env );
|
---|
876 | if ( baseType ) {
|
---|
877 | UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
|
---|
878 | multiply->get_args().push_back( appExpr->get_args().back() );
|
---|
879 | multiply->get_args().push_back( new NameExpr( sizeofName( baseType ) ) );
|
---|
880 | appExpr->get_args().back() = multiply;
|
---|
881 | } // if
|
---|
882 | } // if
|
---|
883 | return appExpr;
|
---|
884 | } // if
|
---|
885 | } // if
|
---|
886 | return 0;
|
---|
887 | }
|
---|
888 |
|
---|
889 | Expression *Pass1::mutate( ApplicationExpr *appExpr ) {
|
---|
890 | // std::cerr << "mutate appExpr: ";
|
---|
891 | // for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
|
---|
892 | // std::cerr << i->first << " ";
|
---|
893 | // }
|
---|
894 | // std::cerr << "\n";
|
---|
895 | bool oldUseRetval = useRetval;
|
---|
896 | useRetval = false;
|
---|
897 | appExpr->get_function()->acceptMutator( *this );
|
---|
898 | mutateAll( appExpr->get_args(), *this );
|
---|
899 | useRetval = oldUseRetval;
|
---|
900 |
|
---|
901 | assert( ! appExpr->get_function()->get_results().empty() );
|
---|
902 | PointerType *pointer = dynamic_cast< PointerType *>( appExpr->get_function()->get_results().front() );
|
---|
903 | assert( pointer );
|
---|
904 | FunctionType *function = dynamic_cast< FunctionType *>( pointer->get_base() );
|
---|
905 | assert( function );
|
---|
906 |
|
---|
907 | if ( Expression *newExpr = handleIntrinsics( appExpr ) ) {
|
---|
908 | return newExpr;
|
---|
909 | } // if
|
---|
910 |
|
---|
911 | Expression *ret = appExpr;
|
---|
912 |
|
---|
913 | std::list< Expression *>::iterator arg = appExpr->get_args().begin();
|
---|
914 | std::list< Expression *>::iterator paramBegin = appExpr->get_args().begin();
|
---|
915 |
|
---|
916 | if ( ReferenceToType *polyType = isPolyRet( function ) ) {
|
---|
917 | ret = addPolyRetParam( appExpr, function, polyType, arg );
|
---|
918 | } else if ( needsAdapter( function, scopeTyVars ) ) {
|
---|
919 | // std::cerr << "needs adapter: ";
|
---|
920 | // for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
|
---|
921 | // std::cerr << i->first << " ";
|
---|
922 | // }
|
---|
923 | // std::cerr << "\n";
|
---|
924 | // change the application so it calls the adapter rather than the passed function
|
---|
925 | ret = applyAdapter( appExpr, function, arg, scopeTyVars );
|
---|
926 | } // if
|
---|
927 | arg = appExpr->get_args().begin();
|
---|
928 |
|
---|
929 | TyVarMap exprTyVars;
|
---|
930 | makeTyVarMap( function, exprTyVars );
|
---|
931 |
|
---|
932 | passTypeVars( appExpr, arg, exprTyVars );
|
---|
933 | addInferredParams( appExpr, function, arg, exprTyVars );
|
---|
934 |
|
---|
935 | arg = paramBegin;
|
---|
936 |
|
---|
937 | boxParams( appExpr, function, arg, exprTyVars );
|
---|
938 |
|
---|
939 | passAdapters( appExpr, function, exprTyVars );
|
---|
940 |
|
---|
941 | return ret;
|
---|
942 | }
|
---|
943 |
|
---|
944 | Expression *Pass1::mutate( UntypedExpr *expr ) {
|
---|
945 | if ( ! expr->get_results().empty() && isPolyType( expr->get_results().front(), scopeTyVars, env ) ) {
|
---|
946 | if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->get_function() ) ) {
|
---|
947 | if ( name->get_name() == "*?" ) {
|
---|
948 | Expression *ret = expr->get_args().front();
|
---|
949 | expr->get_args().clear();
|
---|
950 | delete expr;
|
---|
951 | return ret->acceptMutator( *this );
|
---|
952 | } // if
|
---|
953 | } // if
|
---|
954 | } // if
|
---|
955 | return PolyMutator::mutate( expr );
|
---|
956 | }
|
---|
957 |
|
---|
958 | Expression *Pass1::mutate( AddressExpr *addrExpr ) {
|
---|
959 | assert( ! addrExpr->get_arg()->get_results().empty() );
|
---|
960 |
|
---|
961 | bool needs = false;
|
---|
962 | if ( UntypedExpr *expr = dynamic_cast< UntypedExpr *>( addrExpr->get_arg() ) ) {
|
---|
963 | if ( ! expr->get_results().empty() && isPolyType( expr->get_results().front(), scopeTyVars, env ) ) {
|
---|
964 | if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->get_function() ) ) {
|
---|
965 | if ( name->get_name() == "*?" ) {
|
---|
966 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr->get_args().front() ) ) {
|
---|
967 | assert( ! appExpr->get_function()->get_results().empty() );
|
---|
968 | PointerType *pointer = dynamic_cast< PointerType *>( appExpr->get_function()->get_results().front() );
|
---|
969 | assert( pointer );
|
---|
970 | FunctionType *function = dynamic_cast< FunctionType *>( pointer->get_base() );
|
---|
971 | assert( function );
|
---|
972 | needs = needsAdapter( function, scopeTyVars );
|
---|
973 | } // if
|
---|
974 | } // if
|
---|
975 | } // if
|
---|
976 | } // if
|
---|
977 | } // if
|
---|
978 | addrExpr->set_arg( mutateExpression( addrExpr->get_arg() ) );
|
---|
979 | if ( isPolyType( addrExpr->get_arg()->get_results().front(), scopeTyVars, env ) || needs ) {
|
---|
980 | Expression *ret = addrExpr->get_arg();
|
---|
981 | delete ret->get_results().front();
|
---|
982 | ret->get_results().front() = addrExpr->get_results().front()->clone();
|
---|
983 | addrExpr->set_arg( 0 );
|
---|
984 | delete addrExpr;
|
---|
985 | return ret;
|
---|
986 | } else {
|
---|
987 | return addrExpr;
|
---|
988 | } // if
|
---|
989 | }
|
---|
990 |
|
---|
991 | Statement * Pass1::mutate( ReturnStmt *returnStmt ) {
|
---|
992 | if ( retval && returnStmt->get_expr() ) {
|
---|
993 | assert( ! returnStmt->get_expr()->get_results().empty() );
|
---|
994 | // ***** Code Removal ***** After introducing a temporary variable for all return expressions, the following code appears superfluous.
|
---|
995 | // if ( returnStmt->get_expr()->get_results().front()->get_isLvalue() ) {
|
---|
996 | // by this point, a cast expr on a polymorphic return value is redundant
|
---|
997 | while ( CastExpr *castExpr = dynamic_cast< CastExpr *>( returnStmt->get_expr() ) ) {
|
---|
998 | returnStmt->set_expr( castExpr->get_arg() );
|
---|
999 | returnStmt->get_expr()->set_env( castExpr->get_env() );
|
---|
1000 | castExpr->set_env( 0 );
|
---|
1001 | castExpr->set_arg( 0 );
|
---|
1002 | delete castExpr;
|
---|
1003 | } //while
|
---|
1004 |
|
---|
1005 | // find assignment operator for (polymorphic) return type
|
---|
1006 | DeclarationWithType *assignDecl = 0;
|
---|
1007 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( retval->get_type() ) ) {
|
---|
1008 | std::map< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() );
|
---|
1009 | if ( assignIter == assignOps.end() ) {
|
---|
1010 | throw SemanticError( "Attempt to return dtype or ftype object in ", returnStmt->get_expr() );
|
---|
1011 | } // if
|
---|
1012 | assignDecl = assignIter->second;
|
---|
1013 | } else if ( ReferenceToType *refType = dynamic_cast< ReferenceToType *>( retval->get_type() ) ) {
|
---|
1014 | ScopedMap< std::string, DeclarationWithType *>::const_iterator assignIter = scopedAssignOps.find( refType->get_name() );
|
---|
1015 | if ( assignIter == scopedAssignOps.end() ) {
|
---|
1016 | throw SemanticError( "Attempt to return dtype or ftype generic object in ", returnStmt->get_expr() );
|
---|
1017 | }
|
---|
1018 | DeclarationWithType *functionDecl = assignIter->second;
|
---|
1019 | // line below cloned from FixFunction.cc
|
---|
1020 | assignDecl = new ObjectDecl( functionDecl->get_name(), functionDecl->get_storageClass(), functionDecl->get_linkage(), 0,
|
---|
1021 | new PointerType( Type::Qualifiers(), functionDecl->get_type()->clone() ), 0 );
|
---|
1022 | assignDecl->set_mangleName( functionDecl->get_mangleName() );
|
---|
1023 | }
|
---|
1024 | assert( assignDecl );
|
---|
1025 |
|
---|
1026 | // replace return statement with appropriate assignment to out parameter
|
---|
1027 | ApplicationExpr *assignExpr = new ApplicationExpr( new VariableExpr( assignDecl ) );
|
---|
1028 | Expression *retParm = new NameExpr( retval->get_name() );
|
---|
1029 | retParm->get_results().push_back( new PointerType( Type::Qualifiers(), retval->get_type()->clone() ) );
|
---|
1030 | assignExpr->get_args().push_back( retParm );
|
---|
1031 | assignExpr->get_args().push_back( returnStmt->get_expr() );
|
---|
1032 | stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( assignExpr ) ) );
|
---|
1033 | // } else {
|
---|
1034 | // useRetval = true;
|
---|
1035 | // stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( returnStmt->get_expr() ) ) );
|
---|
1036 | // useRetval = false;
|
---|
1037 | // } // if
|
---|
1038 | returnStmt->set_expr( 0 );
|
---|
1039 | } else {
|
---|
1040 | returnStmt->set_expr( mutateExpression( returnStmt->get_expr() ) );
|
---|
1041 | } // if
|
---|
1042 | return returnStmt;
|
---|
1043 | }
|
---|
1044 |
|
---|
1045 | Type * Pass1::mutate( PointerType *pointerType ) {
|
---|
1046 | TyVarMap oldtyVars = scopeTyVars;
|
---|
1047 | makeTyVarMap( pointerType, scopeTyVars );
|
---|
1048 |
|
---|
1049 | Type *ret = Mutator::mutate( pointerType );
|
---|
1050 |
|
---|
1051 | scopeTyVars = oldtyVars;
|
---|
1052 | return ret;
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 | Type * Pass1::mutate( FunctionType *functionType ) {
|
---|
1056 | TyVarMap oldtyVars = scopeTyVars;
|
---|
1057 | makeTyVarMap( functionType, scopeTyVars );
|
---|
1058 |
|
---|
1059 | Type *ret = Mutator::mutate( functionType );
|
---|
1060 |
|
---|
1061 | scopeTyVars = oldtyVars;
|
---|
1062 | return ret;
|
---|
1063 | }
|
---|
1064 |
|
---|
1065 | void Pass1::doBeginScope() {
|
---|
1066 | // push a copy of the current map
|
---|
1067 | adapters.push(adapters.top());
|
---|
1068 | scopedAssignOps.beginScope();
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | void Pass1::doEndScope() {
|
---|
1072 | adapters.pop();
|
---|
1073 | scopedAssignOps.endScope();
|
---|
1074 | }
|
---|
1075 |
|
---|
1076 | ////////////////////////////////////////// Pass2 ////////////////////////////////////////////////////
|
---|
1077 |
|
---|
1078 | void Pass2::addAdapters( FunctionType *functionType ) {
|
---|
1079 | std::list< DeclarationWithType *> ¶mList = functionType->get_parameters();
|
---|
1080 | std::list< FunctionType *> functions;
|
---|
1081 | for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) {
|
---|
1082 | Type *orig = (*arg)->get_type();
|
---|
1083 | findAndReplaceFunction( orig, functions, scopeTyVars, needsAdapter );
|
---|
1084 | (*arg)->set_type( orig );
|
---|
1085 | }
|
---|
1086 | std::set< std::string > adaptersDone;
|
---|
1087 | for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
|
---|
1088 | std::string mangleName = mangleAdapterName( *funType, scopeTyVars );
|
---|
1089 | if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
|
---|
1090 | std::string adapterName = makeAdapterName( mangleName );
|
---|
1091 | paramList.push_front( new ObjectDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) );
|
---|
1092 | adaptersDone.insert( adaptersDone.begin(), mangleName );
|
---|
1093 | }
|
---|
1094 | }
|
---|
1095 | // deleteAll( functions );
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | template< typename DeclClass >
|
---|
1099 | DeclClass * Pass2::handleDecl( DeclClass *decl, Type *type ) {
|
---|
1100 | DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) );
|
---|
1101 |
|
---|
1102 | return ret;
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 | DeclarationWithType * Pass2::mutate( FunctionDecl *functionDecl ) {
|
---|
1106 | return handleDecl( functionDecl, functionDecl->get_functionType() );
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | ObjectDecl * Pass2::mutate( ObjectDecl *objectDecl ) {
|
---|
1110 | return handleDecl( objectDecl, objectDecl->get_type() );
|
---|
1111 | }
|
---|
1112 |
|
---|
1113 | TypeDecl * Pass2::mutate( TypeDecl *typeDecl ) {
|
---|
1114 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
|
---|
1115 | if ( typeDecl->get_base() ) {
|
---|
1116 | return handleDecl( typeDecl, typeDecl->get_base() );
|
---|
1117 | } else {
|
---|
1118 | return Mutator::mutate( typeDecl );
|
---|
1119 | }
|
---|
1120 | }
|
---|
1121 |
|
---|
1122 | TypedefDecl * Pass2::mutate( TypedefDecl *typedefDecl ) {
|
---|
1123 | return handleDecl( typedefDecl, typedefDecl->get_base() );
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 | Type * Pass2::mutate( PointerType *pointerType ) {
|
---|
1127 | TyVarMap oldtyVars = scopeTyVars;
|
---|
1128 | makeTyVarMap( pointerType, scopeTyVars );
|
---|
1129 |
|
---|
1130 | Type *ret = Mutator::mutate( pointerType );
|
---|
1131 |
|
---|
1132 | scopeTyVars = oldtyVars;
|
---|
1133 | return ret;
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 | Type *Pass2::mutate( FunctionType *funcType ) {
|
---|
1137 | TyVarMap oldtyVars = scopeTyVars;
|
---|
1138 | makeTyVarMap( funcType, scopeTyVars );
|
---|
1139 |
|
---|
1140 | // move polymorphic return type to parameter list
|
---|
1141 | if ( isPolyRet( funcType ) ) {
|
---|
1142 | DeclarationWithType *ret = funcType->get_returnVals().front();
|
---|
1143 | ret->set_type( new PointerType( Type::Qualifiers(), ret->get_type() ) );
|
---|
1144 | funcType->get_parameters().push_front( ret );
|
---|
1145 | funcType->get_returnVals().pop_front();
|
---|
1146 | }
|
---|
1147 |
|
---|
1148 | // add size/align and assertions for type parameters to parameter list
|
---|
1149 | std::list< DeclarationWithType *>::iterator last = funcType->get_parameters().begin();
|
---|
1150 | std::list< DeclarationWithType *> inferredParams;
|
---|
1151 | ObjectDecl newObj( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0 );
|
---|
1152 | ObjectDecl newPtr( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0,
|
---|
1153 | new PointerType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ), 0 );
|
---|
1154 | // ObjectDecl *newFunPtr = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) ), 0 );
|
---|
1155 | for ( std::list< TypeDecl *>::const_iterator tyParm = funcType->get_forall().begin(); tyParm != funcType->get_forall().end(); ++tyParm ) {
|
---|
1156 | ObjectDecl *sizeParm, *alignParm;
|
---|
1157 | // add all size and alignment parameters to parameter list
|
---|
1158 | if ( (*tyParm)->get_kind() == TypeDecl::Any ) {
|
---|
1159 | TypeInstType parmType( Type::Qualifiers(), (*tyParm)->get_name(), *tyParm );
|
---|
1160 |
|
---|
1161 | sizeParm = newObj.clone();
|
---|
1162 | sizeParm->set_name( sizeofName( &parmType ) );
|
---|
1163 | last = funcType->get_parameters().insert( last, sizeParm );
|
---|
1164 | ++last;
|
---|
1165 |
|
---|
1166 | alignParm = newObj.clone();
|
---|
1167 | alignParm->set_name( alignofName( &parmType ) );
|
---|
1168 | last = funcType->get_parameters().insert( last, alignParm );
|
---|
1169 | ++last;
|
---|
1170 | }
|
---|
1171 | // move all assertions into parameter list
|
---|
1172 | for ( std::list< DeclarationWithType *>::iterator assert = (*tyParm)->get_assertions().begin(); assert != (*tyParm)->get_assertions().end(); ++assert ) {
|
---|
1173 | // *assert = (*assert)->acceptMutator( *this );
|
---|
1174 | inferredParams.push_back( *assert );
|
---|
1175 | }
|
---|
1176 | (*tyParm)->get_assertions().clear();
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | // add size/align for generic types to parameter list
|
---|
1180 | std::set< std::string > seenTypes; // sizeofName for generic types we've seen
|
---|
1181 | for ( std::list< DeclarationWithType* >::const_iterator fnParm = last; fnParm != funcType->get_parameters().end(); ++fnParm ) {
|
---|
1182 | Type *polyBase = hasPolyBase( (*fnParm)->get_type(), scopeTyVars );
|
---|
1183 | if ( polyBase && ! dynamic_cast< TypeInstType* >( polyBase ) ) {
|
---|
1184 | std::string sizeName = sizeofName( polyBase );
|
---|
1185 | if ( seenTypes.count( sizeName ) ) continue;
|
---|
1186 |
|
---|
1187 | ObjectDecl *sizeParm, *alignParm, *offsetParm;
|
---|
1188 | sizeParm = newObj.clone();
|
---|
1189 | sizeParm->set_name( sizeName );
|
---|
1190 | last = funcType->get_parameters().insert( last, sizeParm );
|
---|
1191 | ++last;
|
---|
1192 |
|
---|
1193 | alignParm = newObj.clone();
|
---|
1194 | alignParm->set_name( alignofName( polyBase ) );
|
---|
1195 | last = funcType->get_parameters().insert( last, alignParm );
|
---|
1196 | ++last;
|
---|
1197 |
|
---|
1198 | if ( dynamic_cast< StructInstType* >( polyBase ) ) {
|
---|
1199 | offsetParm = newPtr.clone();
|
---|
1200 | offsetParm->set_name( offsetofName( polyBase ) );
|
---|
1201 | last = funcType->get_parameters().insert( last, offsetParm );
|
---|
1202 | ++last;
|
---|
1203 | }
|
---|
1204 |
|
---|
1205 | seenTypes.insert( sizeName );
|
---|
1206 | }
|
---|
1207 | }
|
---|
1208 |
|
---|
1209 | // splice assertion parameters into parameter list
|
---|
1210 | funcType->get_parameters().splice( last, inferredParams );
|
---|
1211 | addAdapters( funcType );
|
---|
1212 | mutateAll( funcType->get_returnVals(), *this );
|
---|
1213 | mutateAll( funcType->get_parameters(), *this );
|
---|
1214 |
|
---|
1215 | scopeTyVars = oldtyVars;
|
---|
1216 | return funcType;
|
---|
1217 | }
|
---|
1218 |
|
---|
1219 | ////////////////////////////////////////// MemberExprFixer ////////////////////////////////////////////////////
|
---|
1220 |
|
---|
1221 | template< typename DeclClass >
|
---|
1222 | DeclClass * MemberExprFixer::handleDecl( DeclClass *decl, Type *type ) {
|
---|
1223 | TyVarMap oldtyVars = scopeTyVars;
|
---|
1224 | makeTyVarMap( type, scopeTyVars );
|
---|
1225 |
|
---|
1226 | DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) );
|
---|
1227 |
|
---|
1228 | scopeTyVars = oldtyVars;
|
---|
1229 | return ret;
|
---|
1230 | }
|
---|
1231 |
|
---|
1232 | ObjectDecl * MemberExprFixer::mutate( ObjectDecl *objectDecl ) {
|
---|
1233 | return handleDecl( objectDecl, objectDecl->get_type() );
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 | DeclarationWithType * MemberExprFixer::mutate( FunctionDecl *functionDecl ) {
|
---|
1237 | return handleDecl( functionDecl, functionDecl->get_functionType() );
|
---|
1238 | }
|
---|
1239 |
|
---|
1240 | TypedefDecl * MemberExprFixer::mutate( TypedefDecl *typedefDecl ) {
|
---|
1241 | return handleDecl( typedefDecl, typedefDecl->get_base() );
|
---|
1242 | }
|
---|
1243 |
|
---|
1244 | TypeDecl * MemberExprFixer::mutate( TypeDecl *typeDecl ) {
|
---|
1245 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
|
---|
1246 | return Mutator::mutate( typeDecl );
|
---|
1247 | }
|
---|
1248 |
|
---|
1249 | Type * MemberExprFixer::mutate( PointerType *pointerType ) {
|
---|
1250 | TyVarMap oldtyVars = scopeTyVars;
|
---|
1251 | makeTyVarMap( pointerType, scopeTyVars );
|
---|
1252 |
|
---|
1253 | Type *ret = Mutator::mutate( pointerType );
|
---|
1254 |
|
---|
1255 | scopeTyVars = oldtyVars;
|
---|
1256 | return ret;
|
---|
1257 | }
|
---|
1258 |
|
---|
1259 | Type * MemberExprFixer::mutate( FunctionType *functionType ) {
|
---|
1260 | TyVarMap oldtyVars = scopeTyVars;
|
---|
1261 | makeTyVarMap( functionType, scopeTyVars );
|
---|
1262 |
|
---|
1263 | Type *ret = Mutator::mutate( functionType );
|
---|
1264 |
|
---|
1265 | scopeTyVars = oldtyVars;
|
---|
1266 | return ret;
|
---|
1267 | }
|
---|
1268 |
|
---|
1269 | Statement *MemberExprFixer::mutate( DeclStmt *declStmt ) {
|
---|
1270 | if ( ObjectDecl *objectDecl = dynamic_cast< ObjectDecl *>( declStmt->get_decl() ) ) {
|
---|
1271 | if ( isPolyType( objectDecl->get_type(), scopeTyVars ) ) {
|
---|
1272 | // change initialization of a polymorphic value object
|
---|
1273 | // to allocate storage with alloca
|
---|
1274 | Type *declType = objectDecl->get_type();
|
---|
1275 | UntypedExpr *alloc = new UntypedExpr( new NameExpr( "__builtin_alloca" ) );
|
---|
1276 | alloc->get_args().push_back( new NameExpr( sizeofName( declType ) ) );
|
---|
1277 |
|
---|
1278 | delete objectDecl->get_init();
|
---|
1279 |
|
---|
1280 | std::list<Expression*> designators;
|
---|
1281 | objectDecl->set_init( new SingleInit( alloc, designators ) );
|
---|
1282 | }
|
---|
1283 | }
|
---|
1284 | return Mutator::mutate( declStmt );
|
---|
1285 | }
|
---|
1286 |
|
---|
1287 | /// Finds the member in the base list that matches the given declaration; returns its index, or -1 if not present
|
---|
1288 | long findMember( DeclarationWithType *memberDecl, std::list< Declaration* > &baseDecls ) {
|
---|
1289 | long i = 0;
|
---|
1290 | for(std::list< Declaration* >::const_iterator decl = baseDecls.begin(); decl != baseDecls.end(); ++decl, ++i ) {
|
---|
1291 | if ( memberDecl->get_name() != (*decl)->get_name() ) continue;
|
---|
1292 |
|
---|
1293 | if ( DeclarationWithType *declWithType = dynamic_cast< DeclarationWithType* >( *decl ) ) {
|
---|
1294 | if ( memberDecl->get_mangleName() == declWithType->get_mangleName() ) return i;
|
---|
1295 | else continue;
|
---|
1296 | } else return i;
|
---|
1297 | }
|
---|
1298 | return -1;
|
---|
1299 | }
|
---|
1300 |
|
---|
1301 | /// Returns an index expression into the offset array for a type
|
---|
1302 | Expression *makeOffsetIndex( Type *objectType, long i ) {
|
---|
1303 | std::stringstream offset_namer;
|
---|
1304 | offset_namer << i;
|
---|
1305 | ConstantExpr *fieldIndex = new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), offset_namer.str() ) );
|
---|
1306 | UntypedExpr *fieldOffset = new UntypedExpr( new NameExpr( "?[?]" ) );
|
---|
1307 | fieldOffset->get_args().push_back( new NameExpr( offsetofName( objectType ) ) );
|
---|
1308 | fieldOffset->get_args().push_back( fieldIndex );
|
---|
1309 | return fieldOffset;
|
---|
1310 | }
|
---|
1311 |
|
---|
1312 | /// Returns an expression dereferenced n times
|
---|
1313 | Expression *makeDerefdVar( Expression *derefdVar, long n ) {
|
---|
1314 | for ( int i = 1; i < n; ++i ) {
|
---|
1315 | UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );
|
---|
1316 | derefExpr->get_args().push_back( derefdVar );
|
---|
1317 | derefdVar = derefExpr;
|
---|
1318 | }
|
---|
1319 | return derefdVar;
|
---|
1320 | }
|
---|
1321 |
|
---|
1322 | Expression *MemberExprFixer::mutate( MemberExpr *memberExpr ) {
|
---|
1323 | // mutate, exiting early if no longer MemberExpr
|
---|
1324 | Expression *expr = Mutator::mutate( memberExpr );
|
---|
1325 | memberExpr = dynamic_cast< MemberExpr* >( expr );
|
---|
1326 | if ( ! memberExpr ) return expr;
|
---|
1327 |
|
---|
1328 | // get declaration for base struct, exiting early if not found
|
---|
1329 | int varDepth;
|
---|
1330 | VariableExpr *varExpr = getBaseVar( memberExpr->get_aggregate(), &varDepth );
|
---|
1331 | if ( ! varExpr ) return memberExpr;
|
---|
1332 | ObjectDecl *objectDecl = dynamic_cast< ObjectDecl* >( varExpr->get_var() );
|
---|
1333 | if ( ! objectDecl ) return memberExpr;
|
---|
1334 |
|
---|
1335 | // only mutate member expressions for polymorphic types
|
---|
1336 | int tyDepth;
|
---|
1337 | Type *objectType = hasPolyBase( objectDecl->get_type(), scopeTyVars, &tyDepth );
|
---|
1338 | if ( ! objectType ) return memberExpr;
|
---|
1339 |
|
---|
1340 | if ( StructInstType *structType = dynamic_cast< StructInstType* >( objectType ) ) {
|
---|
1341 | // look up offset index
|
---|
1342 | long i = findMember( memberExpr->get_member(), structType->get_baseStruct()->get_members() );
|
---|
1343 | if ( i == -1 ) return memberExpr;
|
---|
1344 |
|
---|
1345 | // replace member expression with pointer to base plus offset
|
---|
1346 | UntypedExpr *fieldLoc = new UntypedExpr( new NameExpr( "?+?" ) );
|
---|
1347 | fieldLoc->get_args().push_back( makeDerefdVar( varExpr->clone(), varDepth ) );
|
---|
1348 | fieldLoc->get_args().push_back( makeOffsetIndex( objectType, i ) );
|
---|
1349 |
|
---|
1350 | delete memberExpr;
|
---|
1351 | return fieldLoc;
|
---|
1352 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( objectType ) ) {
|
---|
1353 | // union members are all at offset zero, so build appropriately-dereferenced variable
|
---|
1354 | Expression *derefdVar = makeDerefdVar( varExpr->clone(), varDepth );
|
---|
1355 | delete memberExpr;
|
---|
1356 | return derefdVar;
|
---|
1357 | } else return memberExpr;
|
---|
1358 | }
|
---|
1359 |
|
---|
1360 | Expression *MemberExprFixer::mutate( OffsetofExpr *offsetofExpr ) {
|
---|
1361 | // mutate, exiting early if no longer OffsetofExpr
|
---|
1362 | Expression *expr = Mutator::mutate( offsetofExpr );
|
---|
1363 | offsetofExpr = dynamic_cast< OffsetofExpr* >( expr );
|
---|
1364 | if ( ! offsetofExpr ) return expr;
|
---|
1365 |
|
---|
1366 | // only mutate expressions for polymorphic structs/unions
|
---|
1367 | Type *ty = isPolyType( offsetofExpr->get_type(), scopeTyVars );
|
---|
1368 | if ( ! ty ) return offsetofExpr;
|
---|
1369 |
|
---|
1370 | if ( StructInstType *structType = dynamic_cast< StructInstType* >( ty ) ) {
|
---|
1371 | // replace offsetof expression by index into offset array
|
---|
1372 | long i = findMember( offsetofExpr->get_member(), structType->get_baseStruct()->get_members() );
|
---|
1373 | if ( i == -1 ) return offsetofExpr;
|
---|
1374 |
|
---|
1375 | Expression *offsetInd = makeOffsetIndex( ty, i );
|
---|
1376 | delete offsetofExpr;
|
---|
1377 | return offsetInd;
|
---|
1378 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( ty ) ) {
|
---|
1379 | // all union members are at offset zero
|
---|
1380 | delete offsetofExpr;
|
---|
1381 | return new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), std::string("0") ) );
|
---|
1382 | } else return offsetofExpr;
|
---|
1383 | }
|
---|
1384 |
|
---|
1385 | ////////////////////////////////////////// Pass3 ////////////////////////////////////////////////////
|
---|
1386 |
|
---|
1387 | template< typename DeclClass >
|
---|
1388 | DeclClass * Pass3::handleDecl( DeclClass *decl, Type *type ) {
|
---|
1389 | TyVarMap oldtyVars = scopeTyVars;
|
---|
1390 | makeTyVarMap( type, scopeTyVars );
|
---|
1391 |
|
---|
1392 | DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) );
|
---|
1393 | ScrubTyVars::scrub( decl, scopeTyVars );
|
---|
1394 |
|
---|
1395 | scopeTyVars = oldtyVars;
|
---|
1396 | return ret;
|
---|
1397 | }
|
---|
1398 |
|
---|
1399 | ObjectDecl * Pass3::mutate( ObjectDecl *objectDecl ) {
|
---|
1400 | return handleDecl( objectDecl, objectDecl->get_type() );
|
---|
1401 | }
|
---|
1402 |
|
---|
1403 | DeclarationWithType * Pass3::mutate( FunctionDecl *functionDecl ) {
|
---|
1404 | return handleDecl( functionDecl, functionDecl->get_functionType() );
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 | TypedefDecl * Pass3::mutate( TypedefDecl *typedefDecl ) {
|
---|
1408 | return handleDecl( typedefDecl, typedefDecl->get_base() );
|
---|
1409 | }
|
---|
1410 |
|
---|
1411 | TypeDecl * Pass3::mutate( TypeDecl *typeDecl ) {
|
---|
1412 | // Initializer *init = 0;
|
---|
1413 | // std::list< Expression *> designators;
|
---|
1414 | // scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
|
---|
1415 | // if ( typeDecl->get_base() ) {
|
---|
1416 | // init = new SimpleInit( new SizeofExpr( handleDecl( typeDecl, typeDecl->get_base() ) ), designators );
|
---|
1417 | // }
|
---|
1418 | // return new ObjectDecl( typeDecl->get_name(), Declaration::Extern, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::UnsignedInt ), init );
|
---|
1419 |
|
---|
1420 | scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
|
---|
1421 | return Mutator::mutate( typeDecl );
|
---|
1422 | }
|
---|
1423 |
|
---|
1424 | Type * Pass3::mutate( PointerType *pointerType ) {
|
---|
1425 | TyVarMap oldtyVars = scopeTyVars;
|
---|
1426 | makeTyVarMap( pointerType, scopeTyVars );
|
---|
1427 |
|
---|
1428 | Type *ret = Mutator::mutate( pointerType );
|
---|
1429 |
|
---|
1430 | scopeTyVars = oldtyVars;
|
---|
1431 | return ret;
|
---|
1432 | }
|
---|
1433 |
|
---|
1434 | Type * Pass3::mutate( FunctionType *functionType ) {
|
---|
1435 | TyVarMap oldtyVars = scopeTyVars;
|
---|
1436 | makeTyVarMap( functionType, scopeTyVars );
|
---|
1437 |
|
---|
1438 | Type *ret = Mutator::mutate( functionType );
|
---|
1439 |
|
---|
1440 | scopeTyVars = oldtyVars;
|
---|
1441 | return ret;
|
---|
1442 | }
|
---|
1443 | } // anonymous namespace
|
---|
1444 | } // namespace GenPoly
|
---|
1445 |
|
---|
1446 | // Local Variables: //
|
---|
1447 | // tab-width: 4 //
|
---|
1448 | // mode: c++ //
|
---|
1449 | // compile-command: "make install" //
|
---|
1450 | // End: //
|
---|