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