source: src/CodeGen/FixNames.cc @ c6b4432

Last change on this file since c6b4432 was c6b4432, checked in by Andrew Beach <ajbeach@…>, 7 months ago

Remove BaseSyntaxNode? and clean-up.

  • 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/// Does work with the main function and scopeLevels.
31class FixNames_new final {
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        }
38public:
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 ) ) {
48                        auto mutDecl = ast::mutate( functionDecl );
49
50                        if ( shouldSetScopeLevel( mutDecl ) ) {
51                                mutDecl->scopeLevel = scopeLevel;
52                        }
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                        );
64
65                        return mutDecl;
66                } else if ( shouldSetScopeLevel( functionDecl ) ) {
67                        return ast::mutate_field( functionDecl, &ast::FunctionDecl::scopeLevel, scopeLevel );
68                } else {
69                        return functionDecl;
70                }
71        }
72
73        void previsit( const ast::CompoundStmt * ) {
74                scopeLevel += 1;
75        }
76
77        void postvisit( const ast::CompoundStmt * ) {
78                scopeLevel -= 1;
79        }
80};
81
82void fixNames( ast::TranslationUnit & translationUnit ) {
83        ast::Pass<FixNames_new>::run( translationUnit );
84}
85
86} // namespace CodeGen
87
88// Local Variables: //
89// tab-width: 4 //
90// mode: c++ //
91// compile-command: "make install" //
92// End: //
Note: See TracBrowser for help on using the repository browser.