[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 | //
|
---|
[51b73452] | 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
|
---|
[16ba4a6f] | 27 | #include "CompilationState.h"
|
---|
[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 ) {
|
---|
| 59 | SemanticError( functionDecl, "Main expected to have 0, 2 or 3 arguments\n" );
|
---|
| 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: //
|
---|