[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<< |
---|
[51b7345] | 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 |
---|
[51b7345] | 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 { |
---|
[a54840b] | 35 | void previsit( CompoundStmt * stmt ); |
---|
[a984e65] | 36 | |
---|
| 37 | static bool shouldClean( Declaration * ); |
---|
| 38 | }; |
---|
| 39 | |
---|
| 40 | void cleanTree( std::list< Declaration * > & translationUnit ) { |
---|
| 41 | PassVisitor<TreeCleaner> cleaner; |
---|
| 42 | filter( translationUnit, [](Declaration * decl) { return TreeCleaner::shouldClean(decl); }, false ); |
---|
| 43 | acceptAll( translationUnit, cleaner ); |
---|
| 44 | } // cleanTree |
---|
| 45 | } // namespace |
---|
| 46 | |
---|
[c850687] | 47 | void generate( std::list< Declaration* > translationUnit, std::ostream &os, bool doIntrinsics, bool pretty, bool generateC, bool lineMarks ) { |
---|
[a984e65] | 48 | cleanTree( translationUnit ); |
---|
| 49 | |
---|
[c850687] | 50 | CodeGen::CodeGenerator cgv( os, pretty, generateC, lineMarks ); |
---|
[e6512c8] | 51 | for ( auto & dcl : translationUnit ) { |
---|
| 52 | if ( LinkageSpec::isGeneratable( dcl->get_linkage() ) && (doIntrinsics || ! LinkageSpec::isBuiltin( dcl->get_linkage() ) ) ) { |
---|
[0dd18fd] | 53 | cgv.updateLocation( dcl ); |
---|
[e6512c8] | 54 | dcl->accept(cgv); |
---|
| 55 | if ( doSemicolon( dcl ) ) { |
---|
[51587aa] | 56 | os << ";"; |
---|
| 57 | } // if |
---|
| 58 | os << std::endl; |
---|
| 59 | } // if |
---|
| 60 | } // for |
---|
| 61 | } |
---|
[262f085f] | 62 | |
---|
| 63 | void generate( BaseSyntaxNode * node, std::ostream & os ) { |
---|
| 64 | if ( Type * type = dynamic_cast< Type * >( node ) ) { |
---|
| 65 | os << CodeGen::genPrettyType( type, "" ); |
---|
| 66 | } else { |
---|
[c850687] | 67 | CodeGen::CodeGenerator cgv( os, true, false, false ); |
---|
[262f085f] | 68 | node->accept( cgv ); |
---|
| 69 | } |
---|
| 70 | os << std::endl; |
---|
| 71 | } |
---|
[a984e65] | 72 | |
---|
| 73 | namespace { |
---|
[a54840b] | 74 | void TreeCleaner::previsit( CompoundStmt * cstmt ) { |
---|
[a984e65] | 75 | filter( cstmt->kids, [](Statement * stmt) { |
---|
| 76 | if ( DeclStmt * declStmt = dynamic_cast< DeclStmt * >( stmt ) ) { |
---|
| 77 | return shouldClean( declStmt->decl ); |
---|
| 78 | } |
---|
| 79 | return false; |
---|
| 80 | }, false ); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | bool TreeCleaner::shouldClean( Declaration * decl ) { |
---|
| 84 | return dynamic_cast< TraitDecl * >( decl ); |
---|
| 85 | } |
---|
| 86 | } // namespace |
---|
[51b7345] | 87 | } // namespace CodeGen |
---|
[51587aa] | 88 | |
---|
| 89 | // Local Variables: // |
---|
| 90 | // tab-width: 4 // |
---|
| 91 | // mode: c++ // |
---|
| 92 | // compile-command: "make install" // |
---|
| 93 | // End: // |
---|