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