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