source: src/ResolvExpr/AlternativeFinder.h @ 6b0b624

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 6b0b624 was 6b0b624, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

change #ifndef to #pragma once

  • Property mode set to 100644
File size: 6.0 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// AlternativeFinder.h --
8//
9// Author           : Richard C. Bilson
10// Created On       : Sat May 16 23:56:12 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sat Jul 22 09:35:32 2017
13// Update Count     : 3
14//
15
16#pragma once
17
18#include <set>
19
20#include "Alternative.h"
21#include "Unify.h"
22#include "SynTree/SynTree.h"
23#include "SymTab/Indexer.h"
24#include "SynTree/TypeSubstitution.h"
25
26namespace ResolvExpr {
27        class AlternativeFinder : public Visitor {
28          public:
29                AlternativeFinder( const SymTab::Indexer &indexer, const TypeEnvironment &env );
30                void find( Expression *expr, bool adjust = false, bool prune = true );
31                /// Calls find with the adjust flag set; adjustment turns array and function types into equivalent pointer types
32                void findWithAdjustment( Expression *expr, bool prune = true );
33                AltList &get_alternatives() { return alternatives; }
34
35                // make this look like an STL container so that we can apply generic algorithms
36                typedef Alternative value_type;
37                typedef AltList::iterator iterator;
38                typedef AltList::const_iterator const_iterator;
39                AltList::iterator begin() { return alternatives.begin(); }
40                AltList::iterator end() { return alternatives.end(); }
41                AltList::const_iterator begin() const { return alternatives.begin(); }
42                AltList::const_iterator end() const { return alternatives.end(); }
43
44                const SymTab::Indexer &get_indexer() const { return indexer; }
45                const TypeEnvironment &get_environ() const { return env; }
46          private:
47                virtual void visit( ApplicationExpr *applicationExpr );
48                virtual void visit( UntypedExpr *untypedExpr );
49                virtual void visit( AddressExpr *addressExpr );
50                virtual void visit( CastExpr *castExpr );
51                virtual void visit( UntypedMemberExpr *memberExpr );
52                virtual void visit( MemberExpr *memberExpr );
53                virtual void visit( NameExpr *variableExpr );
54                virtual void visit( VariableExpr *variableExpr );
55                virtual void visit( ConstantExpr *constantExpr );
56                virtual void visit( SizeofExpr *sizeofExpr );
57                virtual void visit( AlignofExpr *alignofExpr );
58                virtual void visit( UntypedOffsetofExpr *offsetofExpr );
59                virtual void visit( OffsetofExpr *offsetofExpr );
60                virtual void visit( OffsetPackExpr *offsetPackExpr );
61                virtual void visit( AttrExpr *attrExpr );
62                virtual void visit( LogicalExpr *logicalExpr );
63                virtual void visit( ConditionalExpr *conditionalExpr );
64                virtual void visit( CommaExpr *commaExpr );
65                virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr );
66                virtual void visit( ConstructorExpr * ctorExpr );
67                virtual void visit( RangeExpr * rangeExpr );
68                virtual void visit( UntypedTupleExpr *tupleExpr );
69                virtual void visit( TupleExpr *tupleExpr );
70                virtual void visit( TupleIndexExpr *tupleExpr );
71                virtual void visit( TupleAssignExpr *tupleExpr );
72                virtual void visit( UniqueExpr *unqExpr );
73                virtual void visit( StmtExpr *stmtExpr );
74                virtual void visit( UntypedInitExpr *initExpr );
75                /// Runs a new alternative finder on each element in [begin, end)
76                /// and writes each alternative finder to out.
77                template< typename InputIterator, typename OutputIterator >
78                void findSubExprs( InputIterator begin, InputIterator end, OutputIterator out );
79
80                /// Adds alternatives for anonymous members
81                void addAnonConversions( const Alternative & alt );
82                /// Adds alternatives for member expressions, given the aggregate, conversion cost for that aggregate, and name of the member
83                template< typename StructOrUnionType > void addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member );
84                /// Adds alternatives for member expressions where the left side has tuple type
85                void addTupleMembers( TupleType * tupleType, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member );
86                /// Adds alternatives for offsetof expressions, given the base type and name of the member
87                template< typename StructOrUnionType > void addOffsetof( StructOrUnionType *aggInst, const std::string &name );
88                bool instantiateFunction( std::list< DeclarationWithType* >& formals, const AltList &actuals, bool isVarArgs, OpenVarSet& openVars, TypeEnvironment &resultEnv, AssertionSet &resultNeed, AssertionSet &resultHave, AltList & out );
89                template< typename OutputIterator >
90                void makeFunctionAlternatives( const Alternative &func, FunctionType *funcType, const AltList &actualAlt, OutputIterator out );
91                template< typename OutputIterator >
92                void inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out );
93                void resolveAttr( DeclarationWithType *funcDecl, FunctionType *function, Type *argType, const TypeEnvironment &env );
94
95                const SymTab::Indexer &indexer;
96                AltList alternatives;
97                const TypeEnvironment &env;
98                Type * targetType = nullptr;
99        }; // AlternativeFinder
100
101        Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer, TypeEnvironment &env );
102
103        template< typename InputIterator, typename OutputIterator >
104        void findMinCost( InputIterator begin, InputIterator end, OutputIterator out ) {
105                AltList alternatives;
106
107                // select the alternatives that have the minimum parameter cost
108                Cost minCost = Cost::infinity;
109                for ( InputIterator i = begin; i != end; ++i ) {
110                        if ( i->cost < minCost ) {
111                                minCost = i->cost;
112                                i->cost = i->cvtCost;
113                                alternatives.clear();
114                                alternatives.push_back( *i );
115                        } else if ( i->cost == minCost ) {
116                                i->cost = i->cvtCost;
117                                alternatives.push_back( *i );
118                        }
119                }
120                std::copy( alternatives.begin(), alternatives.end(), out );
121        }
122
123        Cost sumCost( const AltList &in );
124
125        template< typename InputIterator >
126        void simpleCombineEnvironments( InputIterator begin, InputIterator end, TypeEnvironment &result ) {
127                while ( begin != end ) {
128                        result.simpleCombine( (*begin++).env );
129                }
130        }
131} // namespace ResolvExpr
132
133// Local Variables: //
134// tab-width: 4 //
135// mode: c++ //
136// compile-command: "make install" //
137// End: //
Note: See TracBrowser for help on using the repository browser.