source: src/GenPoly/Lvalue.cc @ 6b8643d

new-envwith_gc
Last change on this file since 6b8643d was eba74ba, checked in by Aaron Moss <a3moss@…>, 6 years ago

Merge remote-tracking branch 'origin/master' into with_gc

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