Changeset 61efa42 for src/CodeGen


Ignore:
Timestamp:
Nov 10, 2023, 5:04:30 PM (23 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
master
Children:
2174191
Parents:
f5ec35a
Message:

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.

Location:
src/CodeGen
Files:
1 deleted
5 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/FixMain.cc

    rf5ec35a r61efa42  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // FixMain.cc --
     7// FixMain.cc -- Tools to change a Cforall main into a C main.
    88//
    99// Author           : Thierry Delisle
     
    3333namespace {
    3434
    35 struct FindMainCore_new {
     35struct FindMainCore final {
    3636        ast::FunctionDecl const * main_declaration = nullptr;
    3737
    3838        void previsit( ast::FunctionDecl const * decl ) {
    39                 if ( FixMain::isMain( decl ) ) {
     39                if ( isMain( decl ) ) {
    4040                        if ( main_declaration ) {
    4141                                SemanticError( decl, "Multiple definition of main routine\n" );
     
    106106}
    107107
     108struct FixLinkageCore final {
     109        ast::Linkage::Spec const spec;
     110        FixLinkageCore( ast::Linkage::Spec spec ) : spec( spec ) {}
     111
     112        ast::FunctionDecl const * previsit( ast::FunctionDecl const * decl ) {
     113                if ( decl->name != "main" ) return decl;
     114                return ast::mutate_field( decl, &ast::FunctionDecl::linkage, spec );
     115        }
     116};
     117
    108118} // namespace
    109119
    110 bool FixMain::isMain( const ast::FunctionDecl * decl ) {
     120bool isMain( const ast::FunctionDecl * decl ) {
    111121        if ( std::string("main") != decl->name ) {
    112122                return false;
     
    115125}
    116126
    117 void FixMain::fix( ast::TranslationUnit & translationUnit,
     127void fixMainLinkage( ast::TranslationUnit & translationUnit,
     128                bool replace_main ) {
     129        ast::Linkage::Spec const spec =
     130                ( replace_main ) ? ast::Linkage::Cforall : ast::Linkage::C;
     131        ast::Pass<FixLinkageCore>::run( translationUnit, spec );
     132}
     133
     134void fixMainInvoke( ast::TranslationUnit & translationUnit,
    118135                std::ostream &os, const char * bootloader_filename ) {
    119136
    120         ast::Pass<FindMainCore_new> main_finder;
     137        ast::Pass<FindMainCore> main_finder;
    121138        ast::accept_all( translationUnit, main_finder );
    122139        if ( nullptr == main_finder.core.main_declaration ) return;
  • src/CodeGen/FixMain.h

    rf5ec35a r61efa42  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // FixMain.h --
     7// FixMain.h -- Tools to change a Cforall main into a C main.
    88//
    99// Author           : Thierry Delisle
     
    1717
    1818#include <iosfwd>
    19 #include <memory>
    20 #include <list>
    21 
    22 #include "AST/LinkageSpec.hpp"
    2319
    2420namespace ast {
     
    2925namespace CodeGen {
    3026
    31 class FixMain {
    32 public :
    33         static inline ast::Linkage::Spec getMainLinkage() {
    34                 return replace_main ? ast::Linkage::Cforall : ast::Linkage::C;
    35         }
     27/// Is this function a program main function?
     28bool isMain( const ast::FunctionDecl * decl );
    3629
    37         static inline void setReplaceMain(bool val) {
    38                 replace_main = val;
    39         }
     30/// Adjust the linkage of main functions.
     31void fixMainLinkage( ast::TranslationUnit & transUnit, bool replaceMain );
    4032
    41         static bool isMain(const ast::FunctionDecl * decl);
    42 
    43         static void fix( ast::TranslationUnit & translationUnit,
    44                         std::ostream &os, const char * bootloader_filename );
    45 
    46 private:
    47         static bool replace_main;
    48 };
     33/// Add a wrapper around to run the Cforall main.
     34void fixMainInvoke( ast::TranslationUnit & transUnit,
     35                std::ostream & os, const char * bootloaderFilename );
    4936
    5037} // namespace CodeGen
  • src/CodeGen/FixNames.cc

    rf5ec35a r61efa42  
    2828
    2929namespace CodeGen {
     30
     31namespace {
     32
    3033/// Does work with the main function and scopeLevels.
    31 class FixNames_new final {
     34class FixNames final {
    3235        int scopeLevel = 1;
    3336
     
    4548
    4649        const ast::FunctionDecl *postvisit( const ast::FunctionDecl *functionDecl ) {
    47                 if ( FixMain::isMain( functionDecl ) ) {
     50                if ( isMain( functionDecl ) ) {
    4851                        auto mutDecl = ast::mutate( functionDecl );
    4952
     
    8083};
    8184
     85} // namespace
     86
    8287void fixNames( ast::TranslationUnit & translationUnit ) {
    83         ast::Pass<FixNames_new>::run( translationUnit );
     88        ast::Pass<FixNames>::run( translationUnit );
    8489}
    8590
  • src/CodeGen/FixNames.h

    rf5ec35a r61efa42  
    1616#pragma once
    1717
    18 #include <list>  // for list
    19 
    20 class Declaration;
    2118namespace ast {
    2219        class TranslationUnit;
     
    2421
    2522namespace CodeGen {
    26         /// mangles object and function names
    27         void fixNames( std::list< Declaration* > & translationUnit );
     23
    2824/// Sets scope levels and fills in main's default return.
    2925void fixNames( ast::TranslationUnit & translationUnit );
     26
    3027} // namespace CodeGen
    3128
  • src/CodeGen/module.mk

    rf5ec35a r61efa42  
    1818        CodeGen/CodeGeneratorNew.cpp \
    1919        CodeGen/CodeGeneratorNew.hpp \
    20         CodeGen/FixMain2.cc \
    21         CodeGen/FixMain.h \
    2220        CodeGen/GenType.cc \
    2321        CodeGen/GenType.h \
     
    2927        CodeGen/Generate.h \
    3028        CodeGen/FixMain.cc \
     29        CodeGen/FixMain.h \
    3130        CodeGen/FixNames.cc \
    3231        CodeGen/FixNames.h \
Note: See TracChangeset for help on using the changeset viewer.