Changeset bff227f


Ignore:
Timestamp:
Jul 21, 2017, 3:57:11 PM (8 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
9191a8e
Parents:
53a8e68
Message:

Refactor operator predicates into OperatorTable?.cc

Location:
src
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/CodeGen/OperatorTable.cc

    r53a8e68 rbff227f  
    1414//
    1515
    16 #include <map>      // for map, _Rb_tree_const_iterator, map<>::const_iterator
    17 #include <utility>  // for pair
     16#include <algorithm>  // for any_of
     17#include <map>        // for map, _Rb_tree_const_iterator, map<>::const_iterator
     18#include <utility>    // for pair
    1819
    1920#include "OperatorTable.h"
     
    9192                } // if
    9293        }
     94
     95        /// determines if a given function name is one of the operator types between [begin, end)
     96        template<typename Iterator>
     97        bool isOperatorType( const std::string & funcName, Iterator begin, Iterator end ) {
     98                OperatorInfo info;
     99                if ( operatorLookup( funcName, info ) ) {
     100                        return std::find( begin, end, info.type ) != end;
     101                }
     102                return false;
     103        }
     104
     105        bool isConstructor( const std::string & funcName ) {
     106                static OperatorType types[] = { OT_CTOR };
     107                return isOperatorType( funcName, std::begin(types), std::end(types) );
     108        }
     109
     110        bool isDestructor( const std::string & funcName ) {
     111                static OperatorType types[] = { OT_DTOR };
     112                return isOperatorType( funcName, std::begin(types), std::end(types) );
     113        }
     114
     115        bool isAssignment( const std::string & funcName ) {
     116                static OperatorType types[] = { OT_PREFIXASSIGN, OT_POSTFIXASSIGN, OT_INFIXASSIGN };
     117                return isOperatorType( funcName, std::begin(types), std::end(types) );
     118        }
     119
     120        bool isCtorDtor( const std::string & funcName ) {
     121                static OperatorType types[] = { OT_CTOR, OT_DTOR };
     122                return isOperatorType( funcName, std::begin(types), std::end(types) );
     123        }
     124
     125        bool isCtorDtorAssign( const std::string & funcName ) {
     126                static OperatorType types[] = { OT_CTOR, OT_DTOR, OT_PREFIXASSIGN, OT_POSTFIXASSIGN, OT_INFIXASSIGN };
     127                return isOperatorType( funcName, std::begin(types), std::end(types) );
     128        }
    93129} // namespace CodeGen
    94130
  • TabularUnified src/CodeGen/OperatorTable.h

    r53a8e68 rbff227f  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // OperatorTable.h -- 
     7// OperatorTable.h --
    88//
    99// Author           : Richard C. Bilson
     
    4343
    4444        bool operatorLookup( std::string funcName, OperatorInfo &info );
     45
     46        bool isConstructor( const std::string & );
     47        bool isDestructor( const std::string & );
     48        bool isAssignment( const std::string & );
     49        bool isCtorDtor( const std::string & );
     50        bool isCtorDtorAssign( const std::string & );
    4551} // namespace CodeGen
    4652
  • TabularUnified src/Concurrency/Keywords.cc

    r53a8e68 rbff227f  
    2222#include "Common/SemanticError.h"  // for SemanticError
    2323#include "Common/utility.h"        // for deleteAll, map_range
    24 #include "InitTweak/InitTweak.h"   // for isConstructor
     24#include "CodeGen/OperatorTable.h" // for isConstructor
     25#include "InitTweak/InitTweak.h"   // for getPointerBase
    2526#include "Parser/LinkageSpec.h"    // for Cforall
    2627#include "SymTab/AddVisit.h"       // for acceptAndAdd
     
    522523                Visitor::visit(decl);
    523524
    524                 if( ! InitTweak::isConstructor(decl->get_name()) ) return;
     525                if( ! CodeGen::isConstructor(decl->get_name()) ) return;
    525526
    526527                DeclarationWithType * param = decl->get_functionType()->get_parameters().front();
  • TabularUnified src/GenPoly/Box.cc

    r53a8e68 rbff227f  
    5555#include "Common/UniqueName.h"
    5656#include "Common/utility.h"
     57
     58#include "CodeGen/OperatorTable.h"
    5759
    5860#include "InitTweak/InitTweak.h"
     
    567569                        // To compound the issue, the right side can be *x, etc. because of lvalue-returning functions
    568570                        if ( UntypedExpr * assign = dynamic_cast< UntypedExpr * >( commaExpr->get_arg1() ) ) {
    569                                 if ( InitTweak::isAssignment( InitTweak::getFunctionName( assign ) ) ) {
     571                                if ( CodeGen::isAssignment( InitTweak::getFunctionName( assign ) ) ) {
    570572                                        assert( assign->get_args().size() == 2 );
    571573                                        if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( assign->get_args().back() ) ) {
  • TabularUnified src/InitTweak/FixInit.cc

    r53a8e68 rbff227f  
    2626#include "FixGlobalInit.h"
    2727#include "CodeGen/GenType.h"  // for warning/error messages
     28#include "CodeGen/OperatorTable.h"
    2829#include "Common/PassVisitor.h"
    2930#include "GenPoly/DeclMutator.h"
     
    363364                                        FunctionType * ftype = dynamic_cast< FunctionType * >( GenPoly::getFunctionType( funcDecl->get_type() ) );
    364365                                        assert( ftype );
    365                                         if ( isConstructor( funcDecl->get_name() ) && ftype->get_parameters().size() == 2 ) {
     366                                        if ( CodeGen::isConstructor( funcDecl->get_name() ) && ftype->get_parameters().size() == 2 ) {
    366367                                                Type * t1 = getPointerBase( ftype->get_parameters().front()->get_type() );
    367368                                                Type * t2 = ftype->get_parameters().back()->get_type();
     
    372373                                                        return appExpr;
    373374                                                } // if
    374                                         } else if ( isDestructor( funcDecl->get_name() ) ) {
     375                                        } else if ( CodeGen::isDestructor( funcDecl->get_name() ) ) {
    375376                                                // correctness: never copy construct arguments to a destructor
    376377                                                return appExpr;
     
    975976                        if ( ! funcDecl ) return false;
    976977                        if ( ! funcDecl->get_statements() ) return false;
    977                         return isCtorDtor( funcDecl->get_name() ) && ! LinkageSpec::isOverridable( funcDecl->get_linkage() );
     978                        return CodeGen::isCtorDtor( funcDecl->get_name() ) && ! LinkageSpec::isOverridable( funcDecl->get_linkage() );
    978979                }
    979980
     
    992993
    993994                        function = funcDecl;
    994                         isCtor = isConstructor( function->get_name() );
     995                        isCtor = CodeGen::isConstructor( function->get_name() );
    995996                        if ( checkWarnings( function ) ) {
    996997                                FunctionType * type = function->get_functionType();
  • TabularUnified src/InitTweak/GenInit.cc

    r53a8e68 rbff227f  
    2121
    2222#include "Common/PassVisitor.h"
     23#include "CodeGen/OperatorTable.h"
    2324
    2425#include "GenPoly/DeclMutator.h"
     
    235236        void CtorDtor::handleDWT( DeclarationWithType * dwt ) {
    236237                // if this function is a user-defined constructor or destructor, mark down the type as "managed"
    237                 if ( ! LinkageSpec::isOverridable( dwt->get_linkage() ) && isCtorDtor( dwt->get_name() ) ) {
     238                if ( ! LinkageSpec::isOverridable( dwt->get_linkage() ) && CodeGen::isCtorDtor( dwt->get_name() ) ) {
    238239                        std::list< DeclarationWithType * > & params = GenPoly::getFunctionType( dwt->get_type() )->get_parameters();
    239240                        assert( ! params.empty() );
  • TabularUnified src/InitTweak/InitTweak.h

    r53a8e68 rbff227f  
    2626// helper functions for initialization
    2727namespace InitTweak {
    28         bool isConstructor( const std::string & );
    29         bool isDestructor( const std::string & );
    30         bool isAssignment( const std::string & );
    31         bool isCtorDtor( const std::string & );
    32         bool isCtorDtorAssign( const std::string & );
    33 
    3428        FunctionDecl * isAssignment( Declaration * decl );
    3529        FunctionDecl * isDestructor( Declaration * decl );
  • TabularUnified src/SymTab/Autogen.cc

    r53a8e68 rbff227f  
    2121#include "SynTree/TypeSubstitution.h"
    2222#include "Common/utility.h"
     23#include "CodeGen/OperatorTable.h"
    2324#include "AddVisit.h"
    2425#include "MakeLibCfa.h"
     
    223224                        FunctionType * ftype = data.genType( refType );
    224225
    225                         if(concurrent_type && InitTweak::isDestructor( data.fname )) {
     226                        if(concurrent_type && CodeGen::isDestructor( data.fname )) {
    226227                                ftype->get_parameters().front()->get_type()->set_mutex( true );
    227228                        }
     
    407408
    408409                // field ctors are only generated if default constructor and copy constructor are both generated
    409                 unsigned numCtors = std::count_if( newFuncs.begin(), newFuncs.end(), [](FunctionDecl * dcl) { return InitTweak::isConstructor( dcl->get_name() ); } );
     410                unsigned numCtors = std::count_if( newFuncs.begin(), newFuncs.end(), [](FunctionDecl * dcl) { return CodeGen::isConstructor( dcl->get_name() ); } );
    410411
    411412                if ( functionNesting == 0 ) {
     
    422423                        // generate appropriate calls to member ctor, assignment
    423424                        // destructor needs to do everything in reverse, so pass "forward" based on whether the function is a destructor
    424                         if ( ! InitTweak::isDestructor( dcl->get_name() ) ) {
     425                        if ( ! CodeGen::isDestructor( dcl->get_name() ) ) {
    425426                                makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), dcl, isDynamicLayout );
    426427                        } else {
    427428                                makeStructFunctionBody( aggregateDecl->get_members().rbegin(), aggregateDecl->get_members().rend(), dcl, isDynamicLayout, false );
    428429                        }
    429                         if ( InitTweak::isAssignment( dcl->get_name() ) ) {
     430                        if ( CodeGen::isAssignment( dcl->get_name() ) ) {
    430431                                // assignment needs to return a value
    431432                                FunctionType * assignType = dcl->get_functionType();
     
    486487
    487488                makeUnionFieldsAssignment( srcParam, dstParam, back_inserter( funcDecl->get_statements()->get_kids() ) );
    488                 if ( InitTweak::isAssignment( funcDecl->get_name() ) ) {
     489                if ( CodeGen::isAssignment( funcDecl->get_name() ) ) {
    489490                        // also generate return statement in assignment
    490491                        funcDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
  • TabularUnified src/SymTab/Indexer.cc

    r53a8e68 rbff227f  
    2626
    2727#include "Common/utility.h"
     28
     29#include "CodeGen/OperatorTable.h"
    2830
    2931#include "ResolvExpr/typeops.h"
     
    112114        void Indexer::removeSpecialOverrides( const std::string &id, std::list< DeclarationWithType * > & out ) const {
    113115                // only need to perform this step for constructors, destructors, and assignment functions
    114                 if ( ! InitTweak::isCtorDtorAssign( id ) ) return;
     116                if ( ! CodeGen::isCtorDtorAssign( id ) ) return;
    115117
    116118                // helpful data structure
     
    140142                                decls.push_back( DeclBall{ function, isUserDefinedFunc, isDefaultCtor, isDtor, isCopyFunc } );
    141143                                existsUserDefinedFunc = existsUserDefinedFunc || isUserDefinedFunc;
    142                                 existsUserDefinedCtor = existsUserDefinedCtor || (isUserDefinedFunc && InitTweak::isConstructor( function->get_name() ) );
     144                                existsUserDefinedCtor = existsUserDefinedCtor || (isUserDefinedFunc && CodeGen::isConstructor( function->get_name() ) );
    143145                                existsUserDefinedDtor = existsUserDefinedDtor || (isUserDefinedFunc && isDtor);
    144146                                existsUserDefinedCopyFunc = existsUserDefinedCopyFunc || (isUserDefinedFunc && isCopyFunc);
  • TabularUnified src/SymTab/Validate.cc

    r53a8e68 rbff227f  
    4343
    4444#include "CodeGen/CodeGenerator.h"
     45#include "CodeGen/OperatorTable.h"
    4546
    4647#include "Common/PassVisitor.h"
     
    5253
    5354#include "GenPoly/DeclMutator.h"
    54 
    55 #include "InitTweak/InitTweak.h"
    5655
    5756#include "AddVisit.h"
     
    827826                std::list< DeclarationWithType * > &params = funcType->get_parameters();
    828827
    829                 if ( InitTweak::isCtorDtorAssign( funcDecl->get_name() ) ) {
     828                if ( CodeGen::isCtorDtorAssign( funcDecl->get_name() ) ) { // TODO: also check /=, etc.
    830829                        if ( params.size() == 0 ) {
    831830                                throw SemanticError( "Constructors, destructors, and assignment functions require at least one parameter ", funcDecl );
     
    835834                                throw SemanticError( "First parameter of a constructor, destructor, or assignment function must be a reference ", funcDecl );
    836835                        }
    837                         if ( InitTweak::isCtorDtor( funcDecl->get_name() ) && returnVals.size() != 0 ) {
     836                        if ( CodeGen::isCtorDtor( funcDecl->get_name() ) && returnVals.size() != 0 ) {
    838837                                throw SemanticError( "Constructors and destructors cannot have explicit return values ", funcDecl );
    839838                        }
  • TabularUnified src/Tuples/TupleAssignment.cc

    r53a8e68 rbff227f  
    2222#include "Explode.h"
    2323#include "Common/SemanticError.h"
     24#include "CodeGen/OperatorTable.h"
    2425#include "InitTweak/InitTweak.h"
    2526#include "InitTweak/GenInit.h"
     
    110111        void TupleAssignSpotter::spot( UntypedExpr * expr, const std::list<ResolvExpr::AltList> &possibilities ) {
    111112                if (  NameExpr *op = dynamic_cast< NameExpr * >(expr->get_function()) ) {
    112                         if ( InitTweak::isCtorDtorAssign( op->get_name() ) ) {
     113                        if ( CodeGen::isCtorDtorAssign( op->get_name() ) ) {
    113114                                fname = op->get_name();
    114115                                for ( std::list<ResolvExpr::AltList>::const_iterator ali = possibilities.begin(); ali != possibilities.end(); ++ali ) {
    115116                                        if ( ali->size() == 0 ) continue; // AlternativeFinder will natrually handle this case, if it's legal
    116                                         if ( ali->size() <= 1 && InitTweak::isAssignment( op->get_name() ) ) {
     117                                        if ( ali->size() <= 1 && CodeGen::isAssignment( op->get_name() ) ) {
    117118                                                // what does it mean if an assignment takes 1 argument? maybe someone defined such a function, in which case AlternativeFinder will naturally handle it
    118119                                                continue;
Note: See TracChangeset for help on using the changeset viewer.