source: src/ResolvExpr/AlternativeFinder.h@ f203a7a

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since f203a7a was 452747a, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Merge branch 'master' of plg.uwaterloo.ca:/u/cforall/software/cfa/cfa-cc

  • Property mode set to 100644
File size: 7.9 KB
RevLine 
[a32b204]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//
[dc2e7e0]7// AlternativeFinder.h --
[a32b204]8//
9// Author : Richard C. Bilson
10// Created On : Sat May 16 23:56:12 2015
[a5f0529]11// Last Modified By : Andrew Beach
12// Last Modified On : Wed Jul 26 11:24:00 2017
13// Update Count : 4
[dc2e7e0]14//
[a32b204]15
[6b0b624]16#pragma once
[51b73452]17
[ea6332d]18#include <algorithm> // for copy
19#include <list> // for list
20#include <string> // for string
[51b73452]21
[ea6332d]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
[51b73452]31
32namespace ResolvExpr {
[452747a]33 struct ArgPack;
34
[a32b204]35 class AlternativeFinder : public Visitor {
36 public:
37 AlternativeFinder( const SymTab::Indexer &indexer, const TypeEnvironment &env );
[aeb75b1]38
39 AlternativeFinder( const AlternativeFinder& o )
[452747a]40 : indexer(o.indexer), alternatives(o.alternatives), env(o.env),
[aeb75b1]41 targetType(o.targetType) {}
[452747a]42
[aeb75b1]43 AlternativeFinder( AlternativeFinder&& o )
[452747a]44 : indexer(o.indexer), alternatives(std::move(o.alternatives)), env(o.env),
[aeb75b1]45 targetType(o.targetType) {}
[452747a]46
[aeb75b1]47 AlternativeFinder& operator= ( const AlternativeFinder& o ) {
48 if (&o == this) return *this;
[452747a]49
[aeb75b1]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;
[452747a]58
[aeb75b1]59 // horrific nasty hack to rebind references...
60 alternatives.~AltList();
61 new(this) AlternativeFinder(std::move(o));
62 return *this;
63 }
64
[4e66a18]65 void find( Expression *expr, bool adjust = false, bool prune = true, bool failFast = true );
[0f19d763]66 /// Calls find with the adjust flag set; adjustment turns array and function types into equivalent pointer types
[4e66a18]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 );
[a32b204]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(); }
[51b73452]82
[a32b204]83 const SymTab::Indexer &get_indexer() const { return indexer; }
84 const TypeEnvironment &get_environ() const { return env; }
[1dcd9554]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 );
[a32b204]90 private:
91 virtual void visit( ApplicationExpr *applicationExpr );
92 virtual void visit( UntypedExpr *untypedExpr );
93 virtual void visit( AddressExpr *addressExpr );
[5809461]94 virtual void visit( LabelAddressExpr *labelExpr );
[a32b204]95 virtual void visit( CastExpr *castExpr );
[a5f0529]96 virtual void visit( VirtualCastExpr *castExpr );
[a32b204]97 virtual void visit( UntypedMemberExpr *memberExpr );
98 virtual void visit( MemberExpr *memberExpr );
99 virtual void visit( NameExpr *variableExpr );
100 virtual void visit( VariableExpr *variableExpr );
[dc2e7e0]101 virtual void visit( ConstantExpr *constantExpr );
[a32b204]102 virtual void visit( SizeofExpr *sizeofExpr );
[25a054f]103 virtual void visit( AlignofExpr *alignofExpr );
[2a4b088]104 virtual void visit( UntypedOffsetofExpr *offsetofExpr );
[25a054f]105 virtual void visit( OffsetofExpr *offsetofExpr );
[afc1045]106 virtual void visit( OffsetPackExpr *offsetPackExpr );
[a32b204]107 virtual void visit( AttrExpr *attrExpr );
108 virtual void visit( LogicalExpr *logicalExpr );
109 virtual void visit( ConditionalExpr *conditionalExpr );
110 virtual void visit( CommaExpr *commaExpr );
[dc2e7e0]111 virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr );
[b6fe7e6]112 virtual void visit( ConstructorExpr * ctorExpr );
[8a5cad8]113 virtual void visit( RangeExpr * rangeExpr );
[907eccb]114 virtual void visit( UntypedTupleExpr *tupleExpr );
115 virtual void visit( TupleExpr *tupleExpr );
[8f7cea1]116 virtual void visit( TupleIndexExpr *tupleExpr );
[aa8f9df]117 virtual void visit( TupleAssignExpr *tupleExpr );
[bf32bb8]118 virtual void visit( UniqueExpr *unqExpr );
[722617d]119 virtual void visit( StmtExpr *stmtExpr );
[e4d829b]120 virtual void visit( UntypedInitExpr *initExpr );
[51b73452]121
[4b0f997]122 /// Adds alternatives for anonymous members
123 void addAnonConversions( const Alternative & alt );
[2a4b088]124 /// Adds alternatives for member expressions, given the aggregate, conversion cost for that aggregate, and name of the member
[add7117]125 template< typename StructOrUnionType > void addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member );
[848ce71]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 );
[2a4b088]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 );
[403b388]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
[e472d54]134 template<typename OutputIterator>
[aeb75b1]135 void makeFunctionAlternatives( const Alternative &func, FunctionType *funcType, const std::vector< AlternativeFinder >& args, OutputIterator out );
[403b388]136 /// Checks if assertion parameters match for a new alternative
[a32b204]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 );
[51b73452]140
[a32b204]141 const SymTab::Indexer &indexer;
142 AltList alternatives;
143 const TypeEnvironment &env;
[7933351]144 Type * targetType = nullptr;
[a32b204]145 }; // AlternativeFinder
146
147 Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer, TypeEnvironment &env );
[1dcd9554]148 void referenceToRvalueConversion( Expression *& expr );
[5af62f1]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 }
[908cc83]169
170 Cost sumCost( const AltList &in );
[ac9ca96]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 }
[51b73452]178} // namespace ResolvExpr
179
[a32b204]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.