[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" |
---|
[bf2438c] | 24 | #include "Common/SemanticError.h" // for SemanticError |
---|
| 25 | #include "FixMain.h" // for FixMain |
---|
| 26 | #include "SymTab/Mangler.h" // for Mangler |
---|
[16ba4a6] | 27 | #include "CompilationState.h" |
---|
[0270824] | 28 | |
---|
[51b7345] | 29 | namespace CodeGen { |
---|
[0c577f7] | 30 | /// Does work with the main function and scopeLevels. |
---|
[b585593] | 31 | class FixNames_new final { |
---|
[0c577f7] | 32 | int scopeLevel = 1; |
---|
| 33 | |
---|
| 34 | bool shouldSetScopeLevel( const ast::DeclWithType * dwt ) { |
---|
| 35 | return !dwt->name.empty() && dwt->linkage.is_mangled |
---|
| 36 | && dwt->scopeLevel != scopeLevel; |
---|
| 37 | } |
---|
| 38 | public: |
---|
| 39 | const ast::ObjectDecl *postvisit( const ast::ObjectDecl *objectDecl ) { |
---|
| 40 | if ( shouldSetScopeLevel( objectDecl ) ) { |
---|
| 41 | return ast::mutate_field( objectDecl, &ast::ObjectDecl::scopeLevel, scopeLevel ); |
---|
| 42 | } |
---|
| 43 | return objectDecl; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | const ast::FunctionDecl *postvisit( const ast::FunctionDecl *functionDecl ) { |
---|
| 47 | if ( FixMain::isMain( functionDecl ) ) { |
---|
[2fd0de0] | 48 | auto mutDecl = ast::mutate( functionDecl ); |
---|
| 49 | |
---|
| 50 | if ( shouldSetScopeLevel( mutDecl ) ) { |
---|
| 51 | mutDecl->scopeLevel = scopeLevel; |
---|
| 52 | } |
---|
[0c577f7] | 53 | |
---|
| 54 | int nargs = mutDecl->params.size(); |
---|
| 55 | if ( 0 != nargs && 2 != nargs && 3 != nargs ) { |
---|
| 56 | SemanticError( functionDecl, "Main expected to have 0, 2 or 3 arguments\n" ); |
---|
| 57 | } |
---|
| 58 | ast::chain_mutate( mutDecl->stmts )->kids.push_back( |
---|
| 59 | new ast::ReturnStmt( |
---|
| 60 | mutDecl->location, |
---|
| 61 | ast::ConstantExpr::from_int( mutDecl->location, 0 ) |
---|
| 62 | ) |
---|
| 63 | ); |
---|
[2fd0de0] | 64 | |
---|
| 65 | return mutDecl; |
---|
| 66 | } else if ( shouldSetScopeLevel( functionDecl ) ) { |
---|
| 67 | return ast::mutate_field( functionDecl, &ast::FunctionDecl::scopeLevel, scopeLevel ); |
---|
| 68 | } else { |
---|
| 69 | return functionDecl; |
---|
[0c577f7] | 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | void previsit( const ast::CompoundStmt * ) { |
---|
[b585593] | 74 | scopeLevel += 1; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | void postvisit( const ast::CompoundStmt * ) { |
---|
| 78 | scopeLevel -= 1; |
---|
[0c577f7] | 79 | } |
---|
| 80 | }; |
---|
| 81 | |
---|
| 82 | void fixNames( ast::TranslationUnit & translationUnit ) { |
---|
| 83 | ast::Pass<FixNames_new>::run( translationUnit ); |
---|
| 84 | } |
---|
| 85 | |
---|
[51b7345] | 86 | } // namespace CodeGen |
---|
[51587aa] | 87 | |
---|
| 88 | // Local Variables: // |
---|
| 89 | // tab-width: 4 // |
---|
| 90 | // mode: c++ // |
---|
| 91 | // compile-command: "make install" // |
---|
| 92 | // End: // |
---|