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 | const ast::Stmt * makeMemberOp( |
---|
136 | const CodeLocation& location, |
---|
137 | const ast::ObjectDecl * dstParam, const ast::Expr * src, |
---|
138 | const ast::ObjectDecl * field, ast::FunctionDecl * func, |
---|
139 | SymTab::LoopDirection direction ); |
---|
140 | |
---|
141 | /// Generates the body of a struct function by iterating the struct members |
---|
142 | /// (via parameters). Generates default constructor, copy constructor, |
---|
143 | /// copy assignment, and destructor bodies. No field constructor bodies. |
---|
144 | template<typename Iterator> |
---|
145 | void makeFunctionBody( Iterator member, Iterator end, |
---|
146 | ast::FunctionDecl * func, SymTab::LoopDirection direction ); |
---|
147 | |
---|
148 | /// Generate the body of a constructor which takes parameters that match |
---|
149 | /// fields. (With arguments for one to all of the fields.) |
---|
150 | template<typename Iterator> |
---|
151 | void makeFieldCtorBody( Iterator member, Iterator end, |
---|
152 | ast::FunctionDecl * func ); |
---|
153 | }; |
---|
154 | |
---|
155 | class UnionFuncGenerator final : public FuncGenerator { |
---|
156 | const ast::UnionDecl * decl; |
---|
157 | public: |
---|
158 | UnionFuncGenerator( const ast::UnionDecl * decl, |
---|
159 | const ast::UnionInstType * type, |
---|
160 | unsigned int functionNesting ) : |
---|
161 | FuncGenerator( type, functionNesting ), decl( decl ) |
---|
162 | {} |
---|
163 | |
---|
164 | // Built-ins do not use autogeneration. |
---|
165 | bool shouldAutogen() const final { return !decl->linkage.is_builtin; } |
---|
166 | private: |
---|
167 | void genFuncBody( ast::FunctionDecl * decl ) final; |
---|
168 | void genFieldCtors() final; |
---|
169 | const ast::Decl * getDecl() const final { return decl; } |
---|
170 | |
---|
171 | /// Generate a single union assignment expression (using memcpy). |
---|
172 | ast::ExprStmt * makeAssignOp( const CodeLocation& location, |
---|
173 | const ast::ObjectDecl * dstParam, const ast::ObjectDecl * srcParam ); |
---|
174 | }; |
---|
175 | |
---|
176 | class EnumFuncGenerator final : public FuncGenerator { |
---|
177 | const ast::EnumDecl * decl; |
---|
178 | public: |
---|
179 | EnumFuncGenerator( const ast::EnumDecl * decl, |
---|
180 | const ast::EnumInstType * type, |
---|
181 | unsigned int functionNesting ) : |
---|
182 | FuncGenerator( type, functionNesting ), decl( decl ) |
---|
183 | { |
---|
184 | // TODO: These functions are somewhere between instrinsic and autogen, |
---|
185 | // could possibly use a new linkage type. For now we just make the |
---|
186 | // basic ones intrinsic to code-gen them as C assignments. |
---|
187 | // const auto & real_type = decl->base; |
---|
188 | // const auto & basic = real_type.as<ast::BasicType>(); |
---|
189 | |
---|
190 | // if(!real_type || (basic && basic->isInteger())) proto_linkage = ast::Linkage::Intrinsic; |
---|
191 | |
---|
192 | // Turns other enumeration type into Intrinstic as well to temporarily fix the recursive |
---|
193 | // construction bug |
---|
194 | proto_linkage = ast::Linkage::Intrinsic; |
---|
195 | } |
---|
196 | |
---|
197 | bool shouldAutogen() const final { return true; } |
---|
198 | void genAttrFuncForward(); |
---|
199 | private: |
---|
200 | void genFuncBody( ast::FunctionDecl * decl ) final; |
---|
201 | void genFieldCtors() final; |
---|
202 | const ast::Decl * getDecl() const final { return decl; } |
---|
203 | |
---|
204 | ast::FunctionDecl * genPosProto() const; |
---|
205 | ast::FunctionDecl * genLabelProto() const; |
---|
206 | ast::FunctionDecl * genValueProto() const; |
---|
207 | // ast::FunctionDecl * genValueProto2() const; |
---|
208 | }; |
---|
209 | |
---|
210 | class TypeFuncGenerator final : public FuncGenerator { |
---|
211 | const ast::TypeDecl * decl; |
---|
212 | public: |
---|
213 | TypeFuncGenerator( const ast::TypeDecl * decl, |
---|
214 | ast::TypeInstType * type, |
---|
215 | unsigned int functionNesting ) : |
---|
216 | FuncGenerator( type, functionNesting ), decl( decl ) |
---|
217 | {} |
---|
218 | |
---|
219 | bool shouldAutogen() const final { return true; } |
---|
220 | void genFieldCtors() final; |
---|
221 | private: |
---|
222 | void genFuncBody( ast::FunctionDecl * decl ) final; |
---|
223 | const ast::Decl * getDecl() const final { return decl; } |
---|
224 | }; |
---|
225 | |
---|
226 | // -------------------------------------------------------------------------- |
---|
227 | const std::vector<ast::ptr<ast::TypeDecl>>& getGenericParams( const ast::Type * t ) { |
---|
228 | if ( auto inst = dynamic_cast< const ast::StructInstType * >( t ) ) { |
---|
229 | return inst->base->params; |
---|
230 | } else if ( auto inst = dynamic_cast< const ast::UnionInstType * >( t ) ) { |
---|
231 | return inst->base->params; |
---|
232 | } |
---|
233 | static std::vector<ast::ptr<ast::TypeDecl>> const empty; |
---|
234 | return empty; |
---|
235 | } |
---|
236 | |
---|
237 | /// Changes the node inside a pointer so that it has the unused attribute. |
---|
238 | void addUnusedAttribute( ast::ptr<ast::DeclWithType> & declPtr ) { |
---|
239 | ast::DeclWithType * decl = declPtr.get_and_mutate(); |
---|
240 | decl->attributes.push_back( new ast::Attribute( "unused" ) ); |
---|
241 | } |
---|
242 | |
---|
243 | // -------------------------------------------------------------------------- |
---|
244 | void AutogenerateRoutines::previsit( const ast::EnumDecl * enumDecl ) { |
---|
245 | if ( !enumDecl->body ) return; |
---|
246 | |
---|
247 | ast::EnumInstType enumInst( enumDecl->name ); |
---|
248 | enumInst.base = enumDecl; |
---|
249 | EnumFuncGenerator gen( enumDecl, &enumInst, functionNesting ); |
---|
250 | if ( enumDecl->base ) { |
---|
251 | gen.genAttrFuncForward(); |
---|
252 | } |
---|
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 | const 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 | const ast::Stmt * 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 | const ast::Stmt * stmt = |
---|
600 | makeMemberOp( location, dstParam, srcSelect, field, func, direction ); |
---|
601 | |
---|
602 | if ( nullptr != stmt ) { |
---|
603 | stmts->kids.emplace_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 | const 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.emplace_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 | |
---|
752 | functionDecl->stmts = new ast::CompoundStmt( location, |
---|
753 | { new ast::ExprStmt( location, callExpr ) } |
---|
754 | ); |
---|
755 | } else { |
---|
756 | assert( 1 == params.size() ); |
---|
757 | // Default constructor and destructor is empty. |
---|
758 | functionDecl->stmts = new ast::CompoundStmt( location ); |
---|
759 | // Just add unused attribute to parameter to silence warnings. |
---|
760 | addUnusedAttribute( params.front() ); |
---|
761 | |
---|
762 | // Just an extra step to make the forward and declaration match. |
---|
763 | if ( forwards.empty() ) return; |
---|
764 | ast::FunctionDecl * fwd = strict_dynamic_cast<ast::FunctionDecl *>( |
---|
765 | forwards.back().get_and_mutate() ); |
---|
766 | addUnusedAttribute( fwd->params.front() ); |
---|
767 | } |
---|
768 | } |
---|
769 | |
---|
770 | ast::FunctionDecl * EnumFuncGenerator::genPosProto() const { |
---|
771 | return genProto( "posE", |
---|
772 | { new ast::ObjectDecl( getLocation(), "_i", |
---|
773 | new ast::EnumInstType( decl ) )}, |
---|
774 | { new ast::ObjectDecl( getLocation(), "_ret", |
---|
775 | new ast::BasicType{ ast::BasicType::UnsignedInt } )} ); |
---|
776 | } |
---|
777 | |
---|
778 | ast::FunctionDecl * EnumFuncGenerator::genLabelProto() const { |
---|
779 | return genProto( "labelE", |
---|
780 | { new ast::ObjectDecl( getLocation(), "_i", |
---|
781 | new ast::EnumInstType( decl ) ) }, |
---|
782 | { new ast::ObjectDecl( getLocation(), "_ret", |
---|
783 | new ast::PointerType( new ast::BasicType{ ast::BasicType::Char } ) ) } ); |
---|
784 | } |
---|
785 | |
---|
786 | ast::FunctionDecl * EnumFuncGenerator::genValueProto() const { |
---|
787 | return genProto( "valueE", |
---|
788 | { new ast::ObjectDecl( getLocation(), "_i", new ast::EnumInstType( decl ) )}, |
---|
789 | { new ast::ObjectDecl( getLocation(), "_ret", ast::deepCopy( decl->base ) ) } ); |
---|
790 | } |
---|
791 | |
---|
792 | // ast::FunctionDecl * EnumFuncGenerator::genValueProto2() const { |
---|
793 | // return genProto( "valueE", |
---|
794 | // { new ast::ObjectDecl( getLocation(), "_i", new ast::EnumPosType( new ast::EnumInstType( decl ) ) )}, |
---|
795 | // { new ast::ObjectDecl( getLocation(), "_ret", ast::deepCopy( decl->base ) ) } ); |
---|
796 | // } |
---|
797 | |
---|
798 | void EnumFuncGenerator::genAttrFuncForward() { |
---|
799 | if ( decl->base ) { |
---|
800 | ast::FunctionDecl *(EnumFuncGenerator::*attrProtos[3])() const = { |
---|
801 | &EnumFuncGenerator::genPosProto, &EnumFuncGenerator::genLabelProto, |
---|
802 | &EnumFuncGenerator::genValueProto |
---|
803 | // , &EnumFuncGenerator::genValueProto2 |
---|
804 | }; |
---|
805 | for ( auto & generator : attrProtos ) { |
---|
806 | produceForwardDecl( (this->*generator)() ); |
---|
807 | } |
---|
808 | } |
---|
809 | } |
---|
810 | |
---|
811 | void TypeFuncGenerator::genFieldCtors() { |
---|
812 | // Opaque types do not have field constructors. |
---|
813 | } |
---|
814 | |
---|
815 | void TypeFuncGenerator::genFuncBody( ast::FunctionDecl * functionDecl ) { |
---|
816 | const CodeLocation& location = functionDecl->location; |
---|
817 | auto & params = functionDecl->type->params; |
---|
818 | assertf( 1 == params.size() || 2 == params.size(), |
---|
819 | "Incorrect number of parameters in autogenerated typedecl function: %zd", |
---|
820 | params.size() ); |
---|
821 | auto dstParam = params.front().strict_as<ast::ObjectDecl>(); |
---|
822 | auto srcParam = (2 == params.size()) |
---|
823 | ? params.back().strict_as<ast::ObjectDecl>() : nullptr; |
---|
824 | // Generate appropriate calls to member constructor and assignment. |
---|
825 | ast::UntypedExpr * expr = new ast::UntypedExpr( location, |
---|
826 | new ast::NameExpr( location, functionDecl->name ) |
---|
827 | ); |
---|
828 | expr->args.push_back( new ast::CastExpr( location, |
---|
829 | new ast::VariableExpr( location, dstParam ), |
---|
830 | new ast::ReferenceType( decl->base ) |
---|
831 | ) ); |
---|
832 | if ( srcParam ) { |
---|
833 | expr->args.push_back( new ast::CastExpr( location, |
---|
834 | new ast::VariableExpr( location, srcParam ), |
---|
835 | decl->base |
---|
836 | ) ); |
---|
837 | } |
---|
838 | functionDecl->stmts = new ast::CompoundStmt( location, |
---|
839 | { new ast::ExprStmt( location, expr ) } |
---|
840 | ); |
---|
841 | } |
---|
842 | |
---|
843 | } // namespace |
---|
844 | |
---|
845 | void autogenerateRoutines( ast::TranslationUnit & translationUnit ) { |
---|
846 | ast::Pass<AutogenerateRoutines>::run( translationUnit ); |
---|
847 | } |
---|
848 | |
---|
849 | } // Validate |
---|
850 | |
---|
851 | // Local Variables: // |
---|
852 | // tab-width: 4 // |
---|
853 | // mode: c++ // |
---|
854 | // compile-command: "make install" // |
---|
855 | // End: // |
---|