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 visit( CompoundStmt * stmt );
|
---|
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 |
|
---|
47 | void generate( std::list< Declaration* > translationUnit, std::ostream &os, bool doIntrinsics, bool pretty, bool generateC, bool lineMarks ) {
|
---|
48 | cleanTree( translationUnit );
|
---|
49 |
|
---|
50 | CodeGen::CodeGenerator cgv( os, pretty, generateC, lineMarks );
|
---|
51 | for ( auto & dcl : translationUnit ) {
|
---|
52 | if ( LinkageSpec::isGeneratable( dcl->get_linkage() ) && (doIntrinsics || ! LinkageSpec::isBuiltin( dcl->get_linkage() ) ) ) {
|
---|
53 | cgv.updateLocation( dcl );
|
---|
54 | dcl->accept(cgv);
|
---|
55 | if ( doSemicolon( dcl ) ) {
|
---|
56 | os << ";";
|
---|
57 | } // if
|
---|
58 | os << std::endl;
|
---|
59 | } // if
|
---|
60 | } // for
|
---|
61 | }
|
---|
62 |
|
---|
63 | void generate( BaseSyntaxNode * node, std::ostream & os ) {
|
---|
64 | if ( Type * type = dynamic_cast< Type * >( node ) ) {
|
---|
65 | os << CodeGen::genPrettyType( type, "" );
|
---|
66 | } else {
|
---|
67 | CodeGen::CodeGenerator cgv( os, true, false, false );
|
---|
68 | node->accept( cgv );
|
---|
69 | }
|
---|
70 | os << std::endl;
|
---|
71 | }
|
---|
72 |
|
---|
73 | namespace {
|
---|
74 | void TreeCleaner::visit( CompoundStmt * cstmt ) {
|
---|
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
|
---|
87 | } // namespace CodeGen
|
---|
88 |
|
---|
89 | // Local Variables: //
|
---|
90 | // tab-width: 4 //
|
---|
91 | // mode: c++ //
|
---|
92 | // compile-command: "make install" //
|
---|
93 | // End: //
|
---|