Changeset d908563 for src/Common


Ignore:
Timestamp:
May 27, 2019, 11:08:54 AM (6 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
f88a252
Parents:
933f32f (diff), 21a44ca (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into cleanup-dtors

Location:
src/Common
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/Common/Eval.cc

    r933f32f rd908563  
    1717
    1818#include "Common/PassVisitor.h"
     19#include "AST/Pass.hpp"
    1920#include "InitTweak/InitTweak.h"
    2021#include "SynTree/Expression.h"
    2122
    22 struct Eval : public WithShortCircuiting {
     23//-------------------------------------------------------------
     24// Old AST
     25struct EvalOld : public WithShortCircuiting {
    2326        long long int value = 0;
    2427        bool valid = true;
     
    8083};
    8184
     85//-------------------------------------------------------------
     86// New AST
     87struct EvalNew : public ast::WithShortCircuiting {
     88        long long int value = 0;
     89        bool valid = true;
     90
     91        void previsit( const ast::Node * ) { visit_children = false; }
     92        void postvisit( const ast::Node * ) { valid = false; }
     93
     94        void postvisit( const ast::ConstantExpr * expr ) {
     95                value = expr->intValue();
     96        }
     97
     98        void postvisit( const ast::CastExpr * expr ) {
     99                auto arg = eval(expr->arg);
     100                valid = arg.second;
     101                value = arg.first;
     102                // TODO: perform type conversion on value if valid
     103        }
     104
     105        void postvisit( const ast::VariableExpr * expr ) {
     106                if ( const ast::EnumInstType * inst = dynamic_cast<const ast::EnumInstType *>(expr->result.get()) ) {
     107                        if ( const ast::EnumDecl * decl = inst->base ) {
     108                                if ( decl->valueOf( expr->var, value ) ) { // value filled by valueOf
     109                                        return;
     110                                }
     111                        }
     112                }
     113                valid = false;
     114        }
     115
     116        void postvisit( const ast::ApplicationExpr * expr ) {
     117                const ast::DeclWithType * function = InitTweak::getFunction(expr);
     118                if ( ! function || function->linkage != ast::Linkage::Intrinsic ) { valid = false; return; }
     119                const std::string & fname = function->name;
     120                assertf( expr->args.size() == 1 || expr->args.size() == 2, "Intrinsic function with %zd arguments: %s", expr->args.size(), fname.c_str() );
     121                std::pair<long long int, bool> arg1, arg2;
     122                arg1 = eval(expr->args.front());
     123                valid = valid && arg1.second;
     124                if ( ! valid ) return;
     125                if ( expr->args.size() == 2 ) {
     126                        arg2 = eval(expr->args.back());
     127                        valid = valid && arg2.second;
     128                        if ( ! valid ) return;
     129                }
     130                if (fname == "?+?") {
     131                        value = arg1.first + arg2.first;
     132                } else if (fname == "?-?") {
     133                        value = arg1.first - arg2.first;
     134                } else if (fname == "?*?") {
     135                        value = arg1.first * arg2.first;
     136                } else if (fname == "?/?") {
     137                        value = arg1.first / arg2.first;
     138                } else if (fname == "?%?") {
     139                        value = arg1.first % arg2.first;
     140                } else {
     141                        valid = false;
     142                }
     143                // TODO: implement other intrinsic functions
     144        }
     145};
     146
    82147std::pair<long long int, bool> eval(Expression * expr) {
    83         PassVisitor<Eval> ev;
     148        PassVisitor<EvalOld> ev;
     149        if (expr) {
     150                expr->accept(ev);
     151                return std::make_pair(ev.pass.value, ev.pass.valid);
     152        } else {
     153                return std::make_pair(0, false);
     154        }
     155}
     156
     157std::pair<long long int, bool> eval(const ast::Expr * expr) {
     158        ast::Pass<EvalNew> ev;
    84159        if (expr) {
    85160                expr->accept(ev);
  • src/Common/PassVisitor.impl.h

    r933f32f rd908563  
    2323        assert( __return ); \
    2424        return __return;
    25 
    26 
    27 #define VISIT_BODY( node )          \
    28         VISIT_START( node );          \
    29         if( children_guard ) {        \
    30                 Visitor::visit( node ); \
    31         }                             \
    32         VISIT_END( node );            \
    33 
    34 
    35 #define MUTATE_BODY( type, node )    \
    36         MUTATE_START( node );          \
    37         if( children_guard ) {         \
    38                 Mutator::mutate( node ); \
    39         }                              \
    40         MUTATE_END( type, node );      \
    41 
    4225
    4326
     
    27562739        MUTATE_END( TypeSubstitution, node );
    27572740}
     2741
     2742#undef VISIT_START
     2743#undef VISIT_END
     2744
     2745#undef MUTATE_START
     2746#undef MUTATE_END
  • src/Common/utility.h

    r933f32f rd908563  
    7474
    7575template< typename Container >
    76 void deleteAll( Container &container ) {
    77         for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) {
    78                 delete *i;
     76void deleteAll( const Container &container ) {
     77        for ( const auto &i : container ) {
     78                delete i;
    7979        } // for
    8080}
Note: See TracChangeset for help on using the changeset viewer.