1 | //
|
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2018 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 | // GenericParameter.hpp --
|
---|
8 | //
|
---|
9 | // Author : Andrew Beach
|
---|
10 | // Created On : Fri Mar 21 10:02:00 2022
|
---|
11 | // Last Modified By : Andrew Beach
|
---|
12 | // Last Modified On : Wed Apr 13 10:09:00 2022
|
---|
13 | // Update Count : 0
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include "GenericParameter.hpp"
|
---|
17 |
|
---|
18 | #include "AST/Decl.hpp"
|
---|
19 | #include "AST/Expr.hpp"
|
---|
20 | #include "AST/ParseNode.hpp"
|
---|
21 | #include "AST/Pass.hpp"
|
---|
22 | #include "AST/TranslationUnit.hpp"
|
---|
23 | #include "AST/Type.hpp"
|
---|
24 |
|
---|
25 | namespace Validate {
|
---|
26 |
|
---|
27 | namespace {
|
---|
28 |
|
---|
29 | // Test for special name on a generic parameter. Special treatment for the
|
---|
30 | // special name is a bootstrapping hack. In most cases, the worlds of T's
|
---|
31 | // and of N's don't overlap (normal treamtemt). The foundations in
|
---|
32 | // array.hfa use tagging for both types and dimensions. Tagging treats
|
---|
33 | // its subject parameter even more opaquely than T&, which assumes it is
|
---|
34 | // possible to have a pointer/reference to such an object. Tagging only
|
---|
35 | // seeks to identify the type-system resident at compile time. Both N's
|
---|
36 | // and T's can make tags. The tag definition uses the special name, which
|
---|
37 | // is treated as "an N or a T." This feature is not inteded to be used
|
---|
38 | // outside of the definition and immediate uses of a tag.
|
---|
39 | inline bool isReservedTysysIdOnlyName( const std::string & name ) {
|
---|
40 | // The name might be wrapped in __..._generic so check for that as well.
|
---|
41 | int foundAt = name.find("__CFA_tysys_id_only");
|
---|
42 | if (foundAt == 0) return true;
|
---|
43 | if (foundAt == 2 && name[0] == '_' && name[1] == '_') return true;
|
---|
44 | return false;
|
---|
45 | }
|
---|
46 |
|
---|
47 | template< typename InstType >
|
---|
48 | const InstType * validateGeneric(
|
---|
49 | const CodeLocation & location, const InstType * type ) {
|
---|
50 | const typename InstType::base_type * base = type->base.get();
|
---|
51 | if ( nullptr == base ) {
|
---|
52 | return type;
|
---|
53 | }
|
---|
54 |
|
---|
55 | const std::vector<ast::ptr<ast::TypeDecl>> & params = base->params;
|
---|
56 | if ( params.empty() ) {
|
---|
57 | return type;
|
---|
58 | }
|
---|
59 |
|
---|
60 | // I think I can move this check up, or it should check the result of
|
---|
61 | // the substuition.
|
---|
62 |
|
---|
63 | auto mutType = ast::mutate( type );
|
---|
64 | std::vector<ast::ptr<ast::Expr>> & args = mutType->params;
|
---|
65 |
|
---|
66 | // Quick check before we get into the real work.
|
---|
67 | if ( params.size() < args.size() ) {
|
---|
68 | SemanticError( location, type, "Too many type arguments in generic type " );
|
---|
69 | }
|
---|
70 |
|
---|
71 | // Insert defaults arguments when a type argument is missing (currently
|
---|
72 | // only supports missing arguments at the end of the list).
|
---|
73 | // A substitution is used to ensure that defaults are replaced correctly:
|
---|
74 | // forall(otype T, otype alloc = heap_allocator(T)) struct vector;
|
---|
75 | // vector(int) v;
|
---|
76 | // After insertion of default values becomes:
|
---|
77 | // vector(int, heap_allocator(T))
|
---|
78 | // The substitution is built with T=int so the result is:
|
---|
79 | // vector(int, heap_allocator(int))
|
---|
80 |
|
---|
81 | ast::TypeSubstitution sub;
|
---|
82 | // Build the substution:
|
---|
83 | auto paramIter = params.begin();
|
---|
84 | auto argIter = args.begin();
|
---|
85 | for ( ; paramIter != params.end() ; ++paramIter, ++argIter ) {
|
---|
86 | if ( argIter != args.end() ) {
|
---|
87 | if ( auto expr = argIter->as<ast::TypeExpr>() ) {
|
---|
88 | sub.add( paramIter->get(), ast::deepCopy( expr->type ) );
|
---|
89 | }
|
---|
90 | } else if ( const ast::Type * defaultType = (*paramIter)->init ) {
|
---|
91 | args.push_back( new ast::TypeExpr(
|
---|
92 | location, ast::deepCopy( defaultType ) ) );
|
---|
93 | sub.add( paramIter->get(), ast::deepCopy( defaultType ) );
|
---|
94 | argIter = std::prev( args.end() );
|
---|
95 | } else {
|
---|
96 | SemanticError( location, type, "Too few type arguments in generic type " );
|
---|
97 | }
|
---|
98 | assert( argIter != args.end() );
|
---|
99 | bool typeParamDeclared = (*paramIter)->kind != ast::TypeDecl::Dimension;
|
---|
100 | bool typeArgGiven;
|
---|
101 | if ( isReservedTysysIdOnlyName( (*paramIter)->name ) ) {
|
---|
102 | // Always match when declaration is reserved name, means "either".
|
---|
103 | typeArgGiven = typeParamDeclared;
|
---|
104 | } else {
|
---|
105 | typeArgGiven = argIter->as<ast::TypeExpr>();
|
---|
106 | }
|
---|
107 | if ( !typeParamDeclared && typeArgGiven ) {
|
---|
108 | SemanticError( location, type, "Type argument given for value parameter: " );
|
---|
109 | }
|
---|
110 | if ( typeParamDeclared && !typeArgGiven ) {
|
---|
111 | SemanticError( location, type, "Expression argument given for type parameter: " );
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | // Actually do the application:
|
---|
116 | auto result = sub.apply( mutType );
|
---|
117 | return result.node.release();
|
---|
118 | }
|
---|
119 |
|
---|
120 | struct ValidateGenericParamsCore : public ast::WithGuards {
|
---|
121 | const CodeLocation * locationPtr = nullptr;
|
---|
122 |
|
---|
123 | void previsit( const ast::ParseNode * node ) {
|
---|
124 | GuardValue( locationPtr ) = &node->location;
|
---|
125 | }
|
---|
126 |
|
---|
127 | const ast::StructInstType * previsit( const ast::StructInstType * type ) {
|
---|
128 | assert( locationPtr );
|
---|
129 | return validateGeneric( *locationPtr, type );
|
---|
130 | }
|
---|
131 |
|
---|
132 | const ast::UnionInstType * previsit( const ast::UnionInstType * type ) {
|
---|
133 | assert( locationPtr );
|
---|
134 | return validateGeneric( *locationPtr, type );
|
---|
135 | }
|
---|
136 | };
|
---|
137 |
|
---|
138 | // --------------------------------------------------------------------------
|
---|
139 |
|
---|
140 | // A SymbolTable that only has the operations used in the Translate Dimension
|
---|
141 | // pass. More importantly, it doesn't have some methods that should no be
|
---|
142 | // called by the Pass template (lookupId and addId).
|
---|
143 | class NoIdSymbolTable {
|
---|
144 | ast::SymbolTable base;
|
---|
145 | public:
|
---|
146 | # define FORWARD_X( func, types_and_names, just_names ) \
|
---|
147 | inline auto func types_and_names -> decltype( base.func just_names ) { \
|
---|
148 | return base.func just_names ; \
|
---|
149 | }
|
---|
150 | # define FORWARD_0( func ) FORWARD_X( func, (), () )
|
---|
151 | # define FORWARD_1( func, type ) FORWARD_X( func, (type arg), (arg) )
|
---|
152 | # define FORWARD_2( func, t0, t1 ) FORWARD_X( func, (t0 a0, t1 a1), (a0, a1) )
|
---|
153 |
|
---|
154 | FORWARD_0( enterScope )
|
---|
155 | FORWARD_0( leaveScope )
|
---|
156 | FORWARD_1( lookupType, const std::string & )
|
---|
157 | FORWARD_1( addType , const ast::NamedTypeDecl * )
|
---|
158 | FORWARD_1( addStruct , const ast::StructDecl * )
|
---|
159 | FORWARD_1( addEnum , const ast::EnumDecl * )
|
---|
160 | FORWARD_1( addUnion , const ast::UnionDecl * )
|
---|
161 | FORWARD_1( addTrait , const ast::TraitDecl * )
|
---|
162 | FORWARD_2( addWith , const std::vector< ast::ptr<ast::Expr> > &, const ast::Decl * )
|
---|
163 | };
|
---|
164 |
|
---|
165 | struct TranslateDimensionCore : public ast::WithGuards {
|
---|
166 | NoIdSymbolTable symtab;
|
---|
167 |
|
---|
168 | // SUIT: Struct- or Union- InstType
|
---|
169 | // Situational awareness:
|
---|
170 | // array( float, [[currentExpr]] ) has visitingChildOfSUIT == true
|
---|
171 | // array( float, [[currentExpr]] - 1 ) has visitingChildOfSUIT == false
|
---|
172 | // size_t x = [[currentExpr]] has visitingChildOfSUIT == false
|
---|
173 | bool nextVisitedNodeIsChildOfSUIT = false;
|
---|
174 | bool visitingChildOfSUIT = false;
|
---|
175 | void changeState_ChildOfSUIT( bool newValue ) {
|
---|
176 | GuardValue( visitingChildOfSUIT ) = nextVisitedNodeIsChildOfSUIT;
|
---|
177 | GuardValue( nextVisitedNodeIsChildOfSUIT ) = newValue;
|
---|
178 | }
|
---|
179 |
|
---|
180 | void previsit( const ast::StructInstType * ) {
|
---|
181 | changeState_ChildOfSUIT( true );
|
---|
182 | }
|
---|
183 | void previsit( const ast::UnionInstType * ) {
|
---|
184 | changeState_ChildOfSUIT( true );
|
---|
185 | }
|
---|
186 | void previsit( const ast::Node * ) {
|
---|
187 | changeState_ChildOfSUIT( false );
|
---|
188 | }
|
---|
189 |
|
---|
190 | const ast::TypeDecl * postvisit( const ast::TypeDecl * decl );
|
---|
191 | const ast::Expr * postvisit( const ast::DimensionExpr * expr );
|
---|
192 | const ast::Expr * postvisit( const ast::Expr * expr );
|
---|
193 | const ast::Expr * postvisit( const ast::TypeExpr * expr );
|
---|
194 | };
|
---|
195 |
|
---|
196 | const ast::TypeDecl * TranslateDimensionCore::postvisit(
|
---|
197 | const ast::TypeDecl * decl ) {
|
---|
198 | if ( decl->kind == ast::TypeDecl::Dimension ) {
|
---|
199 | auto mutDecl = ast::mutate( decl );
|
---|
200 | mutDecl->kind = ast::TypeDecl::Dtype;
|
---|
201 | if ( !isReservedTysysIdOnlyName( mutDecl->name ) ) {
|
---|
202 | mutDecl->sized = true;
|
---|
203 | }
|
---|
204 | return mutDecl;
|
---|
205 | }
|
---|
206 | return decl;
|
---|
207 | }
|
---|
208 |
|
---|
209 | // Passing values as dimension arguments: array( float, 7 ) -> array( float, char[ 7 ] )
|
---|
210 | // Consuming dimension parameters: size_t x = N - 1 ; -> size_t x = sizeof(N) - 1 ;
|
---|
211 | // Intertwined reality: array( float, N ) -> array( float, N )
|
---|
212 | // array( float, N - 1 ) -> array( float, char[ sizeof(N) - 1 ] )
|
---|
213 | // Intertwined case 1 is not just an optimization.
|
---|
214 | // Avoiding char[sizeof(-)] is necessary to enable the call of f to bind the value of N, in:
|
---|
215 | // forall([N]) void f( array(float, N) & );
|
---|
216 | // array(float, 7) a;
|
---|
217 | // f(a);
|
---|
218 | const ast::Expr * TranslateDimensionCore::postvisit(
|
---|
219 | const ast::DimensionExpr * expr ) {
|
---|
220 | // Expression `expr` is an occurrence of N in LHS of above examples.
|
---|
221 | // Look up the name that `expr` references.
|
---|
222 | // If we are in a struct body, then this reference can be to an entry of
|
---|
223 | // the stuct's forall list.
|
---|
224 | // Whether or not we are in a struct body, this reference can be to an
|
---|
225 | // entry of a containing function's forall list.
|
---|
226 | // If we are in a struct body, then the stuct's forall declarations are
|
---|
227 | // innermost (functions don't occur in structs).
|
---|
228 | // Thus, a potential struct's declaration is highest priority.
|
---|
229 | // A struct's forall declarations are already renamed with _generic_ suffix.
|
---|
230 | // Try that name variant first.
|
---|
231 |
|
---|
232 | std::string useName = "__" + expr->name + "_generic_";
|
---|
233 | ast::TypeDecl * namedParamDecl = const_cast<ast::TypeDecl *>(
|
---|
234 | strict_dynamic_cast<const ast::TypeDecl *, nullptr >(
|
---|
235 | symtab.lookupType( useName ) ) );
|
---|
236 |
|
---|
237 | if ( !namedParamDecl ) {
|
---|
238 | useName = expr->name;
|
---|
239 | namedParamDecl = const_cast<ast::TypeDecl *>( strict_dynamic_cast<const ast::TypeDecl *, nullptr >( symtab.lookupType( useName ) ) );
|
---|
240 | }
|
---|
241 |
|
---|
242 | // Expect to find it always.
|
---|
243 | // A misspelled name would have been parsed as an identifier.
|
---|
244 | assertf( namedParamDecl, "Type-system-managed value name not found in symbol table" );
|
---|
245 |
|
---|
246 | auto * refToDecl = new ast::TypeInstType( useName, namedParamDecl );
|
---|
247 |
|
---|
248 | if ( visitingChildOfSUIT ) {
|
---|
249 | // As in postvisit( Expr * ), topmost expression needs a TypeExpr
|
---|
250 | // wrapper. But avoid ArrayType-Sizeof.
|
---|
251 | return new ast::TypeExpr( expr->location, refToDecl );
|
---|
252 | } else {
|
---|
253 | // the N occurrence is being used directly as a runtime value,
|
---|
254 | // if we are in a type instantiation, then the N is within a bigger value computation
|
---|
255 | return new ast::SizeofExpr( expr->location, refToDecl );
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | const ast::Expr * TranslateDimensionCore::postvisit(
|
---|
260 | const ast::Expr * expr ) {
|
---|
261 | // This expression is used as an argument to instantiate a type.
|
---|
262 | if ( visitingChildOfSUIT ) {
|
---|
263 | // DimensionExpr and TypeExpr should not reach here.
|
---|
264 | return new ast::TypeExpr( expr->location,
|
---|
265 | new ast::ArrayType(
|
---|
266 | new ast::BasicType( ast::BasicType::Char ),
|
---|
267 | expr,
|
---|
268 | ast::VariableLen,
|
---|
269 | ast::DynamicDim
|
---|
270 | )
|
---|
271 | );
|
---|
272 | }
|
---|
273 | return expr;
|
---|
274 | }
|
---|
275 |
|
---|
276 | const ast::Expr * TranslateDimensionCore::postvisit(
|
---|
277 | const ast::TypeExpr * expr ) {
|
---|
278 | // Does nothing, except prevents matching ast::Expr (above).
|
---|
279 | return expr;
|
---|
280 | }
|
---|
281 |
|
---|
282 | } // namespace
|
---|
283 |
|
---|
284 | void fillGenericParameters( ast::TranslationUnit & translationUnit ) {
|
---|
285 | ast::Pass<ValidateGenericParamsCore>::run( translationUnit );
|
---|
286 | }
|
---|
287 |
|
---|
288 | void translateDimensionParameters( ast::TranslationUnit & translationUnit ) {
|
---|
289 | ast::Pass<TranslateDimensionCore>::run( translationUnit );
|
---|
290 | }
|
---|
291 |
|
---|
292 | } // namespace Validate
|
---|
293 |
|
---|
294 | // Local Variables: //
|
---|
295 | // tab-width: 4 //
|
---|
296 | // mode: c++ //
|
---|
297 | // compile-command: "make install" //
|
---|
298 | // End: //
|
---|