source: src/CodeGen/FixNames.cc@ 0bd3faf

Last change on this file since 0bd3faf was 61efa42, checked in by Andrew Beach <ajbeach@…>, 23 months ago

Took the special main linkage code out of FunctionDecl and put it into a new pass. There is also a lot of related clean-up done.

  • Property mode set to 100644
File size: 2.5 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/SemanticError.h" // for SemanticError
25#include "FixMain.h" // for FixMain
26#include "SymTab/Mangler.h" // for Mangler
27#include "CompilationState.h"
28
29namespace CodeGen {
30
31namespace {
32
33/// Does work with the main function and scopeLevels.
34class FixNames final {
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 }
41public:
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 ) {
50 if ( isMain( functionDecl ) ) {
51 auto mutDecl = ast::mutate( functionDecl );
52
53 if ( shouldSetScopeLevel( mutDecl ) ) {
54 mutDecl->scopeLevel = scopeLevel;
55 }
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 );
67
68 return mutDecl;
69 } else if ( shouldSetScopeLevel( functionDecl ) ) {
70 return ast::mutate_field( functionDecl, &ast::FunctionDecl::scopeLevel, scopeLevel );
71 } else {
72 return functionDecl;
73 }
74 }
75
76 void previsit( const ast::CompoundStmt * ) {
77 scopeLevel += 1;
78 }
79
80 void postvisit( const ast::CompoundStmt * ) {
81 scopeLevel -= 1;
82 }
83};
84
85} // namespace
86
87void fixNames( ast::TranslationUnit & translationUnit ) {
88 ast::Pass<FixNames>::run( translationUnit );
89}
90
91} // namespace CodeGen
92
93// Local Variables: //
94// tab-width: 4 //
95// mode: c++ //
96// compile-command: "make install" //
97// End: //
Note: See TracBrowser for help on using the repository browser.