[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 | // |
---|
[11df881] | 7 | // FixNames.cc -- Adjustments to typed declarations. |
---|
[51587aa] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[f42fc13] | 11 | // Last Modified By : Andrew Beach |
---|
[b585593] | 12 | // Last Modified On : Wed Jul 20 11:49:00 2022 |
---|
| 13 | // Update Count : 24 |
---|
[51587aa] | 14 | // |
---|
[51b7345] | 15 | |
---|
| 16 | #include "FixNames.h" |
---|
[bf2438c] | 17 | |
---|
| 18 | #include <memory> // for unique_ptr |
---|
| 19 | #include <string> // for string, operator!=, operator== |
---|
| 20 | |
---|
[0c577f7] | 21 | #include "AST/Chain.hpp" |
---|
| 22 | #include "AST/Expr.hpp" |
---|
| 23 | #include "AST/Pass.hpp" |
---|
[c50d54d] | 24 | #include "Common/PassVisitor.h" |
---|
[bf2438c] | 25 | #include "Common/SemanticError.h" // for SemanticError |
---|
| 26 | #include "FixMain.h" // for FixMain |
---|
| 27 | #include "SymTab/Mangler.h" // for Mangler |
---|
[07de76b] | 28 | #include "SynTree/LinkageSpec.h" // for Cforall, isMangled |
---|
[bf2438c] | 29 | #include "SynTree/Constant.h" // for Constant |
---|
| 30 | #include "SynTree/Declaration.h" // for FunctionDecl, ObjectDecl, Declarat... |
---|
| 31 | #include "SynTree/Expression.h" // for ConstantExpr |
---|
| 32 | #include "SynTree/Label.h" // for Label, noLabels |
---|
| 33 | #include "SynTree/Statement.h" // for ReturnStmt, CompoundStmt |
---|
| 34 | #include "SynTree/Type.h" // for Type, BasicType, Type::Qualifiers |
---|
| 35 | #include "SynTree/Visitor.h" // for Visitor, acceptAll |
---|
[16ba4a6f] | 36 | #include "CompilationState.h" |
---|
[0270824] | 37 | |
---|
[51b7345] | 38 | namespace CodeGen { |
---|
[c50d54d] | 39 | class FixNames : public WithGuards { |
---|
[51587aa] | 40 | public: |
---|
[c50d54d] | 41 | void postvisit( ObjectDecl *objectDecl ); |
---|
| 42 | void postvisit( FunctionDecl *functionDecl ); |
---|
[f326f99] | 43 | |
---|
[c50d54d] | 44 | void previsit( CompoundStmt *compoundStmt ); |
---|
[f326f99] | 45 | private: |
---|
| 46 | int scopeLevel = 1; |
---|
| 47 | |
---|
| 48 | void fixDWT( DeclarationWithType *dwt ); |
---|
[51587aa] | 49 | }; |
---|
| 50 | |
---|
[c50d54d] | 51 | void fixNames( std::list< Declaration* > & translationUnit ) { |
---|
| 52 | PassVisitor<FixNames> fixer; |
---|
[51587aa] | 53 | acceptAll( translationUnit, fixer ); |
---|
| 54 | } |
---|
| 55 | |
---|
[c50d54d] | 56 | void FixNames::fixDWT( DeclarationWithType * dwt ) { |
---|
[51587aa] | 57 | if ( dwt->get_name() != "" ) { |
---|
[fa4805f] | 58 | if ( LinkageSpec::isMangled( dwt->get_linkage() ) ) { |
---|
[16ba4a6f] | 59 | if (!useNewAST) { |
---|
| 60 | dwt->set_mangleName( SymTab::Mangler::mangle( dwt ) ); |
---|
| 61 | } |
---|
[f326f99] | 62 | dwt->set_scopeLevel( scopeLevel ); |
---|
[51587aa] | 63 | } // if |
---|
| 64 | } // if |
---|
| 65 | } |
---|
| 66 | |
---|
[c50d54d] | 67 | void FixNames::postvisit( ObjectDecl * objectDecl ) { |
---|
[51587aa] | 68 | fixDWT( objectDecl ); |
---|
| 69 | } |
---|
| 70 | |
---|
[c50d54d] | 71 | void FixNames::postvisit( FunctionDecl * functionDecl ) { |
---|
[51587aa] | 72 | fixDWT( functionDecl ); |
---|
[0270824] | 73 | |
---|
[f42fc13] | 74 | if ( FixMain::isMain( functionDecl ) ) { |
---|
[13de47bc] | 75 | int nargs = functionDecl->get_functionType()->get_parameters().size(); |
---|
| 76 | if( !(nargs == 0 || nargs == 2 || nargs == 3) ) { |
---|
[a16764a6] | 77 | SemanticError(functionDecl, "Main expected to have 0, 2 or 3 arguments\n"); |
---|
[0270824] | 78 | } |
---|
[ba3706f] | 79 | functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( new ConstantExpr( Constant::from_int( 0 ) ) ) ); |
---|
[0270824] | 80 | } |
---|
[51587aa] | 81 | } |
---|
[f326f99] | 82 | |
---|
[c50d54d] | 83 | void FixNames::previsit( CompoundStmt * ) { |
---|
[f326f99] | 84 | scopeLevel++; |
---|
[c50d54d] | 85 | GuardAction( [this](){ scopeLevel--; } ); |
---|
[f326f99] | 86 | } |
---|
[0c577f7] | 87 | |
---|
| 88 | /// Does work with the main function and scopeLevels. |
---|
[b585593] | 89 | class FixNames_new final { |
---|
[0c577f7] | 90 | int scopeLevel = 1; |
---|
| 91 | |
---|
| 92 | bool shouldSetScopeLevel( const ast::DeclWithType * dwt ) { |
---|
| 93 | return !dwt->name.empty() && dwt->linkage.is_mangled |
---|
| 94 | && dwt->scopeLevel != scopeLevel; |
---|
| 95 | } |
---|
| 96 | public: |
---|
| 97 | const ast::ObjectDecl *postvisit( const ast::ObjectDecl *objectDecl ) { |
---|
| 98 | if ( shouldSetScopeLevel( objectDecl ) ) { |
---|
| 99 | return ast::mutate_field( objectDecl, &ast::ObjectDecl::scopeLevel, scopeLevel ); |
---|
| 100 | } |
---|
| 101 | return objectDecl; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | const ast::FunctionDecl *postvisit( const ast::FunctionDecl *functionDecl ) { |
---|
| 105 | if ( FixMain::isMain( functionDecl ) ) { |
---|
[2fd0de0] | 106 | auto mutDecl = ast::mutate( functionDecl ); |
---|
| 107 | |
---|
| 108 | if ( shouldSetScopeLevel( mutDecl ) ) { |
---|
| 109 | mutDecl->scopeLevel = scopeLevel; |
---|
| 110 | } |
---|
[0c577f7] | 111 | |
---|
| 112 | int nargs = mutDecl->params.size(); |
---|
| 113 | if ( 0 != nargs && 2 != nargs && 3 != nargs ) { |
---|
| 114 | SemanticError( functionDecl, "Main expected to have 0, 2 or 3 arguments\n" ); |
---|
| 115 | } |
---|
| 116 | ast::chain_mutate( mutDecl->stmts )->kids.push_back( |
---|
| 117 | new ast::ReturnStmt( |
---|
| 118 | mutDecl->location, |
---|
| 119 | ast::ConstantExpr::from_int( mutDecl->location, 0 ) |
---|
| 120 | ) |
---|
| 121 | ); |
---|
[2fd0de0] | 122 | |
---|
| 123 | return mutDecl; |
---|
| 124 | } else if ( shouldSetScopeLevel( functionDecl ) ) { |
---|
| 125 | return ast::mutate_field( functionDecl, &ast::FunctionDecl::scopeLevel, scopeLevel ); |
---|
| 126 | } else { |
---|
| 127 | return functionDecl; |
---|
[0c577f7] | 128 | } |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | void previsit( const ast::CompoundStmt * ) { |
---|
[b585593] | 132 | scopeLevel += 1; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | void postvisit( const ast::CompoundStmt * ) { |
---|
| 136 | scopeLevel -= 1; |
---|
[0c577f7] | 137 | } |
---|
| 138 | }; |
---|
| 139 | |
---|
| 140 | void fixNames( ast::TranslationUnit & translationUnit ) { |
---|
| 141 | ast::Pass<FixNames_new>::run( translationUnit ); |
---|
| 142 | } |
---|
| 143 | |
---|
[51b7345] | 144 | } // namespace CodeGen |
---|
[51587aa] | 145 | |
---|
| 146 | // Local Variables: // |
---|
| 147 | // tab-width: 4 // |
---|
| 148 | // mode: c++ // |
---|
| 149 | // compile-command: "make install" // |
---|
| 150 | // End: // |
---|