Changeset c6b4432 for src/Common


Ignore:
Timestamp:
Nov 8, 2023, 2:01:11 PM (7 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
master
Children:
3e4bf0d, f5ec35a
Parents:
790d835
Message:

Remove BaseSyntaxNode? and clean-up.

Location:
src/Common
Files:
4 deleted
5 edited

Legend:

Unmodified
Added
Removed
  • src/Common/Eval.cc

    r790d835 rc6b4432  
    1919
    2020#include "AST/Inspect.hpp"
    21 #include "Common/PassVisitor.h"
    2221#include "CodeGen/OperatorTable.h"                                              // access: OperatorInfo
    2322#include "AST/Pass.hpp"
    2423#include "InitTweak/InitTweak.h"
    25 #include "SynTree/Expression.h"
    26 
    27 //-------------------------------------------------------------
    28 // Old AST
    29 struct EvalOld : public WithShortCircuiting {
    30         long long int value = 0;                                                        // compose the result of the constant expression
    31         bool valid = true;                                                                      // true => constant expression and value is the result
    32                                                                                                                 // false => not constant expression, e.g., ++i
    33         bool cfavalid = true;                                                           // true => constant expression and value computable
    34                                                                                                                 // false => constant expression but value not computable, e.g., sizeof(int)
    35 
    36         void previsit( const BaseSyntaxNode * ) { visit_children = false; }
    37         void postvisit( const BaseSyntaxNode * ) { valid = false; }
    38 
    39         void postvisit( const SizeofExpr * ) {
    40         }
    41 
    42         void postvisit( const ConstantExpr * expr ) {
    43                 value = expr->intValue();
    44         }
    45 
    46         void postvisit( const CastExpr * expr ) {
    47                 auto arg = eval(expr->arg);
    48                 valid = arg.second;
    49                 value = arg.first;
    50                 // TODO: perform type conversion on value if valid
    51         }
    52 
    53         void postvisit( const VariableExpr * const expr ) {
    54                 if ( EnumInstType * inst = dynamic_cast<EnumInstType *>(expr->result) ) {
    55                         if ( EnumDecl * decl = inst->baseEnum ) {
    56                                 if ( decl->valueOf( expr->var, value ) ) { // value filled by valueOf
    57                                         return;
    58                                 }
    59                         }
    60                 }
    61                 valid = false;
    62         }
    63 
    64         void postvisit( const ApplicationExpr * expr ) {
    65                 DeclarationWithType * function = InitTweak::getFunction(const_cast<ApplicationExpr *>(expr));
    66                 if ( ! function || function->linkage != LinkageSpec::Intrinsic ) { valid = false; return; }
    67                 const std::string & fname = function->name;
    68                 assertf( expr->args.size() == 1 || expr->args.size() == 2, "Intrinsic function with %zd arguments: %s", expr->args.size(), fname.c_str() );
    69                 std::pair<long long int, bool> arg1, arg2;
    70                 arg1 = eval(expr->args.front());
    71                 valid = valid && arg1.second;
    72                 if ( ! valid ) return;
    73                 if ( expr->args.size() == 2 ) {
    74                         arg2 = eval(expr->args.back());
    75                         valid = valid && arg2.second;
    76                         if ( ! valid ) return;
    77                 }
    78                 if (fname == "?+?") {
    79                         value = arg1.first + arg2.first;
    80                 } else if (fname == "?-?") {
    81                         value = arg1.first - arg2.first;
    82                 } else if (fname == "?*?") {
    83                         value = arg1.first * arg2.first;
    84                 } else if (fname == "?/?") {
    85                         value = arg1.first / arg2.first;
    86                 } else if (fname == "?%?") {
    87                         value = arg1.first % arg2.first;
    88                 } else {
    89                         valid = false;
    90                 }
    91                 // TODO: implement other intrinsic functions
    92         }
    93 };
    94 
    95 //-------------------------------------------------------------
    96 // New AST
     24
    9725struct EvalNew : public ast::WithShortCircuiting {
    9826        Evaluation result = { 0, true, true };
     
    270198};
    271199
    272 std::pair<long long int, bool> eval( const Expression * expr ) {
    273         PassVisitor<EvalOld> ev;
    274         if ( expr ) {
    275                 expr->accept( ev );
    276                 return std::make_pair( ev.pass.value, ev.pass.valid );
    277         } else {
    278                 return std::make_pair( 0, false );
    279         }
    280 }
    281 
    282200Evaluation eval( const ast::Expr * expr ) {
    283201        if ( expr ) {
  • src/Common/Eval.h

    r790d835 rc6b4432  
    3030
    3131/// Evaluates expr as a long long int.
    32 /// If second is false, expr could not be evaluated.
    33 std::pair<long long int, bool> eval(const Expression * expr);
    3432Evaluation eval(const ast::Expr * expr);
    3533
  • src/Common/Examine.cc

    r790d835 rc6b4432  
    1919#include "CodeGen/OperatorTable.h"
    2020#include "InitTweak/InitTweak.h"
    21 
    22 DeclarationWithType * isMainFor( FunctionDecl * func, AggregateDecl::Aggregate kind ) {
    23         if (func->name != "main") return nullptr;
    24         if (func->type->parameters.size() != 1) return nullptr;
    25 
    26         auto param = func->type->parameters.front();
    27 
    28         auto type = dynamic_cast<ReferenceType * >(param->get_type());
    29         if (!type) return nullptr;
    30 
    31         auto obj = dynamic_cast<StructInstType *>(type->base);
    32         if (!obj) return nullptr;
    33 
    34         if (kind != obj->baseStruct->kind) return nullptr;
    35 
    36         return param;
    37 }
    3821
    3922namespace {
     
    6952
    7053namespace {
    71         Type * getDestructorParam( FunctionDecl * func ) {
    72                 if ( !CodeGen::isDestructor( func->name ) ) return nullptr;
    73 
    74                 auto params = func->type->parameters;
    75                 if ( 1 != params.size() ) return nullptr;
    76 
    77                 auto ref = dynamic_cast<ReferenceType *>( params.front()->get_type() );
    78                 if ( ref ) {
    79                         return ref->base;
    80                 }
    81                 return nullptr;
    82         }
    8354
    8455const ast::Type * getDestructorParam( const ast::FunctionDecl * func ) {
     
    8859}
    8960
    90 }
    91 
    92 bool isDestructorFor( FunctionDecl * func, StructDecl * type_decl ) {
    93         if ( Type * type = getDestructorParam( func ) ) {
    94                 auto stype = dynamic_cast<StructInstType *>( type );
    95                 return stype && stype->baseStruct == type_decl;
    96         }
    97         return false;
    9861}
    9962
  • src/Common/Examine.h

    r790d835 rc6b4432  
    1515
    1616#include "AST/Decl.hpp"
    17 #include "SynTree/Declaration.h"
    1817
    1918/// Check if this is a main function for a type of an aggregate kind.
    20 DeclarationWithType * isMainFor( FunctionDecl * func, AggregateDecl::Aggregate kind );
    2119const ast::DeclWithType * isMainFor(
    2220        const ast::FunctionDecl * func, ast::AggregateDecl::Aggregate kind );
     
    2422
    2523/// Check if this function is a destructor for the given structure.
    26 bool isDestructorFor( FunctionDecl * func, StructDecl * type_decl );
    2724bool isDestructorFor(
    2825        const ast::FunctionDecl * func, const ast::StructDecl * type );
  • src/Common/module.mk

    r790d835 rc6b4432  
    3131        Common/Indenter.cc \
    3232        Common/Iterate.hpp \
    33         Common/PassVisitor.cc \
    34         Common/PassVisitor.h \
    35         Common/PassVisitor.impl.h \
    36         Common/PassVisitor.proto.h \
    3733        Common/PersistentMap.h \
    3834        Common/ResolvProtoDump.hpp \
Note: See TracChangeset for help on using the changeset viewer.