| [51587aa] | 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 | //
 | 
|---|
| [35b1bf4] | 7 | // Generate.cc --
 | 
|---|
| [51587aa] | 8 | //
 | 
|---|
 | 9 | // Author           : Richard C. Bilson
 | 
|---|
 | 10 | // Created On       : Mon May 18 07:44:20 2015
 | 
|---|
| [c850687] | 11 | // Last Modified By : Andrew Beach
 | 
|---|
| [0dd18fd] | 12 | // Last Modified On : Fri Aug 18 15:39:00 2017
 | 
|---|
 | 13 | // Update Count     : 7
 | 
|---|
| [51587aa] | 14 | //
 | 
|---|
| [3268a58] | 15 | #include "Generate.h"
 | 
|---|
| [51587aa] | 16 | 
 | 
|---|
| [bf2438c] | 17 | #include <iostream>                  // for ostream, endl, operator<<
 | 
|---|
 | 18 | #include <list>                      // for list
 | 
|---|
 | 19 | #include <string>                    // for operator<<
 | 
|---|
| [51b73452] | 20 | 
 | 
|---|
| [bf2438c] | 21 | #include "CodeGenerator.h"           // for CodeGenerator, doSemicolon, oper...
 | 
|---|
 | 22 | #include "GenType.h"                 // for genPrettyType
 | 
|---|
| [a984e65] | 23 | #include "Common/PassVisitor.h"      // for PassVisitor
 | 
|---|
| [bf2438c] | 24 | #include "Parser/LinkageSpec.h"      // for isBuiltin, isGeneratable
 | 
|---|
 | 25 | #include "SynTree/BaseSyntaxNode.h"  // for BaseSyntaxNode
 | 
|---|
 | 26 | #include "SynTree/Declaration.h"     // for Declaration
 | 
|---|
 | 27 | #include "SynTree/Type.h"            // for Type
 | 
|---|
| [51b73452] | 28 | 
 | 
|---|
 | 29 | using namespace std;
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | namespace CodeGen {
 | 
|---|
| [a984e65] | 32 |         namespace {
 | 
|---|
 | 33 |                 /// Removes misc. nodes that should not exist in CodeGen
 | 
|---|
 | 34 |                 struct TreeCleaner {
 | 
|---|
| [acdfb45] | 35 |                         void premutate( CompoundStmt * stmt );
 | 
|---|
 | 36 |                         Statement * postmutate( ImplicitCtorDtorStmt * stmt );
 | 
|---|
| [a984e65] | 37 | 
 | 
|---|
 | 38 |                         static bool shouldClean( Declaration * );
 | 
|---|
 | 39 |                 };
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 |                 void cleanTree( std::list< Declaration * > & translationUnit ) {
 | 
|---|
 | 42 |                         PassVisitor<TreeCleaner> cleaner;
 | 
|---|
 | 43 |                         filter( translationUnit, [](Declaration * decl) { return TreeCleaner::shouldClean(decl); }, false );
 | 
|---|
| [acdfb45] | 44 |                         mutateAll( translationUnit, cleaner );
 | 
|---|
| [a984e65] | 45 |                 } // cleanTree
 | 
|---|
 | 46 |         } // namespace
 | 
|---|
 | 47 | 
 | 
|---|
| [5f08961d] | 48 |         void generate( std::list< Declaration* > translationUnit, std::ostream &os, bool doIntrinsics, bool pretty, bool generateC, bool lineMarks, bool printExprTypes ) {
 | 
|---|
| [a984e65] | 49 |                 cleanTree( translationUnit );
 | 
|---|
 | 50 | 
 | 
|---|
| [5f08961d] | 51 |                 PassVisitor<CodeGenerator> cgv( os, pretty, generateC, lineMarks, printExprTypes );
 | 
|---|
| [e6512c8] | 52 |                 for ( auto & dcl : translationUnit ) {
 | 
|---|
 | 53 |                         if ( LinkageSpec::isGeneratable( dcl->get_linkage() ) && (doIntrinsics || ! LinkageSpec::isBuiltin( dcl->get_linkage() ) ) ) {
 | 
|---|
| [9857e8d] | 54 |                                 cgv.pass.updateLocation( dcl );
 | 
|---|
| [e6512c8] | 55 |                                 dcl->accept(cgv);
 | 
|---|
 | 56 |                                 if ( doSemicolon( dcl ) ) {
 | 
|---|
| [51587aa] | 57 |                                         os << ";";
 | 
|---|
 | 58 |                                 } // if
 | 
|---|
| [d22e90f] | 59 |                                 os << cgv.pass.endl;
 | 
|---|
| [51587aa] | 60 |                         } // if
 | 
|---|
 | 61 |                 } // for
 | 
|---|
 | 62 |         }
 | 
|---|
| [262f085f] | 63 | 
 | 
|---|
 | 64 |         void generate( BaseSyntaxNode * node, std::ostream & os ) {
 | 
|---|
 | 65 |                 if ( Type * type = dynamic_cast< Type * >( node ) ) {
 | 
|---|
 | 66 |                         os << CodeGen::genPrettyType( type, "" );
 | 
|---|
 | 67 |                 } else {
 | 
|---|
| [5f08961d] | 68 |                         PassVisitor<CodeGenerator> cgv( os, true, false, false, false );
 | 
|---|
| [262f085f] | 69 |                         node->accept( cgv );
 | 
|---|
 | 70 |                 }
 | 
|---|
 | 71 |                 os << std::endl;
 | 
|---|
 | 72 |         }
 | 
|---|
| [a984e65] | 73 | 
 | 
|---|
 | 74 |         namespace {
 | 
|---|
| [acdfb45] | 75 |                 void TreeCleaner::premutate( CompoundStmt * cstmt ) {
 | 
|---|
| [a984e65] | 76 |                         filter( cstmt->kids, [](Statement * stmt) {
 | 
|---|
 | 77 |                                 if ( DeclStmt * declStmt = dynamic_cast< DeclStmt * >( stmt ) ) {
 | 
|---|
 | 78 |                                         return shouldClean( declStmt->decl );
 | 
|---|
 | 79 |                                 }
 | 
|---|
 | 80 |                                 return false;
 | 
|---|
 | 81 |                         }, false );
 | 
|---|
 | 82 |                 }
 | 
|---|
 | 83 | 
 | 
|---|
| [acdfb45] | 84 |                 Statement * TreeCleaner::postmutate( ImplicitCtorDtorStmt * stmt ) {
 | 
|---|
 | 85 |                         Statement * callStmt = nullptr;
 | 
|---|
 | 86 |                         std::swap( stmt->callStmt, callStmt );
 | 
|---|
 | 87 |                         delete stmt;
 | 
|---|
 | 88 |                         return callStmt;
 | 
|---|
 | 89 |                 }
 | 
|---|
 | 90 | 
 | 
|---|
| [a984e65] | 91 |                 bool TreeCleaner::shouldClean( Declaration * decl ) {
 | 
|---|
 | 92 |                         return dynamic_cast< TraitDecl * >( decl );
 | 
|---|
 | 93 |                 }
 | 
|---|
 | 94 |         } // namespace
 | 
|---|
| [51b73452] | 95 | } // namespace CodeGen
 | 
|---|
| [51587aa] | 96 | 
 | 
|---|
 | 97 | // Local Variables: //
 | 
|---|
 | 98 | // tab-width: 4 //
 | 
|---|
 | 99 | // mode: c++ //
 | 
|---|
 | 100 | // compile-command: "make install" //
 | 
|---|
 | 101 | // End: //
 | 
|---|