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 | // GenPoly.cc --
|
---|
8 | //
|
---|
9 | // Author : Richard C. Bilson
|
---|
10 | // Created On : Mon May 18 07:44:20 2015
|
---|
11 | // Last Modified By : Rob Schluntz
|
---|
12 | // Last Modified On : Wed May 25 13:39:21 2016
|
---|
13 | // Update Count : 13
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include "GenPoly.h"
|
---|
17 |
|
---|
18 | #include "SynTree/Expression.h"
|
---|
19 | #include "SynTree/Type.h"
|
---|
20 |
|
---|
21 | #include <iostream>
|
---|
22 | using namespace std;
|
---|
23 |
|
---|
24 | namespace GenPoly {
|
---|
25 | bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) {
|
---|
26 | if ( ! adaptee->get_returnVals().empty() && isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
|
---|
27 | return true;
|
---|
28 | } // if
|
---|
29 | for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); innerArg != adaptee->get_parameters().end(); ++innerArg ) {
|
---|
30 | if ( isPolyType( (*innerArg)->get_type(), tyVars ) ) {
|
---|
31 | return true;
|
---|
32 | } // if
|
---|
33 | } // for
|
---|
34 | return false;
|
---|
35 | }
|
---|
36 |
|
---|
37 | ReferenceToType *isPolyRet( FunctionType *function ) {
|
---|
38 | if ( ! function->get_returnVals().empty() ) {
|
---|
39 | TyVarMap forallTypes( (TypeDecl::Kind)-1 );
|
---|
40 | makeTyVarMap( function, forallTypes );
|
---|
41 | return (ReferenceToType*)isPolyType( function->get_returnVals().front()->get_type(), forallTypes );
|
---|
42 | } // if
|
---|
43 | return 0;
|
---|
44 | }
|
---|
45 |
|
---|
46 | namespace {
|
---|
47 | /// Checks a parameter list for polymorphic parameters; will substitute according to env if present
|
---|
48 | bool hasPolyParams( std::list< Expression* >& params, const TypeSubstitution *env ) {
|
---|
49 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
|
---|
50 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
|
---|
51 | assert(paramType && "Aggregate parameters should be type expressions");
|
---|
52 | if ( isPolyType( paramType->get_type(), env ) ) return true;
|
---|
53 | }
|
---|
54 | return false;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /// Checks a parameter list for polymorphic parameters from tyVars; will substitute according to env if present
|
---|
58 | bool hasPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) {
|
---|
59 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
|
---|
60 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
|
---|
61 | assert(paramType && "Aggregate parameters should be type expressions");
|
---|
62 | if ( isPolyType( paramType->get_type(), tyVars, env ) ) return true;
|
---|
63 | }
|
---|
64 | return false;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /// Replaces a TypeInstType by its referrent in the environment, if applicable
|
---|
68 | Type* replaceTypeInst( Type* type, const TypeSubstitution* env ) {
|
---|
69 | if ( ! env ) return type;
|
---|
70 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) {
|
---|
71 | Type *newType = env->lookup( typeInst->get_name() );
|
---|
72 | if ( newType ) return newType;
|
---|
73 | }
|
---|
74 | return type;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | Type *isPolyType( Type *type, const TypeSubstitution *env ) {
|
---|
79 | type = replaceTypeInst( type, env );
|
---|
80 |
|
---|
81 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
|
---|
82 | return type;
|
---|
83 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
|
---|
84 | if ( hasPolyParams( structType->get_parameters(), env ) ) return type;
|
---|
85 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
|
---|
86 | if ( hasPolyParams( unionType->get_parameters(), env ) ) return type;
|
---|
87 | }
|
---|
88 | return 0;
|
---|
89 | }
|
---|
90 |
|
---|
91 | Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
|
---|
92 | type = replaceTypeInst( type, env );
|
---|
93 |
|
---|
94 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
|
---|
95 | if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) {
|
---|
96 | return type;
|
---|
97 | }
|
---|
98 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
|
---|
99 | if ( hasPolyParams( structType->get_parameters(), tyVars, env ) ) return type;
|
---|
100 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
|
---|
101 | if ( hasPolyParams( unionType->get_parameters(), tyVars, env ) ) return type;
|
---|
102 | }
|
---|
103 | return 0;
|
---|
104 | }
|
---|
105 |
|
---|
106 | Type *isPolyPtr( Type *type, const TypeSubstitution *env ) {
|
---|
107 | type = replaceTypeInst( type, env );
|
---|
108 |
|
---|
109 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
|
---|
110 | return isPolyType( ptr->get_base(), env );
|
---|
111 | }
|
---|
112 | return 0;
|
---|
113 | }
|
---|
114 |
|
---|
115 | Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
|
---|
116 | type = replaceTypeInst( type, env );
|
---|
117 |
|
---|
118 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
|
---|
119 | return isPolyType( ptr->get_base(), tyVars, env );
|
---|
120 | }
|
---|
121 | return 0;
|
---|
122 | }
|
---|
123 |
|
---|
124 | Type * hasPolyBase( Type *type, int *levels, const TypeSubstitution *env ) {
|
---|
125 | int dummy;
|
---|
126 | if ( ! levels ) { levels = &dummy; }
|
---|
127 | *levels = 0;
|
---|
128 |
|
---|
129 | while ( true ) {
|
---|
130 | type = replaceTypeInst( type, env );
|
---|
131 |
|
---|
132 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
|
---|
133 | type = ptr->get_base();
|
---|
134 | ++(*levels);
|
---|
135 | } else break;
|
---|
136 | }
|
---|
137 |
|
---|
138 | return isPolyType( type, env );
|
---|
139 | }
|
---|
140 |
|
---|
141 | Type * hasPolyBase( Type *type, const TyVarMap &tyVars, int *levels, const TypeSubstitution *env ) {
|
---|
142 | int dummy;
|
---|
143 | if ( ! levels ) { levels = &dummy; }
|
---|
144 | *levels = 0;
|
---|
145 |
|
---|
146 | while ( true ) {
|
---|
147 | type = replaceTypeInst( type, env );
|
---|
148 |
|
---|
149 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
|
---|
150 | type = ptr->get_base();
|
---|
151 | ++(*levels);
|
---|
152 | } else break;
|
---|
153 | }
|
---|
154 |
|
---|
155 | return isPolyType( type, tyVars, env );
|
---|
156 | }
|
---|
157 |
|
---|
158 | FunctionType * getFunctionType( Type *ty ) {
|
---|
159 | PointerType *ptrType;
|
---|
160 | if ( ( ptrType = dynamic_cast< PointerType* >( ty ) ) ) {
|
---|
161 | return dynamic_cast< FunctionType* >( ptrType->get_base() ); // pointer if FunctionType, NULL otherwise
|
---|
162 | } else {
|
---|
163 | return dynamic_cast< FunctionType* >( ty ); // pointer if FunctionType, NULL otherwise
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | VariableExpr * getBaseVar( Expression *expr, int *levels ) {
|
---|
168 | int dummy;
|
---|
169 | if ( ! levels ) { levels = &dummy; }
|
---|
170 | *levels = 0;
|
---|
171 |
|
---|
172 | while ( true ) {
|
---|
173 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( expr ) ) {
|
---|
174 | return varExpr;
|
---|
175 | } else if ( MemberExpr *memberExpr = dynamic_cast< MemberExpr* >( expr ) ) {
|
---|
176 | expr = memberExpr->get_aggregate();
|
---|
177 | } else if ( AddressExpr *addressExpr = dynamic_cast< AddressExpr* >( expr ) ) {
|
---|
178 | expr = addressExpr->get_arg();
|
---|
179 | } else if ( UntypedExpr *untypedExpr = dynamic_cast< UntypedExpr* >( expr ) ) {
|
---|
180 | // look for compiler-inserted dereference operator
|
---|
181 | NameExpr *fn = dynamic_cast< NameExpr* >( untypedExpr->get_function() );
|
---|
182 | if ( ! fn || fn->get_name() != std::string("*?") ) return 0;
|
---|
183 | expr = *untypedExpr->begin_args();
|
---|
184 | } else if ( CommaExpr *commaExpr = dynamic_cast< CommaExpr* >( expr ) ) {
|
---|
185 | // copy constructors insert comma exprs, look at second argument which contains the variable
|
---|
186 | expr = commaExpr->get_arg2();
|
---|
187 | continue;
|
---|
188 | } else break;
|
---|
189 |
|
---|
190 | ++(*levels);
|
---|
191 | }
|
---|
192 |
|
---|
193 | return 0;
|
---|
194 | }
|
---|
195 |
|
---|
196 | void makeTyVarMap( Type *type, TyVarMap &tyVarMap ) {
|
---|
197 | for ( std::list< TypeDecl* >::const_iterator tyVar = type->get_forall().begin(); tyVar != type->get_forall().end(); ++tyVar ) {
|
---|
198 | assert( *tyVar );
|
---|
199 | tyVarMap[ (*tyVar)->get_name() ] = (*tyVar)->get_kind();
|
---|
200 | }
|
---|
201 | if ( PointerType *pointer = dynamic_cast< PointerType* >( type ) ) {
|
---|
202 | makeTyVarMap( pointer->get_base(), tyVarMap );
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) {
|
---|
207 | for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) {
|
---|
208 | os << i->first << " (" << i->second << ") ";
|
---|
209 | } // for
|
---|
210 | os << std::endl;
|
---|
211 | }
|
---|
212 |
|
---|
213 | } // namespace GenPoly
|
---|
214 |
|
---|
215 | // Local Variables: //
|
---|
216 | // tab-width: 4 //
|
---|
217 | // mode: c++ //
|
---|
218 | // compile-command: "make install" //
|
---|
219 | // End: //
|
---|