source: src/ResolvExpr/AlternativeFinder.h @ 0872c42

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 0872c42 was 403b388, checked in by Aaron Moss <a3moss@…>, 6 years ago

Tweaked resolver data structure to reduce dynamic allocation

  • Property mode set to 100644
File size: 7.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// AlternativeFinder.h --
8//
9// Author           : Richard C. Bilson
10// Created On       : Sat May 16 23:56:12 2015
11// Last Modified By : Andrew Beach
12// Last Modified On : Wed Jul 26 11:24:00 2017
13// Update Count     : 4
14//
15
16#pragma once
17
18#include <algorithm>                     // for copy
19#include <list>                          // for list
20#include <string>                        // for string
21
22#include "Alternative.h"                 // for AltList, Alternative
23#include "ResolvExpr/Cost.h"             // for Cost, Cost::infinity
24#include "ResolvExpr/TypeEnvironment.h"  // for AssertionSet, OpenVarSet
25#include "SynTree/Visitor.h"             // for Visitor
26#include "SynTree/SynTree.h"             // for Visitor Nodes
27
28namespace SymTab {
29class Indexer;
30}  // namespace SymTab
31
32namespace ResolvExpr {
33        class ArgPack;
34       
35        class AlternativeFinder : public Visitor {
36          public:
37                AlternativeFinder( const SymTab::Indexer &indexer, const TypeEnvironment &env );
38
39                AlternativeFinder( const AlternativeFinder& o )
40                        : indexer(o.indexer), alternatives(o.alternatives), env(o.env), 
41                          targetType(o.targetType) {}
42               
43                AlternativeFinder( AlternativeFinder&& o )
44                        : indexer(o.indexer), alternatives(std::move(o.alternatives)), env(o.env), 
45                          targetType(o.targetType) {}
46               
47                AlternativeFinder& operator= ( const AlternativeFinder& o ) {
48                        if (&o == this) return *this;
49                       
50                        // horrific nasty hack to rebind references...
51                        alternatives.~AltList();
52                        new(this) AlternativeFinder(o);
53                        return *this;
54                }
55
56                AlternativeFinder& operator= ( AlternativeFinder&& o ) {
57                        if (&o == this) return *this;
58                       
59                        // horrific nasty hack to rebind references...
60                        alternatives.~AltList();
61                        new(this) AlternativeFinder(std::move(o));
62                        return *this;
63                }
64
65                void find( Expression *expr, bool adjust = false, bool prune = true, bool failFast = true );
66                /// Calls find with the adjust flag set; adjustment turns array and function types into equivalent pointer types
67                void findWithAdjustment( Expression *expr );
68                /// Calls find with the adjust flag set and prune flag unset; pruning ensures there is at most one alternative per result type
69                void findWithoutPrune( Expression *expr );
70                /// Calls find with the adjust and prune flags set, failFast flags unset; fail fast ensures that there is at least one resulting alternative
71                void maybeFind( Expression *expr );
72                AltList &get_alternatives() { return alternatives; }
73
74                // make this look like an STL container so that we can apply generic algorithms
75                typedef Alternative value_type;
76                typedef AltList::iterator iterator;
77                typedef AltList::const_iterator const_iterator;
78                AltList::iterator begin() { return alternatives.begin(); }
79                AltList::iterator end() { return alternatives.end(); }
80                AltList::const_iterator begin() const { return alternatives.begin(); }
81                AltList::const_iterator end() const { return alternatives.end(); }
82
83                const SymTab::Indexer &get_indexer() const { return indexer; }
84                const TypeEnvironment &get_environ() const { return env; }
85
86                /// Runs a new alternative finder on each element in [begin, end)
87                /// and writes each alternative finder to out.
88                template< typename InputIterator, typename OutputIterator >
89                void findSubExprs( InputIterator begin, InputIterator end, OutputIterator out );
90          private:
91                virtual void visit( ApplicationExpr *applicationExpr );
92                virtual void visit( UntypedExpr *untypedExpr );
93                virtual void visit( AddressExpr *addressExpr );
94                virtual void visit( LabelAddressExpr *labelExpr );
95                virtual void visit( CastExpr *castExpr );
96                virtual void visit( VirtualCastExpr *castExpr );
97                virtual void visit( UntypedMemberExpr *memberExpr );
98                virtual void visit( MemberExpr *memberExpr );
99                virtual void visit( NameExpr *variableExpr );
100                virtual void visit( VariableExpr *variableExpr );
101                virtual void visit( ConstantExpr *constantExpr );
102                virtual void visit( SizeofExpr *sizeofExpr );
103                virtual void visit( AlignofExpr *alignofExpr );
104                virtual void visit( UntypedOffsetofExpr *offsetofExpr );
105                virtual void visit( OffsetofExpr *offsetofExpr );
106                virtual void visit( OffsetPackExpr *offsetPackExpr );
107                virtual void visit( AttrExpr *attrExpr );
108                virtual void visit( LogicalExpr *logicalExpr );
109                virtual void visit( ConditionalExpr *conditionalExpr );
110                virtual void visit( CommaExpr *commaExpr );
111                virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr );
112                virtual void visit( ConstructorExpr * ctorExpr );
113                virtual void visit( RangeExpr * rangeExpr );
114                virtual void visit( UntypedTupleExpr *tupleExpr );
115                virtual void visit( TupleExpr *tupleExpr );
116                virtual void visit( TupleIndexExpr *tupleExpr );
117                virtual void visit( TupleAssignExpr *tupleExpr );
118                virtual void visit( UniqueExpr *unqExpr );
119                virtual void visit( StmtExpr *stmtExpr );
120                virtual void visit( UntypedInitExpr *initExpr );
121
122                /// Adds alternatives for anonymous members
123                void addAnonConversions( const Alternative & alt );
124                /// Adds alternatives for member expressions, given the aggregate, conversion cost for that aggregate, and name of the member
125                template< typename StructOrUnionType > void addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member );
126                /// Adds alternatives for member expressions where the left side has tuple type
127                void addTupleMembers( TupleType * tupleType, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member );
128                /// Adds alternatives for offsetof expressions, given the base type and name of the member
129                template< typename StructOrUnionType > void addOffsetof( StructOrUnionType *aggInst, const std::string &name );
130                /// Takes a final result and checks if its assertions can be satisfied
131                template<typename OutputIterator>
132                void validateFunctionAlternative( const Alternative &func, ArgPack& result, const std::vector<ArgPack>& results, OutputIterator out );
133                /// Finds matching alternatives for a function, given a set of arguments
134                template<typename OutputIterator>
135                void makeFunctionAlternatives( const Alternative &func, FunctionType *funcType, const std::vector< AlternativeFinder >& args, OutputIterator out );
136                /// Checks if assertion parameters match for a new alternative
137                template< typename OutputIterator >
138                void inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out );
139                void resolveAttr( DeclarationWithType *funcDecl, FunctionType *function, Type *argType, const TypeEnvironment &env );
140
141                const SymTab::Indexer &indexer;
142                AltList alternatives;
143                const TypeEnvironment &env;
144                Type * targetType = nullptr;
145        }; // AlternativeFinder
146
147        Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer, TypeEnvironment &env );
148        void referenceToRvalueConversion( Expression *& expr );
149
150        template< typename InputIterator, typename OutputIterator >
151        void findMinCost( InputIterator begin, InputIterator end, OutputIterator out ) {
152                AltList alternatives;
153
154                // select the alternatives that have the minimum parameter cost
155                Cost minCost = Cost::infinity;
156                for ( InputIterator i = begin; i != end; ++i ) {
157                        if ( i->cost < minCost ) {
158                                minCost = i->cost;
159                                i->cost = i->cvtCost;
160                                alternatives.clear();
161                                alternatives.push_back( *i );
162                        } else if ( i->cost == minCost ) {
163                                i->cost = i->cvtCost;
164                                alternatives.push_back( *i );
165                        }
166                }
167                std::copy( alternatives.begin(), alternatives.end(), out );
168        }
169
170        Cost sumCost( const AltList &in );
171
172        template< typename InputIterator >
173        void simpleCombineEnvironments( InputIterator begin, InputIterator end, TypeEnvironment &result ) {
174                while ( begin != end ) {
175                        result.simpleCombine( (*begin++).env );
176                }
177        }
178} // namespace ResolvExpr
179
180// Local Variables: //
181// tab-width: 4 //
182// mode: c++ //
183// compile-command: "make install" //
184// End: //
Note: See TracBrowser for help on using the repository browser.