[a488783] | 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 |
---|
[20737104] | 12 | // Last Modified On : Tue Sep 20 16:00:00 2022 |
---|
| 13 | // Update Count : 2 |
---|
[a488783] | 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" |
---|
[bccd70a] | 27 | #include "AST/Copy.hpp" |
---|
[20737104] | 28 | #include "AST/Create.hpp" |
---|
[a488783] | 29 | #include "AST/Decl.hpp" |
---|
| 30 | #include "AST/DeclReplacer.hpp" |
---|
| 31 | #include "AST/Expr.hpp" |
---|
[91715ce1] | 32 | #include "AST/Inspect.hpp" |
---|
[a488783] | 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 |
---|
[00a8e19] | 41 | #include "InitTweak/InitTweak.h" // for isAssignment, isCopyConstructor |
---|
[fb4dc28] | 42 | #include "SymTab/GenImplicitCall.hpp" // for genImplicitCall |
---|
[a488783] | 43 | #include "SymTab/Mangler.h" // for Mangler |
---|
| 44 | #include "CompilationState.h" |
---|
| 45 | |
---|
| 46 | namespace Validate { |
---|
| 47 | |
---|
| 48 | namespace { |
---|
| 49 | |
---|
| 50 | // -------------------------------------------------------------------------- |
---|
| 51 | struct AutogenerateRoutines_new 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; } |
---|
[8913de4] | 95 | ast::FunctionDecl * genProto( std::string&& name, |
---|
[a488783] | 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. |
---|
[91715ce1] | 124 | bool shouldAutogen() const final { return !decl->linkage.is_builtin && !structHasFlexibleArray(decl); } |
---|
[a488783] | 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, |
---|
[3322180] | 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 | if(!real_type || (basic && basic->isInteger())) proto_linkage = ast::Linkage::Intrinsic; |
---|
[a488783] | 191 | } |
---|
| 192 | |
---|
| 193 | bool shouldAutogen() const final { return true; } |
---|
| 194 | private: |
---|
| 195 | void genFuncBody( ast::FunctionDecl * decl ) final; |
---|
| 196 | void genFieldCtors() final; |
---|
| 197 | const ast::Decl * getDecl() const final { return decl; } |
---|
| 198 | }; |
---|
| 199 | |
---|
| 200 | class TypeFuncGenerator final : public FuncGenerator { |
---|
| 201 | const ast::TypeDecl * decl; |
---|
| 202 | public: |
---|
| 203 | TypeFuncGenerator( const ast::TypeDecl * decl, |
---|
| 204 | ast::TypeInstType * type, |
---|
| 205 | unsigned int functionNesting ) : |
---|
| 206 | FuncGenerator( type, functionNesting ), decl( decl ) |
---|
| 207 | {} |
---|
| 208 | |
---|
| 209 | bool shouldAutogen() const final { return true; } |
---|
| 210 | void genFieldCtors() final; |
---|
| 211 | private: |
---|
| 212 | void genFuncBody( ast::FunctionDecl * decl ) final; |
---|
| 213 | const ast::Decl * getDecl() const final { return decl; } |
---|
| 214 | }; |
---|
| 215 | |
---|
| 216 | // -------------------------------------------------------------------------- |
---|
| 217 | const std::vector<ast::ptr<ast::TypeDecl>>& getGenericParams( const ast::Type * t ) { |
---|
| 218 | if ( auto inst = dynamic_cast< const ast::StructInstType * >( t ) ) { |
---|
| 219 | return inst->base->params; |
---|
| 220 | } else if ( auto inst = dynamic_cast< const ast::UnionInstType * >( t ) ) { |
---|
| 221 | return inst->base->params; |
---|
| 222 | } |
---|
| 223 | static std::vector<ast::ptr<ast::TypeDecl>> const empty; |
---|
| 224 | return empty; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | /// Changes the node inside a pointer so that it has the unused attribute. |
---|
| 228 | void addUnusedAttribute( ast::ptr<ast::DeclWithType> & declPtr ) { |
---|
| 229 | ast::DeclWithType * decl = declPtr.get_and_mutate(); |
---|
| 230 | decl->attributes.push_back( new ast::Attribute( "unused" ) ); |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | // -------------------------------------------------------------------------- |
---|
| 234 | void AutogenerateRoutines_new::previsit( const ast::EnumDecl * enumDecl ) { |
---|
| 235 | // Must visit children (enum constants) to add them to the symbol table. |
---|
| 236 | if ( !enumDecl->body ) return; |
---|
| 237 | |
---|
[d958834b] | 238 | // if ( auto enumBaseType = enumDecl->base ) { |
---|
| 239 | // if ( auto enumBaseTypeAsStructInst = dynamic_cast<const ast::StructInstType *>(enumBaseType.get()) ) { |
---|
| 240 | // const ast::StructDecl * structDecl = enumBaseTypeAsStructInst->base.get(); |
---|
| 241 | // this->previsit( structDecl ); |
---|
| 242 | // } |
---|
| 243 | // } |
---|
| 244 | |
---|
[a488783] | 245 | ast::EnumInstType enumInst( enumDecl->name ); |
---|
| 246 | enumInst.base = enumDecl; |
---|
| 247 | EnumFuncGenerator gen( enumDecl, &enumInst, functionNesting ); |
---|
| 248 | gen.generateAndAppendFunctions( declsToAddAfter ); |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | void AutogenerateRoutines_new::previsit( const ast::StructDecl * structDecl ) { |
---|
| 252 | visit_children = false; |
---|
| 253 | if ( !structDecl->body ) return; |
---|
| 254 | |
---|
| 255 | ast::StructInstType structInst( structDecl->name ); |
---|
| 256 | structInst.base = structDecl; |
---|
| 257 | for ( const ast::TypeDecl * typeDecl : structDecl->params ) { |
---|
| 258 | structInst.params.push_back( new ast::TypeExpr( |
---|
| 259 | typeDecl->location, |
---|
[b230091] | 260 | new ast::TypeInstType( typeDecl ) |
---|
[a488783] | 261 | ) ); |
---|
| 262 | } |
---|
| 263 | StructFuncGenerator gen( structDecl, &structInst, functionNesting ); |
---|
| 264 | gen.generateAndAppendFunctions( declsToAddAfter ); |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | void AutogenerateRoutines_new::previsit( const ast::UnionDecl * unionDecl ) { |
---|
| 268 | visit_children = false; |
---|
| 269 | if ( !unionDecl->body ) return; |
---|
| 270 | |
---|
| 271 | ast::UnionInstType unionInst( unionDecl->name ); |
---|
| 272 | unionInst.base = unionDecl; |
---|
| 273 | for ( const ast::TypeDecl * typeDecl : unionDecl->params ) { |
---|
| 274 | unionInst.params.push_back( new ast::TypeExpr( |
---|
| 275 | unionDecl->location, |
---|
[b230091] | 276 | new ast::TypeInstType( typeDecl ) |
---|
[a488783] | 277 | ) ); |
---|
| 278 | } |
---|
| 279 | UnionFuncGenerator gen( unionDecl, &unionInst, functionNesting ); |
---|
| 280 | gen.generateAndAppendFunctions( declsToAddAfter ); |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | /// Generate ctor/dtors/assign for typedecls, e.g., otype T = int *; |
---|
| 284 | void AutogenerateRoutines_new::previsit( const ast::TypeDecl * typeDecl ) { |
---|
| 285 | if ( !typeDecl->base ) return; |
---|
| 286 | |
---|
| 287 | ast::TypeInstType refType( typeDecl->name, typeDecl ); |
---|
| 288 | TypeFuncGenerator gen( typeDecl, &refType, functionNesting ); |
---|
| 289 | gen.generateAndAppendFunctions( declsToAddAfter ); |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | void AutogenerateRoutines_new::previsit( const ast::TraitDecl * ) { |
---|
| 293 | // Ensure that we don't add assignment ops for types defined as part of the trait |
---|
| 294 | visit_children = false; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | void AutogenerateRoutines_new::previsit( const ast::FunctionDecl * ) { |
---|
| 298 | // Track whether we're currently in a function. |
---|
| 299 | // Can ignore function type idiosyncrasies, because function type can never |
---|
| 300 | // declare a new type. |
---|
| 301 | functionNesting += 1; |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | void AutogenerateRoutines_new::postvisit( const ast::FunctionDecl * ) { |
---|
| 305 | functionNesting -= 1; |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | void FuncGenerator::generateAndAppendFunctions( |
---|
| 309 | std::list<ast::ptr<ast::Decl>> & decls ) { |
---|
| 310 | if ( !shouldAutogen() ) return; |
---|
| 311 | |
---|
| 312 | // Generate the functions (they go into forwards and definitions). |
---|
| 313 | genStandardFuncs(); |
---|
| 314 | genFieldCtors(); |
---|
| 315 | |
---|
| 316 | // Now export the lists contents. |
---|
| 317 | decls.splice( decls.end(), forwards ); |
---|
| 318 | decls.splice( decls.end(), definitions ); |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | void FuncGenerator::produceDecl( const ast::FunctionDecl * decl ) { |
---|
| 322 | assert( nullptr != decl->stmts ); |
---|
[b1e21da] | 323 | assert( decl->type_params.size() == getGenericParams( type ).size() ); |
---|
[a488783] | 324 | |
---|
| 325 | definitions.push_back( decl ); |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | /// Make a forward declaration of the decl and add it to forwards. |
---|
| 329 | void FuncGenerator::produceForwardDecl( const ast::FunctionDecl * decl ) { |
---|
| 330 | if (0 != functionNesting) return; |
---|
[20737104] | 331 | ast::FunctionDecl * fwd = |
---|
| 332 | ( decl->stmts ) ? ast::asForward( decl ) : ast::deepCopy( decl ) ; |
---|
[a488783] | 333 | fwd->fixUniqueId(); |
---|
| 334 | forwards.push_back( fwd ); |
---|
| 335 | } |
---|
| 336 | |
---|
[8913de4] | 337 | void replaceAll( std::vector<ast::ptr<ast::DeclWithType>> & dwts, |
---|
| 338 | const ast::DeclReplacer::TypeMap & map ) { |
---|
| 339 | for ( auto & dwt : dwts ) { |
---|
| 340 | dwt = strict_dynamic_cast<const ast::DeclWithType *>( |
---|
| 341 | ast::DeclReplacer::replace( dwt, map ) ); |
---|
| 342 | } |
---|
| 343 | } |
---|
| 344 | |
---|
[a488783] | 345 | /// Generates a basic prototype function declaration. |
---|
[8913de4] | 346 | ast::FunctionDecl * FuncGenerator::genProto( std::string&& name, |
---|
[a488783] | 347 | std::vector<ast::ptr<ast::DeclWithType>>&& params, |
---|
| 348 | std::vector<ast::ptr<ast::DeclWithType>>&& returns ) const { |
---|
| 349 | |
---|
| 350 | // Handle generic prameters and assertions, if any. |
---|
| 351 | auto const & old_type_params = getGenericParams( type ); |
---|
[8913de4] | 352 | ast::DeclReplacer::TypeMap oldToNew; |
---|
[a488783] | 353 | std::vector<ast::ptr<ast::TypeDecl>> type_params; |
---|
| 354 | std::vector<ast::ptr<ast::DeclWithType>> assertions; |
---|
| 355 | for ( auto & old_param : old_type_params ) { |
---|
| 356 | ast::TypeDecl * decl = ast::deepCopy( old_param ); |
---|
[8913de4] | 357 | decl->init = nullptr; |
---|
| 358 | splice( assertions, decl->assertions ); |
---|
[b1e21da] | 359 | oldToNew.emplace( old_param, decl ); |
---|
[a488783] | 360 | type_params.push_back( decl ); |
---|
| 361 | } |
---|
[8913de4] | 362 | replaceAll( params, oldToNew ); |
---|
| 363 | replaceAll( returns, oldToNew ); |
---|
| 364 | replaceAll( assertions, oldToNew ); |
---|
[a488783] | 365 | |
---|
| 366 | ast::FunctionDecl * decl = new ast::FunctionDecl( |
---|
| 367 | // Auto-generated routines use the type declaration's location. |
---|
| 368 | getLocation(), |
---|
[8913de4] | 369 | std::move( name ), |
---|
[a488783] | 370 | std::move( type_params ), |
---|
[7edd5c1] | 371 | std::move( assertions ), |
---|
[a488783] | 372 | std::move( params ), |
---|
| 373 | std::move( returns ), |
---|
| 374 | // Only a prototype, no body. |
---|
| 375 | nullptr, |
---|
| 376 | // Use static storage if we are at the top level. |
---|
| 377 | (0 < functionNesting) ? ast::Storage::Classes() : ast::Storage::Static, |
---|
| 378 | proto_linkage, |
---|
| 379 | std::vector<ast::ptr<ast::Attribute>>(), |
---|
| 380 | // Auto-generated routines are inline to avoid conflicts. |
---|
| 381 | ast::Function::Specs( ast::Function::Inline ) ); |
---|
| 382 | decl->fixUniqueId(); |
---|
| 383 | return decl; |
---|
| 384 | } |
---|
| 385 | |
---|
| 386 | ast::ObjectDecl * FuncGenerator::dstParam() const { |
---|
| 387 | return new ast::ObjectDecl( getLocation(), "_dst", |
---|
[e8616b6] | 388 | new ast::ReferenceType( ast::deepCopy( type ) ) ); |
---|
[a488783] | 389 | } |
---|
| 390 | |
---|
| 391 | ast::ObjectDecl * FuncGenerator::srcParam() const { |
---|
| 392 | return new ast::ObjectDecl( getLocation(), "_src", |
---|
[e8616b6] | 393 | ast::deepCopy( type ) ); |
---|
[a488783] | 394 | } |
---|
| 395 | |
---|
| 396 | /// Use the current type T to create `void ?{}(T & _dst)`. |
---|
| 397 | ast::FunctionDecl * FuncGenerator::genCtorProto() const { |
---|
| 398 | return genProto( "?{}", { dstParam() }, {} ); |
---|
| 399 | } |
---|
| 400 | |
---|
| 401 | /// Use the current type T to create `void ?{}(T & _dst, T _src)`. |
---|
| 402 | ast::FunctionDecl * FuncGenerator::genCopyProto() const { |
---|
| 403 | return genProto( "?{}", { dstParam(), srcParam() }, {} ); |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | /// Use the current type T to create `void ?{}(T & _dst)`. |
---|
| 407 | ast::FunctionDecl * FuncGenerator::genDtorProto() const { |
---|
| 408 | // The destructor must be mutex on a concurrent type. |
---|
| 409 | auto dst = dstParam(); |
---|
| 410 | if ( isConcurrentType() ) { |
---|
| 411 | add_qualifiers( dst->type, ast::CV::Qualifiers( ast::CV::Mutex ) ); |
---|
| 412 | } |
---|
| 413 | return genProto( "^?{}", { dst }, {} ); |
---|
| 414 | } |
---|
| 415 | |
---|
| 416 | /// Use the current type T to create `T ?{}(T & _dst, T _src)`. |
---|
| 417 | ast::FunctionDecl * FuncGenerator::genAssignProto() const { |
---|
| 418 | // Only the name is different, so just reuse the generation function. |
---|
| 419 | auto retval = srcParam(); |
---|
| 420 | retval->name = "_ret"; |
---|
| 421 | return genProto( "?=?", { dstParam(), srcParam() }, { retval } ); |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | // This one can return null if the last field is an unnamed bitfield. |
---|
| 425 | ast::FunctionDecl * FuncGenerator::genFieldCtorProto( |
---|
| 426 | unsigned int fields ) const { |
---|
| 427 | assert( 0 < fields ); |
---|
| 428 | auto aggr = strict_dynamic_cast<const ast::AggregateDecl *>( getDecl() ); |
---|
| 429 | |
---|
| 430 | std::vector<ast::ptr<ast::DeclWithType>> params = { dstParam() }; |
---|
| 431 | for ( unsigned int index = 0 ; index < fields ; ++index ) { |
---|
| 432 | auto member = aggr->members[index].strict_as<ast::DeclWithType>(); |
---|
[fb4dc28] | 433 | if ( ast::isUnnamedBitfield( |
---|
[a488783] | 434 | dynamic_cast<const ast::ObjectDecl *>( member ) ) ) { |
---|
| 435 | if ( index == fields - 1 ) { |
---|
| 436 | return nullptr; |
---|
| 437 | } |
---|
| 438 | continue; |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | auto * paramType = ast::deepCopy( member->get_type() ); |
---|
| 442 | paramType->attributes.clear(); |
---|
| 443 | ast::ObjectDecl * param = new ast::ObjectDecl( |
---|
| 444 | getLocation(), member->name, paramType ); |
---|
| 445 | for ( auto & attr : member->attributes ) { |
---|
| 446 | if ( attr->isValidOnFuncParam() ) { |
---|
| 447 | param->attributes.push_back( attr ); |
---|
| 448 | } |
---|
| 449 | } |
---|
| 450 | params.emplace_back( param ); |
---|
| 451 | } |
---|
| 452 | return genProto( "?{}", std::move( params ), {} ); |
---|
| 453 | } |
---|
| 454 | |
---|
| 455 | void appendReturnThis( ast::FunctionDecl * decl ) { |
---|
| 456 | assert( 1 <= decl->params.size() ); |
---|
| 457 | assert( 1 == decl->returns.size() ); |
---|
| 458 | assert( decl->stmts ); |
---|
| 459 | |
---|
| 460 | const CodeLocation& location = (decl->stmts->kids.empty()) |
---|
| 461 | ? decl->stmts->location : decl->stmts->kids.back()->location; |
---|
| 462 | const ast::DeclWithType * thisParam = decl->params.front(); |
---|
| 463 | decl->stmts.get_and_mutate()->push_back( |
---|
| 464 | new ast::ReturnStmt( location, |
---|
| 465 | new ast::VariableExpr( location, thisParam ) |
---|
| 466 | ) |
---|
| 467 | ); |
---|
| 468 | } |
---|
| 469 | |
---|
| 470 | void FuncGenerator::genStandardFuncs() { |
---|
| 471 | // The order here determines the order that these functions are generated. |
---|
| 472 | // Assignment should come last since it uses copy constructor in return. |
---|
| 473 | ast::FunctionDecl *(FuncGenerator::*standardProtos[4])() const = { |
---|
| 474 | &FuncGenerator::genCtorProto, &FuncGenerator::genCopyProto, |
---|
| 475 | &FuncGenerator::genDtorProto, &FuncGenerator::genAssignProto }; |
---|
| 476 | for ( auto & generator : standardProtos ) { |
---|
| 477 | ast::FunctionDecl * decl = (this->*generator)(); |
---|
| 478 | produceForwardDecl( decl ); |
---|
| 479 | genFuncBody( decl ); |
---|
| 480 | if ( CodeGen::isAssignment( decl->name ) ) { |
---|
| 481 | appendReturnThis( decl ); |
---|
| 482 | } |
---|
| 483 | produceDecl( decl ); |
---|
| 484 | } |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | void StructFuncGenerator::genFieldCtors() { |
---|
| 488 | // The field constructors are only generated if the default constructor |
---|
| 489 | // and copy constructor are both generated, since the need both. |
---|
| 490 | unsigned numCtors = std::count_if( definitions.begin(), definitions.end(), |
---|
| 491 | [](const ast::Decl * decl){ return CodeGen::isConstructor( decl->name ); } |
---|
| 492 | ); |
---|
| 493 | if ( 2 != numCtors ) return; |
---|
| 494 | |
---|
| 495 | for ( unsigned int num = 1 ; num <= decl->members.size() ; ++num ) { |
---|
| 496 | ast::FunctionDecl * ctor = genFieldCtorProto( num ); |
---|
| 497 | if ( nullptr == ctor ) { |
---|
| 498 | continue; |
---|
| 499 | } |
---|
| 500 | produceForwardDecl( ctor ); |
---|
| 501 | makeFieldCtorBody( decl->members.begin(), decl->members.end(), ctor ); |
---|
| 502 | produceDecl( ctor ); |
---|
| 503 | } |
---|
| 504 | } |
---|
| 505 | |
---|
| 506 | void StructFuncGenerator::genFuncBody( ast::FunctionDecl * functionDecl ) { |
---|
| 507 | // Generate appropriate calls to member constructors and assignment. |
---|
| 508 | // Destructor needs to do everything in reverse, |
---|
| 509 | // so pass "forward" based on whether the function is a destructor |
---|
| 510 | if ( CodeGen::isDestructor( functionDecl->name ) ) { |
---|
| 511 | makeFunctionBody( decl->members.rbegin(), decl->members.rend(), |
---|
| 512 | functionDecl, SymTab::LoopBackward ); |
---|
| 513 | } else { |
---|
| 514 | makeFunctionBody( decl->members.begin(), decl->members.end(), |
---|
| 515 | functionDecl, SymTab::LoopForward ); |
---|
| 516 | } |
---|
| 517 | } |
---|
| 518 | |
---|
| 519 | ast::ptr<ast::Stmt> StructFuncGenerator::makeMemberOp( |
---|
| 520 | const CodeLocation& location, const ast::ObjectDecl * dstParam, |
---|
| 521 | const ast::Expr * src, const ast::ObjectDecl * field, |
---|
| 522 | ast::FunctionDecl * func, SymTab::LoopDirection direction ) { |
---|
| 523 | InitTweak::InitExpander_new srcParam( src ); |
---|
| 524 | // Assign to destination. |
---|
[24d6572] | 525 | ast::MemberExpr * dstSelect = new ast::MemberExpr( |
---|
[a488783] | 526 | location, |
---|
| 527 | field, |
---|
| 528 | new ast::CastExpr( |
---|
| 529 | location, |
---|
| 530 | new ast::VariableExpr( location, dstParam ), |
---|
| 531 | dstParam->type.strict_as<ast::ReferenceType>()->base |
---|
| 532 | ) |
---|
| 533 | ); |
---|
| 534 | return genImplicitCall( |
---|
| 535 | srcParam, dstSelect, location, func->name, |
---|
| 536 | field, direction |
---|
| 537 | ); |
---|
| 538 | } |
---|
| 539 | |
---|
| 540 | template<typename Iterator> |
---|
| 541 | void StructFuncGenerator::makeFunctionBody( Iterator current, Iterator end, |
---|
| 542 | ast::FunctionDecl * func, SymTab::LoopDirection direction ) { |
---|
| 543 | // Trying to get the best code location. Should probably use a helper or |
---|
| 544 | // just figure out what that would be given where this is called. |
---|
| 545 | assert( nullptr == func->stmts ); |
---|
| 546 | const CodeLocation& location = func->location; |
---|
| 547 | |
---|
| 548 | ast::CompoundStmt * stmts = new ast::CompoundStmt( location ); |
---|
| 549 | |
---|
| 550 | for ( ; current != end ; ++current ) { |
---|
| 551 | const ast::ptr<ast::Decl> & member = *current; |
---|
| 552 | auto field = member.as<ast::ObjectDecl>(); |
---|
| 553 | if ( nullptr == field ) { |
---|
| 554 | continue; |
---|
| 555 | } |
---|
| 556 | |
---|
| 557 | // Don't assign to constant members (but do construct/destruct them). |
---|
| 558 | if ( CodeGen::isAssignment( func->name ) ) { |
---|
| 559 | // For array types we need to strip off the array layers. |
---|
| 560 | const ast::Type * type = field->get_type(); |
---|
| 561 | while ( auto at = dynamic_cast<const ast::ArrayType *>( type ) ) { |
---|
| 562 | type = at->base; |
---|
| 563 | } |
---|
| 564 | if ( type->is_const() ) { |
---|
| 565 | continue; |
---|
| 566 | } |
---|
| 567 | } |
---|
| 568 | |
---|
| 569 | assert( !func->params.empty() ); |
---|
| 570 | const ast::ObjectDecl * dstParam = |
---|
| 571 | func->params.front().strict_as<ast::ObjectDecl>(); |
---|
| 572 | const ast::ObjectDecl * srcParam = nullptr; |
---|
| 573 | if ( 2 == func->params.size() ) { |
---|
| 574 | srcParam = func->params.back().strict_as<ast::ObjectDecl>(); |
---|
| 575 | } |
---|
| 576 | |
---|
[24d6572] | 577 | ast::MemberExpr * srcSelect = (srcParam) ? new ast::MemberExpr( |
---|
[a488783] | 578 | location, field, new ast::VariableExpr( location, srcParam ) |
---|
| 579 | ) : nullptr; |
---|
| 580 | ast::ptr<ast::Stmt> stmt = |
---|
| 581 | makeMemberOp( location, dstParam, srcSelect, field, func, direction ); |
---|
| 582 | |
---|
| 583 | if ( nullptr != stmt ) { |
---|
| 584 | stmts->kids.push_back( stmt ); |
---|
| 585 | } |
---|
| 586 | } |
---|
| 587 | |
---|
| 588 | func->stmts = stmts; |
---|
| 589 | } |
---|
| 590 | |
---|
| 591 | template<typename Iterator> |
---|
| 592 | void StructFuncGenerator::makeFieldCtorBody( Iterator current, Iterator end, |
---|
| 593 | ast::FunctionDecl * func ) { |
---|
| 594 | const CodeLocation& location = func->location; |
---|
| 595 | auto & params = func->params; |
---|
| 596 | // Need at least the constructed parameter and one field parameter. |
---|
| 597 | assert( 2 <= params.size() ); |
---|
| 598 | |
---|
| 599 | ast::CompoundStmt * stmts = new ast::CompoundStmt( location ); |
---|
| 600 | |
---|
| 601 | auto dstParam = params.front().strict_as<ast::ObjectDecl>(); |
---|
| 602 | // Skip over the 'this' parameter. |
---|
| 603 | for ( auto param = params.begin() + 1 ; current != end ; ++current ) { |
---|
| 604 | const ast::ptr<ast::Decl> & member = *current; |
---|
| 605 | ast::ptr<ast::Stmt> stmt = nullptr; |
---|
| 606 | auto field = member.as<ast::ObjectDecl>(); |
---|
| 607 | // Not sure why it could be null. |
---|
| 608 | // Don't make a function for a parameter that is an unnamed bitfield. |
---|
[fb4dc28] | 609 | if ( nullptr == field || ast::isUnnamedBitfield( field ) ) { |
---|
[a488783] | 610 | continue; |
---|
| 611 | // Matching Parameter: Initialize the field by copy. |
---|
| 612 | } else if ( params.end() != param ) { |
---|
| 613 | const ast::Expr *srcSelect = new ast::VariableExpr( |
---|
| 614 | func->location, param->get() ); |
---|
| 615 | stmt = makeMemberOp( location, dstParam, srcSelect, field, func, SymTab::LoopForward ); |
---|
| 616 | ++param; |
---|
| 617 | // No Matching Parameter: Initialize the field by default constructor. |
---|
| 618 | } else { |
---|
| 619 | stmt = makeMemberOp( location, dstParam, nullptr, field, func, SymTab::LoopForward ); |
---|
| 620 | } |
---|
| 621 | |
---|
| 622 | if ( nullptr != stmt ) { |
---|
| 623 | stmts->kids.push_back( stmt ); |
---|
| 624 | } |
---|
| 625 | } |
---|
| 626 | func->stmts = stmts; |
---|
| 627 | } |
---|
| 628 | |
---|
| 629 | void UnionFuncGenerator::genFieldCtors() { |
---|
| 630 | // Field constructors are only generated if default and copy constructor |
---|
| 631 | // are generated, since they need access to both |
---|
| 632 | unsigned numCtors = std::count_if( definitions.begin(), definitions.end(), |
---|
| 633 | []( const ast::Decl * d ){ return CodeGen::isConstructor( d->name ); } |
---|
| 634 | ); |
---|
| 635 | if ( 2 != numCtors ) { |
---|
| 636 | return; |
---|
| 637 | } |
---|
| 638 | |
---|
| 639 | // Create a constructor which takes the first member type as a |
---|
| 640 | // parameter. For example for `union A { int x; char y; };` generate |
---|
| 641 | // a function with signature `void ?{}(A *, int)`. This mimics C's |
---|
| 642 | // behaviour which initializes the first member of the union. |
---|
| 643 | |
---|
| 644 | // Still, there must be some members. |
---|
| 645 | if ( !decl->members.empty() ) { |
---|
| 646 | ast::FunctionDecl * ctor = genFieldCtorProto( 1 ); |
---|
| 647 | if ( nullptr == ctor ) { |
---|
| 648 | return; |
---|
| 649 | } |
---|
| 650 | produceForwardDecl( ctor ); |
---|
| 651 | auto params = ctor->params; |
---|
| 652 | auto dstParam = params.front().strict_as<ast::ObjectDecl>(); |
---|
| 653 | auto srcParam = params.back().strict_as<ast::ObjectDecl>(); |
---|
| 654 | ctor->stmts = new ast::CompoundStmt( getLocation(), |
---|
| 655 | { makeAssignOp( getLocation(), dstParam, srcParam ) } |
---|
| 656 | ); |
---|
| 657 | produceDecl( ctor ); |
---|
| 658 | } |
---|
| 659 | } |
---|
| 660 | |
---|
| 661 | void UnionFuncGenerator::genFuncBody( ast::FunctionDecl * functionDecl ) { |
---|
| 662 | const CodeLocation& location = functionDecl->location; |
---|
| 663 | auto & params = functionDecl->params; |
---|
| 664 | if ( InitTweak::isCopyConstructor( functionDecl ) |
---|
| 665 | || InitTweak::isAssignment( functionDecl ) ) { |
---|
| 666 | assert( 2 == params.size() ); |
---|
| 667 | auto dstParam = params.front().strict_as<ast::ObjectDecl>(); |
---|
| 668 | auto srcParam = params.back().strict_as<ast::ObjectDecl>(); |
---|
| 669 | functionDecl->stmts = new ast::CompoundStmt( location, |
---|
| 670 | { makeAssignOp( location, dstParam, srcParam ) } |
---|
| 671 | ); |
---|
| 672 | } else { |
---|
| 673 | assert( 1 == params.size() ); |
---|
| 674 | // Default constructor and destructor is empty. |
---|
| 675 | functionDecl->stmts = new ast::CompoundStmt( location ); |
---|
| 676 | // Add unused attribute to parameter to silence warnings. |
---|
| 677 | addUnusedAttribute( params.front() ); |
---|
| 678 | |
---|
| 679 | // Just an extra step to make the forward and declaration match. |
---|
| 680 | if ( forwards.empty() ) return; |
---|
| 681 | ast::FunctionDecl * fwd = strict_dynamic_cast<ast::FunctionDecl *>( |
---|
| 682 | forwards.back().get_and_mutate() ); |
---|
| 683 | addUnusedAttribute( fwd->params.front() ); |
---|
| 684 | } |
---|
| 685 | } |
---|
| 686 | |
---|
| 687 | ast::ExprStmt * UnionFuncGenerator::makeAssignOp( const CodeLocation& location, |
---|
| 688 | const ast::ObjectDecl * dstParam, const ast::ObjectDecl * srcParam ) { |
---|
| 689 | return new ast::ExprStmt( location, new ast::UntypedExpr( |
---|
| 690 | location, |
---|
| 691 | new ast::NameExpr( location, "__builtin_memcpy" ), |
---|
| 692 | { |
---|
| 693 | new ast::AddressExpr( location, |
---|
| 694 | new ast::VariableExpr( location, dstParam ) ), |
---|
| 695 | new ast::AddressExpr( location, |
---|
| 696 | new ast::VariableExpr( location, srcParam ) ), |
---|
| 697 | new ast::SizeofExpr( location, srcParam->type ), |
---|
| 698 | } ) ); |
---|
| 699 | } |
---|
| 700 | |
---|
| 701 | void EnumFuncGenerator::genFieldCtors() { |
---|
| 702 | // Enumerations to not have field constructors. |
---|
| 703 | } |
---|
| 704 | |
---|
| 705 | void EnumFuncGenerator::genFuncBody( ast::FunctionDecl * functionDecl ) { |
---|
| 706 | const CodeLocation& location = functionDecl->location; |
---|
| 707 | auto & params = functionDecl->params; |
---|
| 708 | if ( InitTweak::isCopyConstructor( functionDecl ) |
---|
| 709 | || InitTweak::isAssignment( functionDecl ) ) { |
---|
| 710 | assert( 2 == params.size() ); |
---|
| 711 | auto dstParam = params.front().strict_as<ast::ObjectDecl>(); |
---|
| 712 | auto srcParam = params.back().strict_as<ast::ObjectDecl>(); |
---|
| 713 | |
---|
| 714 | /* This looks like a recursive call, but code-gen will turn it into |
---|
| 715 | * a C-style assignment. |
---|
| 716 | * |
---|
| 717 | * This is still before function pointer type conversion, |
---|
| 718 | * so this will have to do it manually. |
---|
| 719 | * |
---|
| 720 | * It will also reference the parent function declaration, creating |
---|
| 721 | * a cycle for references. This also means that the ref-counts are |
---|
| 722 | * now non-zero and the declaration will be deleted if it ever |
---|
| 723 | * returns to zero. |
---|
| 724 | */ |
---|
| 725 | auto callExpr = new ast::ApplicationExpr( location, |
---|
| 726 | ast::VariableExpr::functionPointer( location, functionDecl ), |
---|
| 727 | { |
---|
| 728 | new ast::VariableExpr( location, dstParam ), |
---|
| 729 | new ast::VariableExpr( location, srcParam ), |
---|
| 730 | } |
---|
| 731 | ); |
---|
| 732 | functionDecl->stmts = new ast::CompoundStmt( location, |
---|
| 733 | { new ast::ExprStmt( location, callExpr ) } |
---|
| 734 | ); |
---|
| 735 | } else { |
---|
| 736 | assert( 1 == params.size() ); |
---|
| 737 | // Default constructor and destructor is empty. |
---|
| 738 | functionDecl->stmts = new ast::CompoundStmt( location ); |
---|
| 739 | // Just add unused attribute to parameter to silence warnings. |
---|
| 740 | addUnusedAttribute( params.front() ); |
---|
| 741 | |
---|
| 742 | // Just an extra step to make the forward and declaration match. |
---|
| 743 | if ( forwards.empty() ) return; |
---|
| 744 | ast::FunctionDecl * fwd = strict_dynamic_cast<ast::FunctionDecl *>( |
---|
| 745 | forwards.back().get_and_mutate() ); |
---|
| 746 | addUnusedAttribute( fwd->params.front() ); |
---|
| 747 | } |
---|
| 748 | } |
---|
| 749 | |
---|
| 750 | void TypeFuncGenerator::genFieldCtors() { |
---|
| 751 | // Opaque types do not have field constructors. |
---|
| 752 | } |
---|
| 753 | |
---|
| 754 | void TypeFuncGenerator::genFuncBody( ast::FunctionDecl * functionDecl ) { |
---|
| 755 | const CodeLocation& location = functionDecl->location; |
---|
| 756 | auto & params = functionDecl->type->params; |
---|
| 757 | assertf( 1 == params.size() || 2 == params.size(), |
---|
| 758 | "Incorrect number of parameters in autogenerated typedecl function: %zd", |
---|
| 759 | params.size() ); |
---|
| 760 | auto dstParam = params.front().strict_as<ast::ObjectDecl>(); |
---|
| 761 | auto srcParam = (2 == params.size()) |
---|
| 762 | ? params.back().strict_as<ast::ObjectDecl>() : nullptr; |
---|
| 763 | // Generate appropriate calls to member constructor and assignment. |
---|
| 764 | ast::UntypedExpr * expr = new ast::UntypedExpr( location, |
---|
| 765 | new ast::NameExpr( location, functionDecl->name ) |
---|
| 766 | ); |
---|
| 767 | expr->args.push_back( new ast::CastExpr( location, |
---|
| 768 | new ast::VariableExpr( location, dstParam ), |
---|
| 769 | new ast::ReferenceType( decl->base ) |
---|
| 770 | ) ); |
---|
| 771 | if ( srcParam ) { |
---|
| 772 | expr->args.push_back( new ast::CastExpr( location, |
---|
| 773 | new ast::VariableExpr( location, srcParam ), |
---|
| 774 | decl->base |
---|
| 775 | ) ); |
---|
| 776 | } |
---|
| 777 | functionDecl->stmts = new ast::CompoundStmt( location, |
---|
| 778 | { new ast::ExprStmt( location, expr ) } |
---|
| 779 | ); |
---|
| 780 | } |
---|
| 781 | |
---|
| 782 | } // namespace |
---|
| 783 | |
---|
| 784 | void autogenerateRoutines( ast::TranslationUnit & translationUnit ) { |
---|
| 785 | ast::Pass<AutogenerateRoutines_new>::run( translationUnit ); |
---|
| 786 | } |
---|
| 787 | |
---|
| 788 | } // Validate |
---|
| 789 | |
---|
| 790 | // Local Variables: // |
---|
| 791 | // tab-width: 4 // |
---|
| 792 | // mode: c++ // |
---|
| 793 | // compile-command: "make install" // |
---|
| 794 | // End: // |
---|