| [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 | // FixNames.cpp -- Adjustments to typed declarations. | 
|---|
| [51587aa] | 8 | // | 
|---|
|  | 9 | // Author           : Richard C. Bilson | 
|---|
|  | 10 | // Created On       : Mon May 18 07:44:20 2015 | 
|---|
| [ca9d65e] | 11 | // Last Modified By : Peter A. Buhr | 
|---|
|  | 12 | // Last Modified On : Thu Dec 14 16:16:51 2023 | 
|---|
|  | 13 | // Update Count     : 25 | 
|---|
| [51587aa] | 14 | // | 
|---|
| [51b73452] | 15 |  | 
|---|
| [c92bdcc] | 16 | #include "FixNames.hpp" | 
|---|
| [bf2438c] | 17 |  | 
|---|
| [c92bdcc] | 18 | #include <memory>                    // for unique_ptr | 
|---|
|  | 19 | #include <string>                    // for string, operator!=, operator== | 
|---|
| [bf2438c] | 20 |  | 
|---|
| [0c577f7] | 21 | #include "AST/Chain.hpp" | 
|---|
|  | 22 | #include "AST/Expr.hpp" | 
|---|
|  | 23 | #include "AST/Pass.hpp" | 
|---|
| [c92bdcc] | 24 | #include "Common/SemanticError.hpp"  // for SemanticError | 
|---|
|  | 25 | #include "FixMain.hpp"               // for FixMain | 
|---|
|  | 26 | #include "SymTab/Mangler.hpp"        // for Mangler | 
|---|
| [b2ea0cd] | 27 | #include "CompilationState.hpp" | 
|---|
| [0270824] | 28 |  | 
|---|
| [51b73452] | 29 | namespace CodeGen { | 
|---|
| [61efa42] | 30 |  | 
|---|
|  | 31 | namespace { | 
|---|
|  | 32 |  | 
|---|
| [0c577f7] | 33 | /// Does work with the main function and scopeLevels. | 
|---|
| [61efa42] | 34 | class FixNames final { | 
|---|
| [0c577f7] | 35 | int scopeLevel = 1; | 
|---|
|  | 36 |  | 
|---|
|  | 37 | bool shouldSetScopeLevel( const ast::DeclWithType * dwt ) { | 
|---|
|  | 38 | return !dwt->name.empty() && dwt->linkage.is_mangled | 
|---|
|  | 39 | && dwt->scopeLevel != scopeLevel; | 
|---|
|  | 40 | } | 
|---|
|  | 41 | public: | 
|---|
|  | 42 | const ast::ObjectDecl *postvisit( const ast::ObjectDecl *objectDecl ) { | 
|---|
|  | 43 | if ( shouldSetScopeLevel( objectDecl ) ) { | 
|---|
|  | 44 | return ast::mutate_field( objectDecl, &ast::ObjectDecl::scopeLevel, scopeLevel ); | 
|---|
|  | 45 | } | 
|---|
|  | 46 | return objectDecl; | 
|---|
|  | 47 | } | 
|---|
|  | 48 |  | 
|---|
|  | 49 | const ast::FunctionDecl *postvisit( const ast::FunctionDecl *functionDecl ) { | 
|---|
| [61efa42] | 50 | if ( isMain( functionDecl ) ) { | 
|---|
| [2fd0de0] | 51 | auto mutDecl = ast::mutate( functionDecl ); | 
|---|
|  | 52 |  | 
|---|
|  | 53 | if ( shouldSetScopeLevel( mutDecl ) ) { | 
|---|
|  | 54 | mutDecl->scopeLevel = scopeLevel; | 
|---|
|  | 55 | } | 
|---|
| [0c577f7] | 56 |  | 
|---|
|  | 57 | int nargs = mutDecl->params.size(); | 
|---|
|  | 58 | if ( 0 != nargs && 2 != nargs && 3 != nargs ) { | 
|---|
| [ca9d65e] | 59 | SemanticError( functionDecl, "Main expected to have 0, 2 or 3 arguments" ); | 
|---|
| [0c577f7] | 60 | } | 
|---|
|  | 61 | ast::chain_mutate( mutDecl->stmts )->kids.push_back( | 
|---|
|  | 62 | new ast::ReturnStmt( | 
|---|
|  | 63 | mutDecl->location, | 
|---|
|  | 64 | ast::ConstantExpr::from_int( mutDecl->location, 0 ) | 
|---|
|  | 65 | ) | 
|---|
|  | 66 | ); | 
|---|
| [2fd0de0] | 67 |  | 
|---|
|  | 68 | return mutDecl; | 
|---|
|  | 69 | } else if ( shouldSetScopeLevel( functionDecl ) ) { | 
|---|
|  | 70 | return ast::mutate_field( functionDecl, &ast::FunctionDecl::scopeLevel, scopeLevel ); | 
|---|
|  | 71 | } else { | 
|---|
|  | 72 | return functionDecl; | 
|---|
| [0c577f7] | 73 | } | 
|---|
|  | 74 | } | 
|---|
|  | 75 |  | 
|---|
|  | 76 | void previsit( const ast::CompoundStmt * ) { | 
|---|
| [b585593] | 77 | scopeLevel += 1; | 
|---|
|  | 78 | } | 
|---|
|  | 79 |  | 
|---|
|  | 80 | void postvisit( const ast::CompoundStmt * ) { | 
|---|
|  | 81 | scopeLevel -= 1; | 
|---|
| [0c577f7] | 82 | } | 
|---|
|  | 83 | }; | 
|---|
|  | 84 |  | 
|---|
| [61efa42] | 85 | } // namespace | 
|---|
|  | 86 |  | 
|---|
| [0c577f7] | 87 | void fixNames( ast::TranslationUnit & translationUnit ) { | 
|---|
| [61efa42] | 88 | ast::Pass<FixNames>::run( translationUnit ); | 
|---|
| [0c577f7] | 89 | } | 
|---|
|  | 90 |  | 
|---|
| [51b73452] | 91 | } // namespace CodeGen | 
|---|
| [51587aa] | 92 |  | 
|---|
|  | 93 | // Local Variables: // | 
|---|
|  | 94 | // tab-width: 4 // | 
|---|
|  | 95 | // mode: c++ // | 
|---|
|  | 96 | // compile-command: "make install" // | 
|---|
|  | 97 | // End: // | 
|---|