source: src/GenPoly/Lvalue.cc @ 98278b3a

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 98278b3a was a9b1b0c, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Tweak the conditions in FixIntrinsicArgs? to add &/* more correctly when converting between intrinsic and non-intrinsic contexts

  • Property mode set to 100644
File size: 19.2 KB
RevLine 
[51587aa]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[906e24d]7// Lvalue.cc --
[51587aa]8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
[01aeade]11// Last Modified By : Peter A. Buhr
[615a096]12// Last Modified On : Fri Mar 17 09:11:18 2017
13// Update Count     : 5
[51587aa]14//
[51b7345]15
[e3e16bc]16#include <cassert>                       // for strict_dynamic_cast
[08fc48f]17#include <string>                        // for string
[51b7345]18
[8135d4c]19#include "Common/PassVisitor.h"
[08fc48f]20#include "GenPoly.h"                     // for isPolyType
[51b7345]21#include "Lvalue.h"
22
[08fc48f]23#include "Parser/LinkageSpec.h"          // for Spec, isBuiltin, Intrinsic
24#include "ResolvExpr/TypeEnvironment.h"  // for AssertionSet, OpenVarSet
25#include "ResolvExpr/Unify.h"            // for unify
[51b7345]26#include "ResolvExpr/typeops.h"
[8135d4c]27#include "SymTab/Autogen.h"
[08fc48f]28#include "SymTab/Indexer.h"              // for Indexer
29#include "SynTree/Declaration.h"         // for Declaration, FunctionDecl
30#include "SynTree/Expression.h"          // for Expression, ConditionalExpr
31#include "SynTree/Mutator.h"             // for mutateAll, Mutator
32#include "SynTree/Statement.h"           // for ReturnStmt, Statement (ptr o...
33#include "SynTree/Type.h"                // for PointerType, Type, FunctionType
34#include "SynTree/Visitor.h"             // for Visitor, acceptAll
[1d776fd]35
[cb43451]36#if 0
37#define PRINT(x) x
38#else
39#define PRINT(x)
40#endif
41
[51b7345]42namespace GenPoly {
[01aeade]43        namespace {
[9a34b5a]44                // TODO: fold this into the general createDeref function??
45                Expression * mkDeref( Expression * arg ) {
46                        if ( SymTab::dereferenceOperator ) {
[a9b1b0c]47                                // note: reference depth can be arbitrarily deep here, so peel off the outermost pointer/reference, not just pointer because they are effecitvely equivalent in this pass
[9a34b5a]48                                VariableExpr * deref = new VariableExpr( SymTab::dereferenceOperator );
[0690350]49                                deref->result = new PointerType( Type::Qualifiers(), deref->result );
50                                Type * base = InitTweak::getPointerBase( arg->result );
51                                assertf( base, "expected pointer type in dereference (type was %s)", toString( arg->result ).c_str() );
[9a34b5a]52                                ApplicationExpr * ret = new ApplicationExpr( deref, { arg } );
[0690350]53                                delete ret->result;
54                                ret->result = base->clone();
55                                ret->result->set_lvalue( true );
[9a34b5a]56                                return ret;
57                        } else {
58                                return UntypedExpr::createDeref( arg );
59                        }
60                }
61
[31cb252]62                struct ReferenceConversions final : public WithStmtsToAdd {
[1d776fd]63                        Expression * postmutate( CastExpr * castExpr );
[8a6cf7e]64                        Expression * postmutate( AddressExpr * addrExpr );
[01aeade]65                };
66
[1d776fd]67                /// Intrinsic functions that take reference parameters don't REALLY take reference parameters -- their reference arguments must always be implicitly dereferenced.
68                struct FixIntrinsicArgs final {
[8499c707]69                        Expression * postmutate( ApplicationExpr * appExpr );
[1d776fd]70                };
71
[9aaac6e9]72                struct FixIntrinsicResult final : public WithGuards {
[8499c707]73                        Expression * postmutate( ApplicationExpr * appExpr );
[9aaac6e9]74                        void premutate( FunctionDecl * funcDecl );
75                        bool inIntrinsic = false;
[8499c707]76                };
[1d776fd]77
78                /// Replace reference types with pointer types
79                struct ReferenceTypeElimination final {
80                        Type * postmutate( ReferenceType * refType );
[01aeade]81                };
[b6fd751]82
83                /// GCC-like Generalized Lvalues (which have since been removed from GCC)
84                /// https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Lvalues.html#Lvalues
85                /// Replaces &(a,b) with (a, &b), &(a ? b : c) with (a ? &b : &c)
[d335627]86                struct GeneralizedLvalue final : public WithVisitorRef<GeneralizedLvalue> {
[1d776fd]87                        Expression * postmutate( AddressExpr * addressExpr );
[9236060]88                        Expression * postmutate( MemberExpr * memExpr );
[acd7c5dd]89
90                        template<typename Func>
91                        Expression * applyTransformation( Expression * expr, Expression * arg, Func mkExpr );
[b6fd751]92                };
[cb43451]93
94                /// Removes redundant &*/*& pattern that this pass can generate
95                struct CollapseAddrDeref final {
96                        Expression * postmutate( AddressExpr * addressExpr );
97                        Expression * postmutate( ApplicationExpr * appExpr );
98                };
[8499c707]99
100                struct AddrRef final : public WithGuards {
101                        void premutate( AddressExpr * addrExpr );
102                        Expression * postmutate( AddressExpr * addrExpr );
103                        void premutate( Expression * expr );
104
105                        bool first = true;
106                        bool current = false;
107                        int refDepth = 0;
108                };
[01aeade]109        } // namespace
110
[b0440b7]111        static bool referencesEliminated = false;
112        // used by UntypedExpr::createDeref to determine whether result type of dereference should be ReferenceType or value type.
113        bool referencesPermissable() {
114                return ! referencesEliminated;
115        }
116
[b1ccdfd]117        void convertLvalue( std::list< Declaration* > & translationUnit ) {
[1d776fd]118                PassVisitor<ReferenceConversions> refCvt;
119                PassVisitor<ReferenceTypeElimination> elim;
120                PassVisitor<GeneralizedLvalue> genLval;
121                PassVisitor<FixIntrinsicArgs> fixer;
[cb43451]122                PassVisitor<CollapseAddrDeref> collapser;
[8499c707]123                PassVisitor<AddrRef> addrRef;
124                PassVisitor<FixIntrinsicResult> intrinsicResults;
125                mutateAll( translationUnit, intrinsicResults );
126                mutateAll( translationUnit, addrRef );
[1d776fd]127                mutateAll( translationUnit, refCvt );
128                mutateAll( translationUnit, fixer );
[cb43451]129                mutateAll( translationUnit, collapser );
[8499c707]130                mutateAll( translationUnit, genLval );
[8a6cf7e]131                mutateAll( translationUnit, elim );  // last because other passes need reference types to work
[b0440b7]132
133                // from this point forward, no other pass should create reference types.
134                referencesEliminated = true;
[01aeade]135        }
136
[acd7c5dd]137        Expression * generalizedLvalue( Expression * expr ) {
[9236060]138                PassVisitor<GeneralizedLvalue> genLval;
[acd7c5dd]139                return expr->acceptMutator( genLval );
140        }
141
[01aeade]142        namespace {
[8a6cf7e]143                // true for intrinsic function calls that return a reference
[1d776fd]144                bool isIntrinsicReference( Expression * expr ) {
[8a6cf7e]145                        if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * >( expr ) ) {
146                                std::string fname = InitTweak::getFunctionName( untyped );
147                                // known intrinsic-reference prelude functions
148                                return fname == "*?" || fname == "?[?]";
[1d776fd]149                        } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) {
150                                if ( DeclarationWithType * func = InitTweak::getFunction( appExpr ) ) {
[8a6cf7e]151                                        // use type of return variable rather than expr result type, since it may have been changed to a pointer type
152                                        FunctionType * ftype = GenPoly::getFunctionType( func->get_type() );
[b1ccdfd]153                                        Type * ret = ftype->returnVals.empty() ? nullptr : ftype->returnVals.front()->get_type();
154                                        return func->linkage == LinkageSpec::Intrinsic && dynamic_cast<ReferenceType *>( ret );
[1d776fd]155                                }
[ce8c12f]156                        }
[1d776fd]157                        return false;
[ce8c12f]158                }
159
[8499c707]160                Expression * FixIntrinsicResult::postmutate( ApplicationExpr * appExpr ) {
161                        if ( isIntrinsicReference( appExpr ) ) {
162                                // eliminate reference types from intrinsic applications - now they return lvalues
[b1ccdfd]163                                Type * result = appExpr->result;
164                                appExpr->result = result->stripReferences()->clone();
165                                appExpr->result->set_lvalue( true );
[9aaac6e9]166                                if ( ! inIntrinsic ) {
167                                        // when not in an intrinsic function, add a cast to
168                                        // don't add cast when in an intrinsic function, since they already have the cast
169                                        Expression * ret = new CastExpr( appExpr, result );
[b1ccdfd]170                                        std::swap( ret->env, appExpr->env );
[9aaac6e9]171                                        return ret;
172                                }
173                                delete result;
[8499c707]174                        }
175                        return appExpr;
176                }
177
[9aaac6e9]178                void FixIntrinsicResult::premutate( FunctionDecl * funcDecl ) {
179                        GuardValue( inIntrinsic );
180                        inIntrinsic =  funcDecl->linkage == LinkageSpec::Intrinsic;
181                }
182
[1d776fd]183                Expression * FixIntrinsicArgs::postmutate( ApplicationExpr * appExpr ) {
[9a34b5a]184                        // intrinsic functions don't really take reference-typed parameters, so they require an implicit dereference on their arguments.
[1d776fd]185                        if ( DeclarationWithType * function = InitTweak::getFunction( appExpr ) ) {
[d335627]186                                FunctionType * ftype = GenPoly::getFunctionType( function->get_type() );
187                                assertf( ftype, "Function declaration does not have function type." );
188                                // can be of differing lengths only when function is variadic
[b1ccdfd]189                                assertf( ftype->parameters.size() == appExpr->args.size() || ftype->isVarArgs, "ApplicationExpr args do not match formal parameter type." );
[b0440b7]190
191
[d335627]192                                unsigned int i = 0;
[b1ccdfd]193                                const unsigned int end = ftype->parameters.size();
194                                for ( auto p : unsafe_group_iterate( appExpr->args, ftype->parameters ) ) {
[d335627]195                                        if (i == end) break;
196                                        Expression *& arg = std::get<0>( p );
197                                        Type * formal = std::get<1>( p )->get_type();
198                                        PRINT(
199                                                std::cerr << "pair<0>: " << arg << std::endl;
[a9b1b0c]200                                                std::cerr << " -- " << arg->result << std::endl;
[d335627]201                                                std::cerr << "pair<1>: " << formal << std::endl;
202                                        )
203                                        if ( dynamic_cast<ReferenceType*>( formal ) ) {
[a9b1b0c]204                                                PRINT(
205                                                        std::cerr << "===formal is reference" << std::endl;
206                                                )
207                                                // TODO: it's likely that the second condition should be ... && ! isIntrinsicReference( arg ), but this requires investigation.
208                                                if ( function->get_linkage() != LinkageSpec::Intrinsic && isIntrinsicReference( arg ) ) {
209                                                        // if argument is dereference or array subscript, the result isn't REALLY a reference, but non-intrinsic functions expect a reference: take address
210                                                        PRINT(
211                                                                std::cerr << "===is intrinsic arg in non-intrinsic call - adding address" << std::endl;
212                                                        )
213                                                        arg = new AddressExpr( arg );
214                                                } else if ( function->get_linkage() == LinkageSpec::Intrinsic && arg->result->referenceDepth() != 0 ) {
215                                                        // argument is a 'real' reference, but function expects a C lvalue: add a dereference to the reference-typed argument
216                                                        PRINT(
217                                                                std::cerr << "===is non-intrinsic arg in intrinsic call - adding deref to arg" << std::endl;
218                                                        )
[b1ccdfd]219                                                        Type * baseType = InitTweak::getPointerBase( arg->result );
220                                                        assertf( baseType, "parameter is reference, arg must be pointer or reference: %s", toString( arg->result ).c_str() );
[d335627]221                                                        PointerType * ptrType = new PointerType( Type::Qualifiers(), baseType->clone() );
[b1ccdfd]222                                                        delete arg->result;
[d335627]223                                                        arg->set_result( ptrType );
224                                                        arg = mkDeref( arg );
[a9b1b0c]225                                                        assertf( arg->result->referenceDepth() == 0, "Reference types should have been eliminated from intrinsic function calls, but weren't: %s", toCString( arg->result ) );
[1d776fd]226                                                }
227                                        }
[d335627]228                                        ++i;
[baba5d8]229                                }
[1d776fd]230                        }
231                        return appExpr;
[01aeade]232                }
233
[8499c707]234                // idea: &&&E: get outer &, inner &
235                // at inner &, record depth D of reference type
236                // at outer &, add D derefs.
[8135d4c]237                void AddrRef::premutate( Expression * ) {
[8499c707]238                        GuardValue( current );
239                        GuardValue( first );
240                        current = false;
241                        first = true;
242                }
243
[8135d4c]244                void AddrRef::premutate( AddressExpr * ) {
[8499c707]245                        GuardValue( current );
246                        GuardValue( first );
247                        current = first;
248                        first = false;
249                        if ( current ) {
250                                GuardValue( refDepth );
251                                refDepth = 0;
252                        }
253                }
254
255                Expression * AddrRef::postmutate( AddressExpr * addrExpr ) {
256                        if ( refDepth == 0 ) {
[b1ccdfd]257                                if ( ! isIntrinsicReference( addrExpr->arg ) ) {
[8499c707]258                                        // try to avoid ?[?]
[b1ccdfd]259                                        refDepth = addrExpr->arg->result->referenceDepth();
[8499c707]260                                }
261                        }
262                        if ( current ) {
263                                Expression * ret = addrExpr;
264                                while ( refDepth ) {
265                                        ret = mkDeref( ret );
266                                        refDepth--;
267                                }
268                                return ret;
269                        }
270                        return addrExpr;
271                }
272
[8a6cf7e]273                Expression * ReferenceConversions::postmutate( AddressExpr * addrExpr ) {
274                        // Inner expression may have been lvalue to reference conversion, which becomes an address expression.
275                        // In this case, remove the outer address expression and return the argument.
276                        // TODO: It's possible that this might catch too much and require a more sophisticated check.
277                        return addrExpr;
278                }
279
[1d776fd]280                Expression * ReferenceConversions::postmutate( CastExpr * castExpr ) {
[9a34b5a]281                        // xxx - is it possible to convert directly between reference types with a different base? E.g.,
282                        //   int x;
283                        //   (double&)x;
284                        // At the moment, I am working off of the assumption that this is illegal, thus the cast becomes redundant
285                        // after this pass, so trash the cast altogether. If that changes, care must be taken to insert the correct
286                        // pointer casts in the right places.
287
[a4d188f]288                        // Note: reference depth difference is the determining factor in what code is run, rather than whether something is
289                        // reference type or not, since conversion still needs to occur when both types are references that differ in depth.
[31cb252]290
291                        Type * destType = castExpr->result;
292                        Type * srcType = castExpr->arg->result;
293                        int depth1 = destType->referenceDepth();
294                        int depth2 = srcType->referenceDepth();
295                        int diff = depth1 - depth2;
296
297                        if ( diff > 0 && ! srcType->get_lvalue() ) {
298                                // rvalue to reference conversion -- introduce temporary
299                                // know that reference depth of cast argument is 0, need to introduce n temporaries for reference depth of n, e.g.
300                                //   (int &&&)3;
301                                // becomes
302                                //   int __ref_tmp_0 = 3;
303                                //   int & __ref_tmp_1 = _&_ref_tmp_0;
304                                //   int && __ref_tmp_2 = &__ref_tmp_1;
305                                //   &__ref_tmp_2;
[453b586]306                                // the last & comes from the remaining reference conversion code
[2348881]307                                SemanticWarning( castExpr->arg->location, Warning::RvalueToReferenceConversion, toCString( castExpr->arg ) );
[31cb252]308
309                                static UniqueName tempNamer( "__ref_tmp_" );
310                                ObjectDecl * temp = ObjectDecl::newObject( tempNamer.newName(), castExpr->arg->result->clone(), new SingleInit( castExpr->arg ) );
311                                PRINT( std::cerr << "made temp: " << temp << std::endl; )
312                                stmtsToAddBefore.push_back( new DeclStmt( temp ) );
[a4d188f]313                                for ( int i = 0; i < depth1-1; i++ ) { // xxx - maybe this should be diff-1? check how this works with reference type for srcType
[31cb252]314                                        ObjectDecl * newTemp = ObjectDecl::newObject( tempNamer.newName(), new ReferenceType( Type::Qualifiers(), temp->type->clone() ), new SingleInit( new AddressExpr( new VariableExpr( temp ) ) ) );
315                                        PRINT( std::cerr << "made temp" << i << ": " << newTemp << std::endl; )
316                                        stmtsToAddBefore.push_back( new DeclStmt( newTemp ) );
317                                        temp = newTemp;
318                                }
319                                // update diff so that remaining code works out correctly
320                                castExpr->arg = new VariableExpr( temp );
321                                PRINT( std::cerr << "update cast to: " << castExpr << std::endl; )
322                                srcType = castExpr->arg->result;
323                                depth2 = srcType->referenceDepth();
324                                diff = depth1 - depth2;
325                                assert( diff == 1 );
326                        }
[fc56cdbf]327
[a4d188f]328                        // handle conversion between different depths
[31cb252]329                        PRINT (
330                                if ( depth1 || depth2 ) {
331                                        std::cerr << "destType: " << destType << " / srcType: " << srcType << std::endl;
332                                        std::cerr << "depth: " << depth1 << " / " << depth2 << std::endl;
333                                }
334                        )
335                        if ( diff > 0 ) {
336                                // conversion to type with more depth (e.g. int & -> int &&): add address-of for each level of difference
337                                Expression * ret = castExpr->arg;
338                                for ( int i = 0; i < diff; ++i ) {
339                                        ret = new AddressExpr( ret );
340                                }
341                                if ( srcType->get_lvalue() && srcType->get_qualifiers() != strict_dynamic_cast<ReferenceType *>( destType )->base->get_qualifiers() ) {
342                                        // must keep cast if cast-to type is different from the actual type
343                                        castExpr->arg = ret;
[1d776fd]344                                        return castExpr;
345                                }
[31cb252]346                                ret->env = castExpr->env;
347                                delete ret->result;
348                                ret->result = castExpr->result;
349                                castExpr->env = nullptr;
350                                castExpr->arg = nullptr;
351                                castExpr->result = nullptr;
352                                delete castExpr;
353                                return ret;
354                        } else if ( diff < 0 ) {
355                                // conversion to type with less depth (e.g. int && -> int &): add dereferences for each level of difference
356                                diff = -diff; // care only about magnitude now
[0690350]357                                Expression * ret = castExpr->arg;
[31cb252]358                                for ( int i = 0; i < diff; ++i ) {
[9191a8e]359                                        ret = mkDeref( ret );
360                                }
[31cb252]361                                if ( ! ResolvExpr::typesCompatibleIgnoreQualifiers( destType->stripReferences(), srcType->stripReferences(), SymTab::Indexer() ) ) {
[8a6cf7e]362                                        // must keep cast if types are different
[0690350]363                                        castExpr->arg = ret;
[31cb252]364                                        return castExpr;
[8a6cf7e]365                                }
[31cb252]366                                ret->env = castExpr->env;
367                                delete ret->result;
368                                ret->result = castExpr->result;
369                                ret->result->set_lvalue( true ); // ensure result is lvalue
370                                castExpr->env = nullptr;
371                                castExpr->arg = nullptr;
372                                castExpr->result = nullptr;
373                                delete castExpr;
[9191a8e]374                                return ret;
[31cb252]375                        } else {
376                                assert( diff == 0 );
377                                // conversion between references of the same depth
378                                return castExpr;
[1d776fd]379                        }
[01aeade]380                }
[b6fd751]381
[1d776fd]382                Type * ReferenceTypeElimination::postmutate( ReferenceType * refType ) {
[b1ccdfd]383                        Type * base = refType->base;
[8a6cf7e]384                        Type::Qualifiers qualifiers = refType->get_qualifiers();
[b1ccdfd]385                        refType->base = nullptr;
[1d776fd]386                        delete refType;
[8a6cf7e]387                        return new PointerType( qualifiers, base );
[ce8c12f]388                }
389
[acd7c5dd]390                template<typename Func>
391                Expression * GeneralizedLvalue::applyTransformation( Expression * expr, Expression * arg, Func mkExpr ) {
392                        if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( arg ) ) {
[b1ccdfd]393                                Expression * arg1 = commaExpr->arg1->clone();
394                                Expression * arg2 = commaExpr->arg2->clone();
[9236060]395                                Expression * ret = new CommaExpr( arg1, mkExpr( arg2 )->acceptMutator( *visitor ) );
[b1ccdfd]396                                ret->env = expr->env;
397                                expr->env = nullptr;
[acd7c5dd]398                                delete expr;
[8499c707]399                                return ret;
[acd7c5dd]400                        } else if ( ConditionalExpr * condExpr = dynamic_cast< ConditionalExpr * >( arg ) ) {
[b1ccdfd]401                                Expression * arg1 = condExpr->arg1->clone();
402                                Expression * arg2 = condExpr->arg2->clone();
403                                Expression * arg3 = condExpr->arg3->clone();
[9236060]404                                ConditionalExpr * ret = new ConditionalExpr( arg1, mkExpr( arg2 )->acceptMutator( *visitor ), mkExpr( arg3 )->acceptMutator( *visitor ) );
[b1ccdfd]405                                ret->env = expr->env;
406                                expr->env = nullptr;
[acd7c5dd]407                                delete expr;
408
409                                // conditional expr type may not be either of the argument types, need to unify
410                                using namespace ResolvExpr;
411                                Type* commonType = nullptr;
412                                TypeEnvironment newEnv;
413                                AssertionSet needAssertions, haveAssertions;
414                                OpenVarSet openVars;
[b1ccdfd]415                                unify( ret->arg2->result, ret->arg3->result, newEnv, needAssertions, haveAssertions, openVars, SymTab::Indexer(), commonType );
416                                ret->result = commonType ? commonType : ret->arg2->result->clone();
[8499c707]417                                return ret;
[b6fd751]418                        }
[acd7c5dd]419                        return expr;
420                }
421
[9236060]422                Expression * GeneralizedLvalue::postmutate( MemberExpr * memExpr ) {
[b1ccdfd]423                        return applyTransformation( memExpr, memExpr->aggregate, [=]( Expression * aggr ) { return new MemberExpr( memExpr->member, aggr ); } );
[acd7c5dd]424                }
425
[9236060]426                Expression * GeneralizedLvalue::postmutate( AddressExpr * addrExpr ) {
[b1ccdfd]427                        return applyTransformation( addrExpr, addrExpr->arg, []( Expression * arg ) { return new AddressExpr( arg ); } );
[b6fd751]428                }
[cb43451]429
[8499c707]430                Expression * CollapseAddrDeref::postmutate( AddressExpr * addrExpr ) {
[b1ccdfd]431                        Expression * arg = addrExpr->arg;
[cb43451]432                        if ( isIntrinsicReference( arg ) ) {
433                                std::string fname = InitTweak::getFunctionName( arg );
434                                if ( fname == "*?" ) {
435                                        Expression *& arg0 = InitTweak::getCallArg( arg, 0 );
436                                        Expression * ret = arg0;
[b1ccdfd]437                                        ret->set_env( addrExpr->env );
[cb43451]438                                        arg0 = nullptr;
[b1ccdfd]439                                        addrExpr->env = nullptr;
[8499c707]440                                        delete addrExpr;
[cb43451]441                                        return ret;
442                                }
[bf7b6015]443                        } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * > ( arg ) ) {
444                                // need to move cast to pointer type out a level since address of pointer
445                                // is not valid C code (can be introduced in prior passes, e.g., InstantiateGeneric)
446                                if ( InitTweak::getPointerBase( castExpr->result ) ) {
447                                        addrExpr->arg = castExpr->arg;
448                                        castExpr->arg = addrExpr;
449                                        castExpr->result = new PointerType( Type::Qualifiers(), castExpr->result );
450                                        return castExpr;
451                                }
[cb43451]452                        }
[8499c707]453                        return addrExpr;
[cb43451]454                }
455
456                Expression * CollapseAddrDeref::postmutate( ApplicationExpr * appExpr ) {
457                        if ( isIntrinsicReference( appExpr ) ) {
458                                std::string fname = InitTweak::getFunctionName( appExpr );
459                                if ( fname == "*?" ) {
460                                        Expression * arg = InitTweak::getCallArg( appExpr, 0 );
461                                        // xxx - this isn't right, because it can remove casts that should be there...
462                                        // while ( CastExpr * castExpr = dynamic_cast< CastExpr * >( arg ) ) {
463                                        //      arg = castExpr->get_arg();
464                                        // }
465                                        if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( arg ) ) {
[b1ccdfd]466                                                Expression * ret = addrExpr->arg;
467                                                ret->env = appExpr->env;
468                                                addrExpr->arg = nullptr;
469                                                appExpr->env = nullptr;
[cb43451]470                                                delete appExpr;
471                                                return ret;
472                                        }
473                                }
474                        }
475                        return appExpr;
476                }
[01aeade]477        } // namespace
[51b7345]478} // namespace GenPoly
[01aeade]479
[51587aa]480// Local Variables: //
481// tab-width: 4 //
482// mode: c++ //
483// compile-command: "make install" //
484// End: //
Note: See TracBrowser for help on using the repository browser.