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 | // Autogen.cpp -- Generate automatic routines for data types. |
---|
8 | // |
---|
9 | // Author : Andrew Beach |
---|
10 | // Created On : Thu Dec 2 13:44:00 2021 |
---|
11 | // Last Modified By : Andrew Beach |
---|
12 | // Last Modified On : Tue Sep 20 16:00:00 2022 |
---|
13 | // Update Count : 2 |
---|
14 | // |
---|
15 | |
---|
16 | #include "Autogen.hpp" |
---|
17 | |
---|
18 | #include <algorithm> // for count_if |
---|
19 | #include <cassert> // for strict_dynamic_cast, assert, assertf |
---|
20 | #include <iterator> // for back_insert_iterator, back_inserter |
---|
21 | #include <list> // for list, _List_iterator, list<>::iter... |
---|
22 | #include <set> // for set, _Rb_tree_const_iterator |
---|
23 | #include <utility> // for pair |
---|
24 | #include <vector> // for vector |
---|
25 | |
---|
26 | #include "AST/Attribute.hpp" |
---|
27 | #include "AST/Copy.hpp" |
---|
28 | #include "AST/Create.hpp" |
---|
29 | #include "AST/Decl.hpp" |
---|
30 | #include "AST/DeclReplacer.hpp" |
---|
31 | #include "AST/Expr.hpp" |
---|
32 | #include "AST/Inspect.hpp" |
---|
33 | #include "AST/Pass.hpp" |
---|
34 | #include "AST/Stmt.hpp" |
---|
35 | #include "AST/SymbolTable.hpp" |
---|
36 | #include "CodeGen/OperatorTable.h" // for isCtorDtor, isCtorDtorAssign |
---|
37 | #include "Common/ScopedMap.h" // for ScopedMap<>::const_iterator, Scope... |
---|
38 | #include "Common/utility.h" // for cloneAll, operator+ |
---|
39 | #include "GenPoly/ScopedSet.h" // for ScopedSet, ScopedSet<>::iterator |
---|
40 | #include "InitTweak/GenInit.h" // for fixReturnStatements |
---|
41 | #include "InitTweak/InitTweak.h" // for isAssignment, isCopyConstructor |
---|
42 | #include "SymTab/GenImplicitCall.hpp" // for genImplicitCall |
---|
43 | #include "SymTab/Mangler.h" // for Mangler |
---|
44 | #include "CompilationState.h" |
---|
45 | |
---|
46 | namespace Validate { |
---|
47 | |
---|
48 | namespace { |
---|
49 | |
---|
50 | // -------------------------------------------------------------------------- |
---|
51 | struct AutogenerateRoutines final : |
---|
52 | public ast::WithDeclsToAdd<>, |
---|
53 | public ast::WithShortCircuiting { |
---|
54 | void previsit( const ast::EnumDecl * enumDecl ); |
---|
55 | void previsit( const ast::StructDecl * structDecl ); |
---|
56 | void previsit( const ast::UnionDecl * structDecl ); |
---|
57 | void previsit( const ast::TypeDecl * typeDecl ); |
---|
58 | void previsit( const ast::TraitDecl * traitDecl ); |
---|
59 | void previsit( const ast::FunctionDecl * functionDecl ); |
---|
60 | void postvisit( const ast::FunctionDecl * functionDecl ); |
---|
61 | |
---|
62 | private: |
---|
63 | // Current level of nested functions. |
---|
64 | unsigned int functionNesting = 0; |
---|
65 | }; |
---|
66 | |
---|
67 | // -------------------------------------------------------------------------- |
---|
68 | /// Class used to generate functions for a particular declaration. |
---|
69 | /// Note it isn't really stored, it is just a class for organization and to |
---|
70 | /// help pass around some of the common arguments. |
---|
71 | class FuncGenerator { |
---|
72 | public: |
---|
73 | std::list<ast::ptr<ast::Decl>> forwards; |
---|
74 | std::list<ast::ptr<ast::Decl>> definitions; |
---|
75 | |
---|
76 | FuncGenerator( const ast::Type * type, unsigned int functionNesting ) : |
---|
77 | type( type ), functionNesting( functionNesting ) |
---|
78 | {} |
---|
79 | |
---|
80 | /// Generate functions (and forward decls.) and append them to the list. |
---|
81 | void generateAndAppendFunctions( std::list<ast::ptr<ast::Decl>> & ); |
---|
82 | |
---|
83 | virtual bool shouldAutogen() const = 0; |
---|
84 | protected: |
---|
85 | const ast::Type * type; |
---|
86 | unsigned int functionNesting; |
---|
87 | ast::Linkage::Spec proto_linkage = ast::Linkage::AutoGen; |
---|
88 | |
---|
89 | // Internal helpers: |
---|
90 | void genStandardFuncs(); |
---|
91 | void produceDecl( const ast::FunctionDecl * decl ); |
---|
92 | void produceForwardDecl( const ast::FunctionDecl * decl ); |
---|
93 | |
---|
94 | const CodeLocation& getLocation() const { return getDecl()->location; } |
---|
95 | ast::FunctionDecl * genProto( std::string&& name, |
---|
96 | std::vector<ast::ptr<ast::DeclWithType>>&& params, |
---|
97 | std::vector<ast::ptr<ast::DeclWithType>>&& returns ) const; |
---|
98 | |
---|
99 | ast::ObjectDecl * dstParam() const; |
---|
100 | ast::ObjectDecl * srcParam() const; |
---|
101 | ast::FunctionDecl * genCtorProto() const; |
---|
102 | ast::FunctionDecl * genCopyProto() const; |
---|
103 | ast::FunctionDecl * genDtorProto() const; |
---|
104 | ast::FunctionDecl * genAssignProto() const; |
---|
105 | ast::FunctionDecl * genFieldCtorProto( unsigned int fields ) const; |
---|
106 | |
---|
107 | // Internal Hooks: |
---|
108 | virtual void genFuncBody( ast::FunctionDecl * decl ) = 0; |
---|
109 | virtual void genFieldCtors() = 0; |
---|
110 | virtual bool isConcurrentType() const { return false; } |
---|
111 | virtual const ast::Decl * getDecl() const = 0; |
---|
112 | }; |
---|
113 | |
---|
114 | class StructFuncGenerator final : public FuncGenerator { |
---|
115 | const ast::StructDecl * decl; |
---|
116 | public: |
---|
117 | StructFuncGenerator( const ast::StructDecl * decl, |
---|
118 | const ast::StructInstType * type, |
---|
119 | unsigned int functionNesting ) : |
---|
120 | FuncGenerator( type, functionNesting ), decl( decl ) |
---|
121 | {} |
---|
122 | |
---|
123 | // Built-ins do not use autogeneration. |
---|
124 | bool shouldAutogen() const final { return !decl->linkage.is_builtin && !structHasFlexibleArray(decl); } |
---|
125 | private: |
---|
126 | void genFuncBody( ast::FunctionDecl * decl ) final; |
---|
127 | void genFieldCtors() final; |
---|
128 | bool isConcurrentType() const final { |
---|
129 | return decl->is_thread() || decl->is_monitor(); |
---|
130 | } |
---|
131 | virtual const ast::Decl * getDecl() const final { return decl; } |
---|
132 | |
---|
133 | /// Generates a single struct member operation. |
---|
134 | /// (constructor call, destructor call, assignment call) |
---|
135 | // This is managed because it uses another helper that returns a ast::ptr. |
---|
136 | ast::ptr<ast::Stmt> makeMemberOp( |
---|
137 | const CodeLocation& location, |
---|
138 | const ast::ObjectDecl * dstParam, const ast::Expr * src, |
---|
139 | const ast::ObjectDecl * field, ast::FunctionDecl * func, |
---|
140 | SymTab::LoopDirection direction ); |
---|
141 | |
---|
142 | /// Generates the body of a struct function by iterating the struct members |
---|
143 | /// (via parameters). Generates default constructor, copy constructor, |
---|
144 | /// copy assignment, and destructor bodies. No field constructor bodies. |
---|
145 | template<typename Iterator> |
---|
146 | void makeFunctionBody( Iterator member, Iterator end, |
---|
147 | ast::FunctionDecl * func, SymTab::LoopDirection direction ); |
---|
148 | |
---|
149 | /// Generate the body of a constructor which takes parameters that match |
---|
150 | /// fields. (With arguments for one to all of the fields.) |
---|
151 | template<typename Iterator> |
---|
152 | void makeFieldCtorBody( Iterator member, Iterator end, |
---|
153 | ast::FunctionDecl * func ); |
---|
154 | }; |
---|
155 | |
---|
156 | class UnionFuncGenerator final : public FuncGenerator { |
---|
157 | const ast::UnionDecl * decl; |
---|
158 | public: |
---|
159 | UnionFuncGenerator( const ast::UnionDecl * decl, |
---|
160 | const ast::UnionInstType * type, |
---|
161 | unsigned int functionNesting ) : |
---|
162 | FuncGenerator( type, functionNesting ), decl( decl ) |
---|
163 | {} |
---|
164 | |
---|
165 | // Built-ins do not use autogeneration. |
---|
166 | bool shouldAutogen() const final { return !decl->linkage.is_builtin; } |
---|
167 | private: |
---|
168 | void genFuncBody( ast::FunctionDecl * decl ) final; |
---|
169 | void genFieldCtors() final; |
---|
170 | const ast::Decl * getDecl() const final { return decl; } |
---|
171 | |
---|
172 | /// Generate a single union assignment expression (using memcpy). |
---|
173 | ast::ExprStmt * makeAssignOp( const CodeLocation& location, |
---|
174 | const ast::ObjectDecl * dstParam, const ast::ObjectDecl * srcParam ); |
---|
175 | }; |
---|
176 | |
---|
177 | class EnumFuncGenerator final : public FuncGenerator { |
---|
178 | const ast::EnumDecl * decl; |
---|
179 | public: |
---|
180 | EnumFuncGenerator( const ast::EnumDecl * decl, |
---|
181 | const ast::EnumInstType * type, |
---|
182 | unsigned int functionNesting ) : |
---|
183 | FuncGenerator( type, functionNesting ), decl( decl ) |
---|
184 | { |
---|
185 | // TODO: These functions are somewhere between instrinsic and autogen, |
---|
186 | // could possibly use a new linkage type. For now we just make the |
---|
187 | // basic ones intrinsic to code-gen them as C assignments. |
---|
188 | // const auto & real_type = decl->base; |
---|
189 | // const auto & basic = real_type.as<ast::BasicType>(); |
---|
190 | |
---|
191 | // if(!real_type || (basic && basic->isInteger())) proto_linkage = ast::Linkage::Intrinsic; |
---|
192 | |
---|
193 | // Turns other enumeration type into Intrinstic as well to temporarily fix the recursive |
---|
194 | // construction bug |
---|
195 | proto_linkage = ast::Linkage::Intrinsic; |
---|
196 | } |
---|
197 | |
---|
198 | bool shouldAutogen() const final { return true; } |
---|
199 | private: |
---|
200 | void genFuncBody( ast::FunctionDecl * decl ) final; |
---|
201 | void genFieldCtors() final; |
---|
202 | const ast::Decl * getDecl() const final { return decl; } |
---|
203 | }; |
---|
204 | |
---|
205 | class TypeFuncGenerator final : public FuncGenerator { |
---|
206 | const ast::TypeDecl * decl; |
---|
207 | public: |
---|
208 | TypeFuncGenerator( const ast::TypeDecl * decl, |
---|
209 | ast::TypeInstType * type, |
---|
210 | unsigned int functionNesting ) : |
---|
211 | FuncGenerator( type, functionNesting ), decl( decl ) |
---|
212 | {} |
---|
213 | |
---|
214 | bool shouldAutogen() const final { return true; } |
---|
215 | void genFieldCtors() final; |
---|
216 | private: |
---|
217 | void genFuncBody( ast::FunctionDecl * decl ) final; |
---|
218 | const ast::Decl * getDecl() const final { return decl; } |
---|
219 | }; |
---|
220 | |
---|
221 | // -------------------------------------------------------------------------- |
---|
222 | const std::vector<ast::ptr<ast::TypeDecl>>& getGenericParams( const ast::Type * t ) { |
---|
223 | if ( auto inst = dynamic_cast< const ast::StructInstType * >( t ) ) { |
---|
224 | return inst->base->params; |
---|
225 | } else if ( auto inst = dynamic_cast< const ast::UnionInstType * >( t ) ) { |
---|
226 | return inst->base->params; |
---|
227 | } |
---|
228 | static std::vector<ast::ptr<ast::TypeDecl>> const empty; |
---|
229 | return empty; |
---|
230 | } |
---|
231 | |
---|
232 | /// Changes the node inside a pointer so that it has the unused attribute. |
---|
233 | void addUnusedAttribute( ast::ptr<ast::DeclWithType> & declPtr ) { |
---|
234 | ast::DeclWithType * decl = declPtr.get_and_mutate(); |
---|
235 | decl->attributes.push_back( new ast::Attribute( "unused" ) ); |
---|
236 | } |
---|
237 | |
---|
238 | // -------------------------------------------------------------------------- |
---|
239 | void AutogenerateRoutines::previsit( const ast::EnumDecl * enumDecl ) { |
---|
240 | // Must visit children (enum constants) to add them to the symbol table. |
---|
241 | if ( !enumDecl->body ) return; |
---|
242 | |
---|
243 | // if ( auto enumBaseType = enumDecl->base ) { |
---|
244 | // if ( auto enumBaseTypeAsStructInst = dynamic_cast<const ast::StructInstType *>(enumBaseType.get()) ) { |
---|
245 | // const ast::StructDecl * structDecl = enumBaseTypeAsStructInst->base.get(); |
---|
246 | // this->previsit( structDecl ); |
---|
247 | // } |
---|
248 | // } |
---|
249 | |
---|
250 | ast::EnumInstType enumInst( enumDecl->name ); |
---|
251 | enumInst.base = enumDecl; |
---|
252 | EnumFuncGenerator gen( enumDecl, &enumInst, functionNesting ); |
---|
253 | gen.generateAndAppendFunctions( declsToAddAfter ); |
---|
254 | } |
---|
255 | |
---|
256 | void AutogenerateRoutines::previsit( const ast::StructDecl * structDecl ) { |
---|
257 | visit_children = false; |
---|
258 | if ( !structDecl->body ) return; |
---|
259 | |
---|
260 | ast::StructInstType structInst( structDecl->name ); |
---|
261 | structInst.base = structDecl; |
---|
262 | for ( const ast::TypeDecl * typeDecl : structDecl->params ) { |
---|
263 | structInst.params.push_back( new ast::TypeExpr( |
---|
264 | typeDecl->location, |
---|
265 | new ast::TypeInstType( typeDecl ) |
---|
266 | ) ); |
---|
267 | } |
---|
268 | StructFuncGenerator gen( structDecl, &structInst, functionNesting ); |
---|
269 | gen.generateAndAppendFunctions( declsToAddAfter ); |
---|
270 | } |
---|
271 | |
---|
272 | void AutogenerateRoutines::previsit( const ast::UnionDecl * unionDecl ) { |
---|
273 | visit_children = false; |
---|
274 | if ( !unionDecl->body ) return; |
---|
275 | |
---|
276 | ast::UnionInstType unionInst( unionDecl->name ); |
---|
277 | unionInst.base = unionDecl; |
---|
278 | for ( const ast::TypeDecl * typeDecl : unionDecl->params ) { |
---|
279 | unionInst.params.push_back( new ast::TypeExpr( |
---|
280 | unionDecl->location, |
---|
281 | new ast::TypeInstType( typeDecl ) |
---|
282 | ) ); |
---|
283 | } |
---|
284 | UnionFuncGenerator gen( unionDecl, &unionInst, functionNesting ); |
---|
285 | gen.generateAndAppendFunctions( declsToAddAfter ); |
---|
286 | } |
---|
287 | |
---|
288 | /// Generate ctor/dtors/assign for typedecls, e.g., otype T = int *; |
---|
289 | void AutogenerateRoutines::previsit( const ast::TypeDecl * typeDecl ) { |
---|
290 | if ( !typeDecl->base ) return; |
---|
291 | |
---|
292 | ast::TypeInstType refType( typeDecl->name, typeDecl ); |
---|
293 | TypeFuncGenerator gen( typeDecl, &refType, functionNesting ); |
---|
294 | gen.generateAndAppendFunctions( declsToAddAfter ); |
---|
295 | } |
---|
296 | |
---|
297 | void AutogenerateRoutines::previsit( const ast::TraitDecl * ) { |
---|
298 | // Ensure that we don't add assignment ops for types defined as part of the trait |
---|
299 | visit_children = false; |
---|
300 | } |
---|
301 | |
---|
302 | void AutogenerateRoutines::previsit( const ast::FunctionDecl * ) { |
---|
303 | // Track whether we're currently in a function. |
---|
304 | // Can ignore function type idiosyncrasies, because function type can never |
---|
305 | // declare a new type. |
---|
306 | functionNesting += 1; |
---|
307 | } |
---|
308 | |
---|
309 | void AutogenerateRoutines::postvisit( const ast::FunctionDecl * ) { |
---|
310 | functionNesting -= 1; |
---|
311 | } |
---|
312 | |
---|
313 | void FuncGenerator::generateAndAppendFunctions( |
---|
314 | std::list<ast::ptr<ast::Decl>> & decls ) { |
---|
315 | if ( !shouldAutogen() ) return; |
---|
316 | |
---|
317 | // Generate the functions (they go into forwards and definitions). |
---|
318 | genStandardFuncs(); |
---|
319 | genFieldCtors(); |
---|
320 | |
---|
321 | // Now export the lists contents. |
---|
322 | decls.splice( decls.end(), forwards ); |
---|
323 | decls.splice( decls.end(), definitions ); |
---|
324 | } |
---|
325 | |
---|
326 | void FuncGenerator::produceDecl( const ast::FunctionDecl * decl ) { |
---|
327 | assert( nullptr != decl->stmts ); |
---|
328 | assert( decl->type_params.size() == getGenericParams( type ).size() ); |
---|
329 | |
---|
330 | definitions.push_back( decl ); |
---|
331 | } |
---|
332 | |
---|
333 | /// Make a forward declaration of the decl and add it to forwards. |
---|
334 | void FuncGenerator::produceForwardDecl( const ast::FunctionDecl * decl ) { |
---|
335 | if (0 != functionNesting) return; |
---|
336 | ast::FunctionDecl * fwd = |
---|
337 | ( decl->stmts ) ? ast::asForward( decl ) : ast::deepCopy( decl ) ; |
---|
338 | fwd->fixUniqueId(); |
---|
339 | forwards.push_back( fwd ); |
---|
340 | } |
---|
341 | |
---|
342 | void replaceAll( std::vector<ast::ptr<ast::DeclWithType>> & dwts, |
---|
343 | const ast::DeclReplacer::TypeMap & map ) { |
---|
344 | for ( auto & dwt : dwts ) { |
---|
345 | dwt = strict_dynamic_cast<const ast::DeclWithType *>( |
---|
346 | ast::DeclReplacer::replace( dwt, map ) ); |
---|
347 | } |
---|
348 | } |
---|
349 | |
---|
350 | /// Generates a basic prototype function declaration. |
---|
351 | ast::FunctionDecl * FuncGenerator::genProto( std::string&& name, |
---|
352 | std::vector<ast::ptr<ast::DeclWithType>>&& params, |
---|
353 | std::vector<ast::ptr<ast::DeclWithType>>&& returns ) const { |
---|
354 | |
---|
355 | // Handle generic prameters and assertions, if any. |
---|
356 | auto const & old_type_params = getGenericParams( type ); |
---|
357 | ast::DeclReplacer::TypeMap oldToNew; |
---|
358 | std::vector<ast::ptr<ast::TypeDecl>> type_params; |
---|
359 | std::vector<ast::ptr<ast::DeclWithType>> assertions; |
---|
360 | for ( auto & old_param : old_type_params ) { |
---|
361 | ast::TypeDecl * decl = ast::deepCopy( old_param ); |
---|
362 | decl->init = nullptr; |
---|
363 | splice( assertions, decl->assertions ); |
---|
364 | oldToNew.emplace( old_param, decl ); |
---|
365 | type_params.push_back( decl ); |
---|
366 | } |
---|
367 | replaceAll( params, oldToNew ); |
---|
368 | replaceAll( returns, oldToNew ); |
---|
369 | replaceAll( assertions, oldToNew ); |
---|
370 | |
---|
371 | ast::FunctionDecl * decl = new ast::FunctionDecl( |
---|
372 | // Auto-generated routines use the type declaration's location. |
---|
373 | getLocation(), |
---|
374 | std::move( name ), |
---|
375 | std::move( type_params ), |
---|
376 | std::move( assertions ), |
---|
377 | std::move( params ), |
---|
378 | std::move( returns ), |
---|
379 | // Only a prototype, no body. |
---|
380 | nullptr, |
---|
381 | // Use static storage if we are at the top level. |
---|
382 | (0 < functionNesting) ? ast::Storage::Classes() : ast::Storage::Static, |
---|
383 | proto_linkage, |
---|
384 | std::vector<ast::ptr<ast::Attribute>>(), |
---|
385 | // Auto-generated routines are inline to avoid conflicts. |
---|
386 | ast::Function::Specs( ast::Function::Inline ) ); |
---|
387 | decl->fixUniqueId(); |
---|
388 | return decl; |
---|
389 | } |
---|
390 | |
---|
391 | ast::ObjectDecl * FuncGenerator::dstParam() const { |
---|
392 | return new ast::ObjectDecl( getLocation(), "_dst", |
---|
393 | new ast::ReferenceType( ast::deepCopy( type ) ) ); |
---|
394 | } |
---|
395 | |
---|
396 | ast::ObjectDecl * FuncGenerator::srcParam() const { |
---|
397 | return new ast::ObjectDecl( getLocation(), "_src", |
---|
398 | ast::deepCopy( type ) ); |
---|
399 | } |
---|
400 | |
---|
401 | /// Use the current type T to create `void ?{}(T & _dst)`. |
---|
402 | ast::FunctionDecl * FuncGenerator::genCtorProto() const { |
---|
403 | return genProto( "?{}", { dstParam() }, {} ); |
---|
404 | } |
---|
405 | |
---|
406 | /// Use the current type T to create `void ?{}(T & _dst, T _src)`. |
---|
407 | ast::FunctionDecl * FuncGenerator::genCopyProto() const { |
---|
408 | return genProto( "?{}", { dstParam(), srcParam() }, {} ); |
---|
409 | } |
---|
410 | |
---|
411 | /// Use the current type T to create `void ?{}(T & _dst)`. |
---|
412 | ast::FunctionDecl * FuncGenerator::genDtorProto() const { |
---|
413 | // The destructor must be mutex on a concurrent type. |
---|
414 | auto dst = dstParam(); |
---|
415 | if ( isConcurrentType() ) { |
---|
416 | add_qualifiers( dst->type, ast::CV::Qualifiers( ast::CV::Mutex ) ); |
---|
417 | } |
---|
418 | return genProto( "^?{}", { dst }, {} ); |
---|
419 | } |
---|
420 | |
---|
421 | /// Use the current type T to create `T ?{}(T & _dst, T _src)`. |
---|
422 | ast::FunctionDecl * FuncGenerator::genAssignProto() const { |
---|
423 | // Only the name is different, so just reuse the generation function. |
---|
424 | auto retval = srcParam(); |
---|
425 | retval->name = "_ret"; |
---|
426 | return genProto( "?=?", { dstParam(), srcParam() }, { retval } ); |
---|
427 | } |
---|
428 | |
---|
429 | // This one can return null if the last field is an unnamed bitfield. |
---|
430 | ast::FunctionDecl * FuncGenerator::genFieldCtorProto( |
---|
431 | unsigned int fields ) const { |
---|
432 | assert( 0 < fields ); |
---|
433 | auto aggr = strict_dynamic_cast<const ast::AggregateDecl *>( getDecl() ); |
---|
434 | |
---|
435 | std::vector<ast::ptr<ast::DeclWithType>> params = { dstParam() }; |
---|
436 | for ( unsigned int index = 0 ; index < fields ; ++index ) { |
---|
437 | auto member = aggr->members[index].strict_as<ast::DeclWithType>(); |
---|
438 | if ( ast::isUnnamedBitfield( |
---|
439 | dynamic_cast<const ast::ObjectDecl *>( member ) ) ) { |
---|
440 | if ( index == fields - 1 ) { |
---|
441 | return nullptr; |
---|
442 | } |
---|
443 | continue; |
---|
444 | } |
---|
445 | |
---|
446 | auto * paramType = ast::deepCopy( member->get_type() ); |
---|
447 | erase_if( paramType->attributes, []( ast::Attribute const * attr ){ |
---|
448 | return !attr->isValidOnFuncParam(); |
---|
449 | } ); |
---|
450 | ast::ObjectDecl * param = new ast::ObjectDecl( |
---|
451 | getLocation(), member->name, paramType ); |
---|
452 | for ( auto & attr : member->attributes ) { |
---|
453 | if ( attr->isValidOnFuncParam() ) { |
---|
454 | param->attributes.push_back( attr ); |
---|
455 | } |
---|
456 | } |
---|
457 | params.emplace_back( param ); |
---|
458 | } |
---|
459 | return genProto( "?{}", std::move( params ), {} ); |
---|
460 | } |
---|
461 | |
---|
462 | void appendReturnThis( ast::FunctionDecl * decl ) { |
---|
463 | assert( 1 <= decl->params.size() ); |
---|
464 | assert( 1 == decl->returns.size() ); |
---|
465 | assert( decl->stmts ); |
---|
466 | |
---|
467 | const CodeLocation& location = (decl->stmts->kids.empty()) |
---|
468 | ? decl->stmts->location : decl->stmts->kids.back()->location; |
---|
469 | const ast::DeclWithType * thisParam = decl->params.front(); |
---|
470 | decl->stmts.get_and_mutate()->push_back( |
---|
471 | new ast::ReturnStmt( location, |
---|
472 | new ast::VariableExpr( location, thisParam ) |
---|
473 | ) |
---|
474 | ); |
---|
475 | } |
---|
476 | |
---|
477 | void FuncGenerator::genStandardFuncs() { |
---|
478 | // The order here determines the order that these functions are generated. |
---|
479 | // Assignment should come last since it uses copy constructor in return. |
---|
480 | ast::FunctionDecl *(FuncGenerator::*standardProtos[4])() const = { |
---|
481 | &FuncGenerator::genCtorProto, &FuncGenerator::genCopyProto, |
---|
482 | &FuncGenerator::genDtorProto, &FuncGenerator::genAssignProto }; |
---|
483 | for ( auto & generator : standardProtos ) { |
---|
484 | ast::FunctionDecl * decl = (this->*generator)(); |
---|
485 | produceForwardDecl( decl ); |
---|
486 | genFuncBody( decl ); |
---|
487 | if ( CodeGen::isAssignment( decl->name ) ) { |
---|
488 | appendReturnThis( decl ); |
---|
489 | } |
---|
490 | produceDecl( decl ); |
---|
491 | } |
---|
492 | } |
---|
493 | |
---|
494 | void StructFuncGenerator::genFieldCtors() { |
---|
495 | // The field constructors are only generated if the default constructor |
---|
496 | // and copy constructor are both generated, since the need both. |
---|
497 | unsigned numCtors = std::count_if( definitions.begin(), definitions.end(), |
---|
498 | [](const ast::Decl * decl){ return CodeGen::isConstructor( decl->name ); } |
---|
499 | ); |
---|
500 | if ( 2 != numCtors ) return; |
---|
501 | |
---|
502 | for ( unsigned int num = 1 ; num <= decl->members.size() ; ++num ) { |
---|
503 | ast::FunctionDecl * ctor = genFieldCtorProto( num ); |
---|
504 | if ( nullptr == ctor ) { |
---|
505 | continue; |
---|
506 | } |
---|
507 | produceForwardDecl( ctor ); |
---|
508 | makeFieldCtorBody( decl->members.begin(), decl->members.end(), ctor ); |
---|
509 | produceDecl( ctor ); |
---|
510 | } |
---|
511 | } |
---|
512 | |
---|
513 | void StructFuncGenerator::genFuncBody( ast::FunctionDecl * functionDecl ) { |
---|
514 | // Generate appropriate calls to member constructors and assignment. |
---|
515 | // Destructor needs to do everything in reverse, |
---|
516 | // so pass "forward" based on whether the function is a destructor |
---|
517 | if ( CodeGen::isDestructor( functionDecl->name ) ) { |
---|
518 | makeFunctionBody( decl->members.rbegin(), decl->members.rend(), |
---|
519 | functionDecl, SymTab::LoopBackward ); |
---|
520 | } else { |
---|
521 | makeFunctionBody( decl->members.begin(), decl->members.end(), |
---|
522 | functionDecl, SymTab::LoopForward ); |
---|
523 | } |
---|
524 | } |
---|
525 | |
---|
526 | ast::ptr<ast::Stmt> StructFuncGenerator::makeMemberOp( |
---|
527 | const CodeLocation& location, const ast::ObjectDecl * dstParam, |
---|
528 | const ast::Expr * src, const ast::ObjectDecl * field, |
---|
529 | ast::FunctionDecl * func, SymTab::LoopDirection direction ) { |
---|
530 | InitTweak::InitExpander srcParam( src ); |
---|
531 | // Assign to destination. |
---|
532 | ast::MemberExpr * dstSelect = new ast::MemberExpr( |
---|
533 | location, |
---|
534 | field, |
---|
535 | new ast::CastExpr( |
---|
536 | location, |
---|
537 | new ast::VariableExpr( location, dstParam ), |
---|
538 | dstParam->type.strict_as<ast::ReferenceType>()->base |
---|
539 | ) |
---|
540 | ); |
---|
541 | auto stmt = genImplicitCall( |
---|
542 | srcParam, dstSelect, location, func->name, |
---|
543 | field, direction |
---|
544 | ); |
---|
545 | // This could return the above directly, except the generated code is |
---|
546 | // built using the structure's members and that means all the scoped |
---|
547 | // names (the forall parameters) are incorrect. This corrects them. |
---|
548 | if ( stmt && !decl->params.empty() ) { |
---|
549 | ast::DeclReplacer::TypeMap oldToNew; |
---|
550 | for ( auto pair : group_iterate( decl->params, func->type_params ) ) { |
---|
551 | oldToNew.emplace( std::get<0>(pair), std::get<1>(pair) ); |
---|
552 | } |
---|
553 | auto node = ast::DeclReplacer::replace( stmt, oldToNew ); |
---|
554 | stmt = strict_dynamic_cast<const ast::Stmt *>( node ); |
---|
555 | } |
---|
556 | return stmt; |
---|
557 | } |
---|
558 | |
---|
559 | template<typename Iterator> |
---|
560 | void StructFuncGenerator::makeFunctionBody( Iterator current, Iterator end, |
---|
561 | ast::FunctionDecl * func, SymTab::LoopDirection direction ) { |
---|
562 | // Trying to get the best code location. Should probably use a helper or |
---|
563 | // just figure out what that would be given where this is called. |
---|
564 | assert( nullptr == func->stmts ); |
---|
565 | const CodeLocation& location = func->location; |
---|
566 | |
---|
567 | ast::CompoundStmt * stmts = new ast::CompoundStmt( location ); |
---|
568 | |
---|
569 | for ( ; current != end ; ++current ) { |
---|
570 | const ast::ptr<ast::Decl> & member = *current; |
---|
571 | auto field = member.as<ast::ObjectDecl>(); |
---|
572 | if ( nullptr == field ) { |
---|
573 | continue; |
---|
574 | } |
---|
575 | |
---|
576 | // Don't assign to constant members (but do construct/destruct them). |
---|
577 | if ( CodeGen::isAssignment( func->name ) ) { |
---|
578 | // For array types we need to strip off the array layers. |
---|
579 | const ast::Type * type = field->get_type(); |
---|
580 | while ( auto at = dynamic_cast<const ast::ArrayType *>( type ) ) { |
---|
581 | type = at->base; |
---|
582 | } |
---|
583 | if ( type->is_const() ) { |
---|
584 | continue; |
---|
585 | } |
---|
586 | } |
---|
587 | |
---|
588 | assert( !func->params.empty() ); |
---|
589 | const ast::ObjectDecl * dstParam = |
---|
590 | func->params.front().strict_as<ast::ObjectDecl>(); |
---|
591 | const ast::ObjectDecl * srcParam = nullptr; |
---|
592 | if ( 2 == func->params.size() ) { |
---|
593 | srcParam = func->params.back().strict_as<ast::ObjectDecl>(); |
---|
594 | } |
---|
595 | |
---|
596 | ast::MemberExpr * srcSelect = (srcParam) ? new ast::MemberExpr( |
---|
597 | location, field, new ast::VariableExpr( location, srcParam ) |
---|
598 | ) : nullptr; |
---|
599 | ast::ptr<ast::Stmt> stmt = |
---|
600 | makeMemberOp( location, dstParam, srcSelect, field, func, direction ); |
---|
601 | |
---|
602 | if ( nullptr != stmt ) { |
---|
603 | stmts->kids.push_back( stmt ); |
---|
604 | } |
---|
605 | } |
---|
606 | |
---|
607 | func->stmts = stmts; |
---|
608 | } |
---|
609 | |
---|
610 | template<typename Iterator> |
---|
611 | void StructFuncGenerator::makeFieldCtorBody( Iterator current, Iterator end, |
---|
612 | ast::FunctionDecl * func ) { |
---|
613 | const CodeLocation& location = func->location; |
---|
614 | auto & params = func->params; |
---|
615 | // Need at least the constructed parameter and one field parameter. |
---|
616 | assert( 2 <= params.size() ); |
---|
617 | |
---|
618 | ast::CompoundStmt * stmts = new ast::CompoundStmt( location ); |
---|
619 | |
---|
620 | auto dstParam = params.front().strict_as<ast::ObjectDecl>(); |
---|
621 | // Skip over the 'this' parameter. |
---|
622 | for ( auto param = params.begin() + 1 ; current != end ; ++current ) { |
---|
623 | const ast::ptr<ast::Decl> & member = *current; |
---|
624 | ast::ptr<ast::Stmt> stmt = nullptr; |
---|
625 | auto field = member.as<ast::ObjectDecl>(); |
---|
626 | // Not sure why it could be null. |
---|
627 | // Don't make a function for a parameter that is an unnamed bitfield. |
---|
628 | if ( nullptr == field || ast::isUnnamedBitfield( field ) ) { |
---|
629 | continue; |
---|
630 | // Matching Parameter: Initialize the field by copy. |
---|
631 | } else if ( params.end() != param ) { |
---|
632 | const ast::Expr *srcSelect = new ast::VariableExpr( |
---|
633 | func->location, param->get() ); |
---|
634 | stmt = makeMemberOp( location, dstParam, srcSelect, field, func, SymTab::LoopForward ); |
---|
635 | ++param; |
---|
636 | // No Matching Parameter: Initialize the field by default constructor. |
---|
637 | } else { |
---|
638 | stmt = makeMemberOp( location, dstParam, nullptr, field, func, SymTab::LoopForward ); |
---|
639 | } |
---|
640 | |
---|
641 | if ( nullptr != stmt ) { |
---|
642 | stmts->kids.push_back( stmt ); |
---|
643 | } |
---|
644 | } |
---|
645 | func->stmts = stmts; |
---|
646 | } |
---|
647 | |
---|
648 | void UnionFuncGenerator::genFieldCtors() { |
---|
649 | // Field constructors are only generated if default and copy constructor |
---|
650 | // are generated, since they need access to both |
---|
651 | unsigned numCtors = std::count_if( definitions.begin(), definitions.end(), |
---|
652 | []( const ast::Decl * d ){ return CodeGen::isConstructor( d->name ); } |
---|
653 | ); |
---|
654 | if ( 2 != numCtors ) { |
---|
655 | return; |
---|
656 | } |
---|
657 | |
---|
658 | // Create a constructor which takes the first member type as a |
---|
659 | // parameter. For example for `union A { int x; char y; };` generate |
---|
660 | // a function with signature `void ?{}(A *, int)`. This mimics C's |
---|
661 | // behaviour which initializes the first member of the union. |
---|
662 | |
---|
663 | // Still, there must be some members. |
---|
664 | if ( !decl->members.empty() ) { |
---|
665 | ast::FunctionDecl * ctor = genFieldCtorProto( 1 ); |
---|
666 | if ( nullptr == ctor ) { |
---|
667 | return; |
---|
668 | } |
---|
669 | produceForwardDecl( ctor ); |
---|
670 | auto params = ctor->params; |
---|
671 | auto dstParam = params.front().strict_as<ast::ObjectDecl>(); |
---|
672 | auto srcParam = params.back().strict_as<ast::ObjectDecl>(); |
---|
673 | ctor->stmts = new ast::CompoundStmt( getLocation(), |
---|
674 | { makeAssignOp( getLocation(), dstParam, srcParam ) } |
---|
675 | ); |
---|
676 | produceDecl( ctor ); |
---|
677 | } |
---|
678 | } |
---|
679 | |
---|
680 | void UnionFuncGenerator::genFuncBody( ast::FunctionDecl * functionDecl ) { |
---|
681 | const CodeLocation& location = functionDecl->location; |
---|
682 | auto & params = functionDecl->params; |
---|
683 | if ( InitTweak::isCopyConstructor( functionDecl ) |
---|
684 | || InitTweak::isAssignment( functionDecl ) ) { |
---|
685 | assert( 2 == params.size() ); |
---|
686 | auto dstParam = params.front().strict_as<ast::ObjectDecl>(); |
---|
687 | auto srcParam = params.back().strict_as<ast::ObjectDecl>(); |
---|
688 | functionDecl->stmts = new ast::CompoundStmt( location, |
---|
689 | { makeAssignOp( location, dstParam, srcParam ) } |
---|
690 | ); |
---|
691 | } else { |
---|
692 | assert( 1 == params.size() ); |
---|
693 | // Default constructor and destructor is empty. |
---|
694 | functionDecl->stmts = new ast::CompoundStmt( location ); |
---|
695 | // Add unused attribute to parameter to silence warnings. |
---|
696 | addUnusedAttribute( params.front() ); |
---|
697 | |
---|
698 | // Just an extra step to make the forward and declaration match. |
---|
699 | if ( forwards.empty() ) return; |
---|
700 | ast::FunctionDecl * fwd = strict_dynamic_cast<ast::FunctionDecl *>( |
---|
701 | forwards.back().get_and_mutate() ); |
---|
702 | addUnusedAttribute( fwd->params.front() ); |
---|
703 | } |
---|
704 | } |
---|
705 | |
---|
706 | ast::ExprStmt * UnionFuncGenerator::makeAssignOp( const CodeLocation& location, |
---|
707 | const ast::ObjectDecl * dstParam, const ast::ObjectDecl * srcParam ) { |
---|
708 | return new ast::ExprStmt( location, new ast::UntypedExpr( |
---|
709 | location, |
---|
710 | new ast::NameExpr( location, "__builtin_memcpy" ), |
---|
711 | { |
---|
712 | new ast::AddressExpr( location, |
---|
713 | new ast::VariableExpr( location, dstParam ) ), |
---|
714 | new ast::AddressExpr( location, |
---|
715 | new ast::VariableExpr( location, srcParam ) ), |
---|
716 | new ast::SizeofExpr( location, srcParam->type ), |
---|
717 | } ) ); |
---|
718 | } |
---|
719 | |
---|
720 | void EnumFuncGenerator::genFieldCtors() { |
---|
721 | // Enumerations to not have field constructors. |
---|
722 | } |
---|
723 | |
---|
724 | void EnumFuncGenerator::genFuncBody( ast::FunctionDecl * functionDecl ) { |
---|
725 | const CodeLocation& location = functionDecl->location; |
---|
726 | auto & params = functionDecl->params; |
---|
727 | if ( InitTweak::isCopyConstructor( functionDecl ) |
---|
728 | || InitTweak::isAssignment( functionDecl ) ) { |
---|
729 | assert( 2 == params.size() ); |
---|
730 | auto dstParam = params.front().strict_as<ast::ObjectDecl>(); |
---|
731 | auto srcParam = params.back().strict_as<ast::ObjectDecl>(); |
---|
732 | |
---|
733 | /* This looks like a recursive call, but code-gen will turn it into |
---|
734 | * a C-style assignment. |
---|
735 | * |
---|
736 | * This is still before function pointer type conversion, |
---|
737 | * so this will have to do it manually. |
---|
738 | * |
---|
739 | * It will also reference the parent function declaration, creating |
---|
740 | * a cycle for references. This also means that the ref-counts are |
---|
741 | * now non-zero and the declaration will be deleted if it ever |
---|
742 | * returns to zero. |
---|
743 | */ |
---|
744 | auto callExpr = new ast::ApplicationExpr( location, |
---|
745 | ast::VariableExpr::functionPointer( location, functionDecl ), |
---|
746 | { |
---|
747 | new ast::VariableExpr( location, dstParam ), |
---|
748 | new ast::VariableExpr( location, srcParam ), |
---|
749 | } |
---|
750 | ); |
---|
751 | // auto fname = ast::getFunctionName( callExpr ); |
---|
752 | // if (fname == "posE" ) { |
---|
753 | // std::cerr << "Found posE autogen" << std::endl; |
---|
754 | // } |
---|
755 | functionDecl->stmts = new ast::CompoundStmt( location, |
---|
756 | { new ast::ExprStmt( location, callExpr ) } |
---|
757 | ); |
---|
758 | } else { |
---|
759 | assert( 1 == params.size() ); |
---|
760 | // Default constructor and destructor is empty. |
---|
761 | functionDecl->stmts = new ast::CompoundStmt( location ); |
---|
762 | // Just add unused attribute to parameter to silence warnings. |
---|
763 | addUnusedAttribute( params.front() ); |
---|
764 | |
---|
765 | // Just an extra step to make the forward and declaration match. |
---|
766 | if ( forwards.empty() ) return; |
---|
767 | ast::FunctionDecl * fwd = strict_dynamic_cast<ast::FunctionDecl *>( |
---|
768 | forwards.back().get_and_mutate() ); |
---|
769 | addUnusedAttribute( fwd->params.front() ); |
---|
770 | } |
---|
771 | } |
---|
772 | |
---|
773 | void TypeFuncGenerator::genFieldCtors() { |
---|
774 | // Opaque types do not have field constructors. |
---|
775 | } |
---|
776 | |
---|
777 | void TypeFuncGenerator::genFuncBody( ast::FunctionDecl * functionDecl ) { |
---|
778 | const CodeLocation& location = functionDecl->location; |
---|
779 | auto & params = functionDecl->type->params; |
---|
780 | assertf( 1 == params.size() || 2 == params.size(), |
---|
781 | "Incorrect number of parameters in autogenerated typedecl function: %zd", |
---|
782 | params.size() ); |
---|
783 | auto dstParam = params.front().strict_as<ast::ObjectDecl>(); |
---|
784 | auto srcParam = (2 == params.size()) |
---|
785 | ? params.back().strict_as<ast::ObjectDecl>() : nullptr; |
---|
786 | // Generate appropriate calls to member constructor and assignment. |
---|
787 | ast::UntypedExpr * expr = new ast::UntypedExpr( location, |
---|
788 | new ast::NameExpr( location, functionDecl->name ) |
---|
789 | ); |
---|
790 | expr->args.push_back( new ast::CastExpr( location, |
---|
791 | new ast::VariableExpr( location, dstParam ), |
---|
792 | new ast::ReferenceType( decl->base ) |
---|
793 | ) ); |
---|
794 | if ( srcParam ) { |
---|
795 | expr->args.push_back( new ast::CastExpr( location, |
---|
796 | new ast::VariableExpr( location, srcParam ), |
---|
797 | decl->base |
---|
798 | ) ); |
---|
799 | } |
---|
800 | functionDecl->stmts = new ast::CompoundStmt( location, |
---|
801 | { new ast::ExprStmt( location, expr ) } |
---|
802 | ); |
---|
803 | } |
---|
804 | |
---|
805 | } // namespace |
---|
806 | |
---|
807 | void autogenerateRoutines( ast::TranslationUnit & translationUnit ) { |
---|
808 | ast::Pass<AutogenerateRoutines>::run( translationUnit ); |
---|
809 | } |
---|
810 | |
---|
811 | } // Validate |
---|
812 | |
---|
813 | // Local Variables: // |
---|
814 | // tab-width: 4 // |
---|
815 | // mode: c++ // |
---|
816 | // compile-command: "make install" // |
---|
817 | // End: // |
---|