[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 | //
|
---|
[c92bdcc] | 7 | // Generate.cpp --
|
---|
[51587aa] | 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Mon May 18 07:44:20 2015
|
---|
[07de76b] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[60a8062] | 12 | // Last Modified On : Sun Feb 16 03:01:51 2020
|
---|
| 13 | // Update Count : 9
|
---|
[51587aa] | 14 | //
|
---|
[c92bdcc] | 15 | #include "Generate.hpp"
|
---|
[51587aa] | 16 |
|
---|
[bf2438c] | 17 | #include <iostream> // for ostream, endl, operator<<
|
---|
| 18 | #include <list> // for list
|
---|
| 19 | #include <string> // for operator<<
|
---|
[51b73452] | 20 |
|
---|
[83fd57d] | 21 | #include "CodeGenerator.hpp" // for CodeGenerator, doSemicolon, ...
|
---|
[c92bdcc] | 22 | #include "GenType.hpp" // for genPrettyType
|
---|
[51b73452] | 23 |
|
---|
| 24 | using namespace std;
|
---|
| 25 |
|
---|
| 26 | namespace CodeGen {
|
---|
[8941b6b] | 27 |
|
---|
| 28 | namespace {
|
---|
| 29 | bool shouldClean( ast::Decl const * decl ) {
|
---|
| 30 | return dynamic_cast<ast::TraitDecl const *>( decl );
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | /// Removes various nodes that should not exist in CodeGen.
|
---|
[0bd3faf] | 34 | struct TreeCleaner final {
|
---|
[8941b6b] | 35 | ast::CompoundStmt const * previsit( ast::CompoundStmt const * stmt ) {
|
---|
| 36 | auto mutStmt = ast::mutate( stmt );
|
---|
| 37 | erase_if( mutStmt->kids, []( ast::Stmt const * stmt ){
|
---|
| 38 | auto declStmt = dynamic_cast<ast::DeclStmt const *>( stmt );
|
---|
| 39 | return ( declStmt ) ? shouldClean( declStmt->decl ) : false;
|
---|
| 40 | } );
|
---|
| 41 | return mutStmt;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | ast::Stmt const * postvisit( ast::ImplicitCtorDtorStmt const * stmt ) {
|
---|
| 45 | return stmt->callStmt;
|
---|
| 46 | }
|
---|
| 47 | };
|
---|
[7959e56] | 48 |
|
---|
| 49 | struct ZeroOneObjectHider final {
|
---|
| 50 | ast::ObjectDecl const * postvisit( ast::ObjectDecl const * decl ) {
|
---|
| 51 | if ( decl->type.as<ast::ZeroType>() || decl->type.as<ast::OneType>() ) {
|
---|
| 52 | ast::ObjectDecl * mutDecl = ast::mutate( decl );
|
---|
| 53 | mutDecl->attributes.push_back( new ast::Attribute( "unused" ) );
|
---|
| 54 | return mutDecl;
|
---|
| 55 | }
|
---|
| 56 | return decl;
|
---|
| 57 | }
|
---|
| 58 | };
|
---|
[8941b6b] | 59 | } // namespace
|
---|
| 60 |
|
---|
| 61 | void generate( ast::TranslationUnit & translationUnit, std::ostream & os, bool doIntrinsics,
|
---|
| 62 | bool pretty, bool generateC, bool lineMarks, bool printExprTypes ) {
|
---|
| 63 | erase_if( translationUnit.decls, shouldClean );
|
---|
[0bd3faf] | 64 | ast::Pass<TreeCleaner>::run( translationUnit );
|
---|
[7959e56] | 65 | ast::Pass<ZeroOneObjectHider>::run( translationUnit );
|
---|
[8941b6b] | 66 |
|
---|
[0bd3faf] | 67 | ast::Pass<CodeGenerator> cgv( os,
|
---|
[8941b6b] | 68 | Options( pretty, generateC, lineMarks, printExprTypes ) );
|
---|
| 69 | for ( auto & decl : translationUnit.decls ) {
|
---|
| 70 | if ( decl->linkage.is_generatable && (doIntrinsics || !decl->linkage.is_builtin ) ) {
|
---|
| 71 | cgv.core.updateLocation( decl );
|
---|
| 72 | decl->accept( cgv );
|
---|
| 73 | if ( doSemicolon( decl ) ) {
|
---|
| 74 | os << ";";
|
---|
| 75 | }
|
---|
| 76 | os << cgv.core.endl;
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[9b9d3f9] | 81 | // to be invoked manually from GDB
|
---|
| 82 | void generate( const ast::Node * node, std::ostream & os,
|
---|
| 83 | bool pretty, bool generateC, bool lineMarks, bool printExprTypes ) {
|
---|
| 84 | ast::Pass<CodeGenerator> cgv( os,
|
---|
| 85 | Options( pretty, generateC, lineMarks, printExprTypes ) );
|
---|
| 86 | node->accept( cgv );
|
---|
| 87 | if ( auto decl = dynamic_cast<const ast::Decl *>( node ) ) {
|
---|
| 88 | if ( doSemicolon( decl ) ) {
|
---|
| 89 | os << ";";
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | os << cgv.core.endl;
|
---|
| 93 | }
|
---|
| 94 | void generate( const ast::Node * node, std::ostream & os ) {
|
---|
| 95 | generate( node, os, true, false, false, false );
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[51b73452] | 98 | } // namespace CodeGen
|
---|
[51587aa] | 99 |
|
---|
| 100 | // Local Variables: //
|
---|
| 101 | // tab-width: 4 //
|
---|
| 102 | // mode: c++ //
|
---|
| 103 | // compile-command: "make install" //
|
---|
| 104 | // End: //
|
---|