source: src/CodeGen/FixNames.cc@ 36e6f10

Last change on this file since 36e6f10 was 2fd0de0, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Another clean-up pass on fix names since I was in the area. This one is just for readability.

  • Property mode set to 100644
File size: 4.4 KB
Line 
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//
7// FixNames.cc -- Adjustments to typed declarations.
8//
9// Author : Richard C. Bilson
10// Created On : Mon May 18 07:44:20 2015
11// Last Modified By : Andrew Beach
12// Last Modified On : Wed Jul 20 11:49:00 2022
13// Update Count : 24
14//
15
16#include "FixNames.h"
17
18#include <memory> // for unique_ptr
19#include <string> // for string, operator!=, operator==
20
21#include "AST/Chain.hpp"
22#include "AST/Expr.hpp"
23#include "AST/Pass.hpp"
24#include "Common/PassVisitor.h"
25#include "Common/SemanticError.h" // for SemanticError
26#include "FixMain.h" // for FixMain
27#include "SymTab/Mangler.h" // for Mangler
28#include "SynTree/LinkageSpec.h" // for Cforall, isMangled
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
36#include "CompilationState.h"
37
38namespace CodeGen {
39 class FixNames : public WithGuards {
40 public:
41 void postvisit( ObjectDecl *objectDecl );
42 void postvisit( FunctionDecl *functionDecl );
43
44 void previsit( CompoundStmt *compoundStmt );
45 private:
46 int scopeLevel = 1;
47
48 void fixDWT( DeclarationWithType *dwt );
49 };
50
51 void fixNames( std::list< Declaration* > & translationUnit ) {
52 PassVisitor<FixNames> fixer;
53 acceptAll( translationUnit, fixer );
54 }
55
56 void FixNames::fixDWT( DeclarationWithType * dwt ) {
57 if ( dwt->get_name() != "" ) {
58 if ( LinkageSpec::isMangled( dwt->get_linkage() ) ) {
59 if (!useNewAST) {
60 dwt->set_mangleName( SymTab::Mangler::mangle( dwt ) );
61 }
62 dwt->set_scopeLevel( scopeLevel );
63 } // if
64 } // if
65 }
66
67 void FixNames::postvisit( ObjectDecl * objectDecl ) {
68 fixDWT( objectDecl );
69 }
70
71 void FixNames::postvisit( FunctionDecl * functionDecl ) {
72 fixDWT( functionDecl );
73
74 if ( FixMain::isMain( functionDecl ) ) {
75 int nargs = functionDecl->get_functionType()->get_parameters().size();
76 if( !(nargs == 0 || nargs == 2 || nargs == 3) ) {
77 SemanticError(functionDecl, "Main expected to have 0, 2 or 3 arguments\n");
78 }
79 functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( new ConstantExpr( Constant::from_int( 0 ) ) ) );
80 }
81 }
82
83 void FixNames::previsit( CompoundStmt * ) {
84 scopeLevel++;
85 GuardAction( [this](){ scopeLevel--; } );
86 }
87
88/// Does work with the main function and scopeLevels.
89class FixNames_new final {
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 }
96public:
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 ) ) {
106 auto mutDecl = ast::mutate( functionDecl );
107
108 if ( shouldSetScopeLevel( mutDecl ) ) {
109 mutDecl->scopeLevel = scopeLevel;
110 }
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 );
122
123 return mutDecl;
124 } else if ( shouldSetScopeLevel( functionDecl ) ) {
125 return ast::mutate_field( functionDecl, &ast::FunctionDecl::scopeLevel, scopeLevel );
126 } else {
127 return functionDecl;
128 }
129 }
130
131 void previsit( const ast::CompoundStmt * ) {
132 scopeLevel += 1;
133 }
134
135 void postvisit( const ast::CompoundStmt * ) {
136 scopeLevel -= 1;
137 }
138};
139
140void fixNames( ast::TranslationUnit & translationUnit ) {
141 ast::Pass<FixNames_new>::run( translationUnit );
142}
143
144} // namespace CodeGen
145
146// Local Variables: //
147// tab-width: 4 //
148// mode: c++ //
149// compile-command: "make install" //
150// End: //
Note: See TracBrowser for help on using the repository browser.