| 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 | // Create.cpp -- Helpers to create pieces of the AST.
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Andrew Beach
 | 
|---|
| 10 | // Created On       : Tue Sep 20 13:28:00 2022
 | 
|---|
| 11 | // Last Modified By : Andrew Beach
 | 
|---|
| 12 | // Last Modified On : Tue Sep 21  9:29:00 2022
 | 
|---|
| 13 | // Update Count     : 1
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "AST/Create.hpp"
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include "AST/Attribute.hpp"
 | 
|---|
| 19 | #include "AST/Copy.hpp"
 | 
|---|
| 20 | #include "AST/Decl.hpp"
 | 
|---|
| 21 | #include "AST/Type.hpp"
 | 
|---|
| 22 | #include "Common/Iterate.hpp"
 | 
|---|
| 23 | 
 | 
|---|
| 24 | namespace ast {
 | 
|---|
| 25 | 
 | 
|---|
| 26 | namespace {
 | 
|---|
| 27 | 
 | 
|---|
| 28 |         template<typename node_t>
 | 
|---|
| 29 |         std::vector<ast::ptr<node_t>> vectorCopy(
 | 
|---|
| 30 |                         std::vector<ast::ptr<node_t>> const & nodes ) {
 | 
|---|
| 31 |                 return map_range<std::vector<ast::ptr<node_t>>>( nodes,
 | 
|---|
| 32 |                         []( ptr<node_t> const & node ){
 | 
|---|
| 33 |                                 return deepCopy<node_t>( node );
 | 
|---|
| 34 |                         }
 | 
|---|
| 35 |                 );
 | 
|---|
| 36 |         }
 | 
|---|
| 37 | 
 | 
|---|
| 38 | } // namespace
 | 
|---|
| 39 | 
 | 
|---|
| 40 | FunctionDecl * asForward( FunctionDecl const * decl ) {
 | 
|---|
| 41 |         if ( nullptr == decl->stmts ) {
 | 
|---|
| 42 |                 return nullptr;
 | 
|---|
| 43 |         }
 | 
|---|
| 44 |         // The cast and changing the original should be safe as long as the
 | 
|---|
| 45 |         // change is reverted before anything else sees it. It's also faster.
 | 
|---|
| 46 |         FunctionDecl * mutDecl = const_cast<FunctionDecl *>( decl );
 | 
|---|
| 47 |         CompoundStmt const * stmts = mutDecl->stmts.release();
 | 
|---|
| 48 |         FunctionDecl * copy = deepCopy( mutDecl );
 | 
|---|
| 49 |         mutDecl->stmts = stmts;
 | 
|---|
| 50 |         return copy;
 | 
|---|
| 51 | }
 | 
|---|
| 52 | 
 | 
|---|
| 53 | StructDecl * asForward( StructDecl const * decl ) {
 | 
|---|
| 54 |         if ( !decl->body ) {
 | 
|---|
| 55 |                 return nullptr;
 | 
|---|
| 56 |         }
 | 
|---|
| 57 |         StructDecl * fwd = new StructDecl( decl->location,
 | 
|---|
| 58 |                 decl->name,
 | 
|---|
| 59 |                 decl->kind,
 | 
|---|
| 60 |                 vectorCopy<ast::Attribute>( decl->attributes ),
 | 
|---|
| 61 |                 decl->linkage );
 | 
|---|
| 62 |         fwd->params = vectorCopy( decl->params );
 | 
|---|
| 63 |         return fwd;
 | 
|---|
| 64 | }
 | 
|---|
| 65 | 
 | 
|---|
| 66 | UnionDecl * asForward( UnionDecl const * decl ) {
 | 
|---|
| 67 |         if ( !decl->body ) {
 | 
|---|
| 68 |                 return nullptr;
 | 
|---|
| 69 |         }
 | 
|---|
| 70 |         UnionDecl * fwd = new UnionDecl( decl->location,
 | 
|---|
| 71 |                 decl->name,
 | 
|---|
| 72 |                 vectorCopy( decl->attributes ),
 | 
|---|
| 73 |                 decl->linkage );
 | 
|---|
| 74 |         fwd->params = vectorCopy( decl->params );
 | 
|---|
| 75 |         return fwd;
 | 
|---|
| 76 | }
 | 
|---|
| 77 | 
 | 
|---|
| 78 | EnumDecl * asForward( EnumDecl const * decl ) {
 | 
|---|
| 79 |         if ( !decl->body ) {
 | 
|---|
| 80 |                 return nullptr;
 | 
|---|
| 81 |         }
 | 
|---|
| 82 |         EnumDecl * fwd = new EnumDecl( decl->location,
 | 
|---|
| 83 |                 decl->name,
 | 
|---|
| 84 |                 decl->isCfa,
 | 
|---|
| 85 |                 vectorCopy( decl->attributes ),
 | 
|---|
| 86 |                 decl->linkage,
 | 
|---|
| 87 |                 decl->base,
 | 
|---|
| 88 |                 decl->hide
 | 
|---|
| 89 |         );
 | 
|---|
| 90 |         fwd->params = vectorCopy( decl->params );
 | 
|---|
| 91 |         return fwd;
 | 
|---|
| 92 | }
 | 
|---|
| 93 | 
 | 
|---|
| 94 | }
 | 
|---|