Changeset a172972


Ignore:
Timestamp:
Feb 23, 2016, 11:20:39 AM (9 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
ae42f2a
Parents:
7528ba1 (diff), 6ce67ce (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 ctor

Conflicts:

src/ResolvExpr/Resolver.cc

Location:
src
Files:
2 added
25 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    r7528ba1 ra172972  
    7373                        /// Makes a new temporary array holding the offsets of the fields of `type`, and returns a new variable expression referencing it
    7474                        Expression *makeOffsetArray( StructInstType *type );
     75                        /// Pass the extra type parameters from polymorphic generic arguments or return types into a function application
     76                        void passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes );
    7577                        /// passes extra type parameters into a polymorphic function application
    76                         void passTypeVars( ApplicationExpr *appExpr, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars );
     78                        void passTypeVars( ApplicationExpr *appExpr, ReferenceToType *polyRetType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars );
    7779                        /// wraps a function application with a new temporary for the out-parameter return value
    7880                        Expression *addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg );
     
    399401                }
    400402
    401                 void Pass1::passTypeVars( ApplicationExpr *appExpr, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) {
     403                void Pass1::passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes ) {
     404                        Type *polyBase = hasPolyBase( parmType, exprTyVars );
     405                        if ( polyBase && ! dynamic_cast< TypeInstType* >( polyBase ) ) {
     406                                std::string sizeName = sizeofName( polyBase );
     407                                if ( seenTypes.count( sizeName ) ) return;
     408
     409                                arg = appExpr->get_args().insert( arg, new SizeofExpr( argBaseType->clone() ) );
     410                                arg++;
     411                                arg = appExpr->get_args().insert( arg, new AlignofExpr( argBaseType->clone() ) );
     412                                arg++;
     413                                if ( dynamic_cast< StructInstType* >( polyBase ) ) {
     414                                        if ( StructInstType *argBaseStructType = dynamic_cast< StructInstType* >( argBaseType ) ) {
     415                                                arg = appExpr->get_args().insert( arg, makeOffsetArray( argBaseStructType ) );
     416                                                arg++;
     417                                        } else {
     418                                                throw SemanticError( "Cannot pass non-struct type for generic struct" );
     419                                        }
     420                                }
     421
     422                                seenTypes.insert( sizeName );
     423                        }
     424                }
     425
     426                void Pass1::passTypeVars( ApplicationExpr *appExpr, ReferenceToType *polyRetType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) {
    402427                        // pass size/align for type variables
    403428                        for ( TyVarMap::const_iterator tyParm = exprTyVars.begin(); tyParm != exprTyVars.end(); ++tyParm ) {
     
    425450                        std::list< Expression* >::const_iterator fnArg = arg;
    426451                        std::set< std::string > seenTypes; //< names for generic types we've seen
     452
     453                        // a polymorphic return type may need to be added to the argument list
     454                        if ( polyRetType ) {
     455                                Type *concRetType = replaceWithConcrete( appExpr, polyRetType );
     456                                passArgTypeVars( appExpr, polyRetType, concRetType, arg, exprTyVars, seenTypes );
     457                        }
     458                       
     459                        // add type information args for presently unseen types in parameter list
    427460                        for ( ; fnParm != funcType->get_parameters().end() && fnArg != appExpr->get_args().end(); ++fnParm, ++fnArg ) {
    428                                 Type *polyBase = hasPolyBase( (*fnParm)->get_type(), exprTyVars );
    429                                 if ( polyBase && ! dynamic_cast< TypeInstType* >( polyBase ) ) {
    430                                         std::string sizeName = sizeofName( polyBase );
    431                                         if ( seenTypes.count( sizeName ) ) continue;
    432 
    433                                         VariableExpr *fnArgBase = getBaseVar( *fnArg );
    434                                         assert( fnArgBase && ! fnArgBase->get_results().empty() );
    435                                         Type *argBaseType = fnArgBase->get_results().front();
    436                                         arg = appExpr->get_args().insert( arg, new SizeofExpr( argBaseType->clone() ) );
    437                                         arg++;
    438                                         arg = appExpr->get_args().insert( arg, new AlignofExpr( argBaseType->clone() ) );
    439                                         arg++;
    440                                         if ( dynamic_cast< StructInstType* >( polyBase ) ) {
    441                                                 if ( StructInstType *argBaseStructType = dynamic_cast< StructInstType* >( argBaseType ) ) {
    442                                                         arg = appExpr->get_args().insert( arg, makeOffsetArray( argBaseStructType ) );
    443                                                         arg++;
    444                                                 } else {
    445                                                         throw SemanticError( "Cannot pass non-struct type for generic struct" );
    446                                                 }
    447                                         }
    448 
    449                                         seenTypes.insert( sizeName );
    450                                 }
     461                                VariableExpr *fnArgBase = getBaseVar( *fnArg );
     462                                if ( ! fnArgBase || fnArgBase->get_results().empty() ) continue;
     463                                passArgTypeVars( appExpr, (*fnParm)->get_type(), fnArgBase->get_results().front(), arg, exprTyVars, seenTypes );
    451464                        }
    452465                }
     
    471484                        ObjectDecl *newObj = makeTemporary( retType->clone() );
    472485                        Expression *paramExpr = new VariableExpr( newObj );
    473                         // If the type of the temporary is not polymorphic, box temporary by taking its address; otherwise the
    474                         // temporary is already boxed and can be used directly.
     486
     487                        // If the type of the temporary is not polymorphic, box temporary by taking its address;
     488                        // otherwise the temporary is already boxed and can be used directly.
    475489                        if ( ! isPolyType( newObj->get_type(), scopeTyVars, env ) ) {
    476490                                paramExpr = new AddressExpr( paramExpr );
     
    521535                        assert( env );
    522536                        Type *concrete = replaceWithConcrete( appExpr, polyType );
     537                        // add out-parameter for return value   
    523538                        return addRetParam( appExpr, function, concrete, arg );
    524539                }
     
    543558                        assert( ! arg->get_results().empty() );
    544559                        if ( isPolyType( param, exprTyVars ) ) {
    545                                 if ( dynamic_cast< TypeInstType *>( arg->get_results().front() ) ) {
    546                                         // if the argument's type is a type parameter, we don't need to box again!
     560                                if ( isPolyType( arg->get_results().front() ) ) {
     561                                        // if the argument's type is polymorphic, we don't need to box again!
    547562                                        return;
    548563                                } else if ( arg->get_results().front()->get_isLvalue() ) {
     
    623638                        assert( arg );
    624639                        if ( isPolyType( realParam->get_type(), tyVars ) ) {
    625                                 if ( dynamic_cast<TypeInstType *>(arg->get_type()) == NULL ) {
     640                                if ( ! isPolyType( arg->get_type() ) ) {
    626641                                        UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
    627642                                        deref->get_args().push_back( new CastExpr( new VariableExpr( param ), new PointerType( Type::Qualifiers(), arg->get_type()->clone() ) ) );
     
    918933                        std::list< Expression *>::iterator paramBegin = appExpr->get_args().begin();
    919934
    920                         if ( ReferenceToType *polyType = isPolyRet( function ) ) {
    921                                 ret = addPolyRetParam( appExpr, function, polyType, arg );
     935                        TyVarMap exprTyVars;
     936                        makeTyVarMap( function, exprTyVars );
     937                        ReferenceToType *polyRetType = 0;
     938
     939                        if ( polyRetType = isPolyRet( function ) ) {
     940                                ret = addPolyRetParam( appExpr, function, polyRetType, arg );
    922941                        } else if ( needsAdapter( function, scopeTyVars ) ) {
    923942                                // std::cerr << "needs adapter: ";
     
    931950                        arg = appExpr->get_args().begin();
    932951
    933                         TyVarMap exprTyVars;
    934                         makeTyVarMap( function, exprTyVars );
    935 
    936                         passTypeVars( appExpr, arg, exprTyVars );
     952                        passTypeVars( appExpr, polyRetType, arg, exprTyVars );
    937953                        addInferredParams( appExpr, function, arg, exprTyVars );
    938954
     
    9931009                }
    9941010
     1011                /// Wraps a function declaration in a new pointer-to-function variable expression
     1012                VariableExpr *wrapFunctionDecl( DeclarationWithType *functionDecl ) {
     1013                        // line below cloned from FixFunction.cc
     1014                        ObjectDecl *functionObj = new ObjectDecl( functionDecl->get_name(), functionDecl->get_storageClass(), functionDecl->get_linkage(), 0,
     1015                                                                  new PointerType( Type::Qualifiers(), functionDecl->get_type()->clone() ), 0 );
     1016                        functionObj->set_mangleName( functionDecl->get_mangleName() );
     1017                        return new VariableExpr( functionObj );
     1018                }
     1019               
    9951020                Statement * Pass1::mutate( ReturnStmt *returnStmt ) {
    9961021                        if ( retval && returnStmt->get_expr() ) {
     
    10081033
    10091034                                // find assignment operator for (polymorphic) return type
    1010                                 DeclarationWithType *assignDecl = 0;
     1035                                ApplicationExpr *assignExpr = 0;
    10111036                                if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( retval->get_type() ) ) {
     1037                                        // find assignment operator for type variable
    10121038                                        std::map< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() );
    10131039                                        if ( assignIter == assignOps.end() ) {
    10141040                                                throw SemanticError( "Attempt to return dtype or ftype object in ", returnStmt->get_expr() );
    10151041                                        } // if
    1016                                         assignDecl = assignIter->second;
     1042                                        assignExpr = new ApplicationExpr( new VariableExpr( assignIter->second ) );
    10171043                                } else if ( ReferenceToType *refType = dynamic_cast< ReferenceToType *>( retval->get_type() ) ) {
     1044                                        // find assignment operator for generic type
    10181045                                        ScopedMap< std::string, DeclarationWithType *>::const_iterator assignIter = scopedAssignOps.find( refType->get_name() );
    10191046                                        if ( assignIter == scopedAssignOps.end() ) {
    10201047                                                throw SemanticError( "Attempt to return dtype or ftype generic object in ", returnStmt->get_expr() );
    10211048                                        }
     1049
     1050                                        // wrap it up in an application expression
    10221051                                        DeclarationWithType *functionDecl = assignIter->second;
    1023                                         // line below cloned from FixFunction.cc
    1024                                         assignDecl = new ObjectDecl( functionDecl->get_name(), functionDecl->get_storageClass(), functionDecl->get_linkage(), 0,
    1025                                                                      new PointerType( Type::Qualifiers(), functionDecl->get_type()->clone() ), 0 );
    1026                                         assignDecl->set_mangleName( functionDecl->get_mangleName() );
     1052                                        assignExpr = new ApplicationExpr( wrapFunctionDecl( functionDecl ) );
     1053                                        assignExpr->set_env( env->clone() );
     1054
     1055                                        // find each of its needed secondary assignment operators
     1056                                        std::list< Expression* > &tyParams = refType->get_parameters();
     1057                                        std::list< TypeDecl* > &forallParams = functionDecl->get_type()->get_forall();
     1058                                        std::list< Expression* >::const_iterator tyIt = tyParams.begin();
     1059                                        std::list< TypeDecl* >::const_iterator forallIt = forallParams.begin();
     1060                                        for ( ; tyIt != tyParams.end() && forallIt != forallParams.end(); ++tyIt, ++forallIt ) {
     1061                                                if ( (*forallIt)->get_kind() != TypeDecl::Any ) continue; // skip types with no assign op (ftype/dtype)
     1062
     1063                                                std::list< DeclarationWithType* > &asserts = (*forallIt)->get_assertions();
     1064                                                assert( ! asserts.empty() && "Type param needs assignment operator assertion" );
     1065                                                DeclarationWithType *actualDecl = asserts.front();
     1066                                                ReferenceToType *actualType = isAssignment( actualDecl );
     1067                                                assert( actualType && "First assertion of type with assertions should be assignment operator" );
     1068                                                TypeExpr *formalTypeExpr = dynamic_cast< TypeExpr* >( *tyIt );
     1069                                                assert( formalTypeExpr && "type parameters must be type expressions" );
     1070                                                Type *formalType = formalTypeExpr->get_type();
     1071                                                assignExpr->get_env()->add( actualType->get_name(), formalType );
     1072                                               
     1073                                                DeclarationWithType *assertAssign = 0;
     1074                                                if ( TypeInstType *formalTypeInstType = dynamic_cast< TypeInstType* >( formalType ) ) {
     1075                                                        std::map< std::string, DeclarationWithType *>::const_iterator assertAssignIt = assignOps.find( formalTypeInstType->get_name() );
     1076                                                        if ( assertAssignIt == assignOps.end() ) {
     1077                                                                throw SemanticError( "No assignment operation found for ", formalTypeInstType );
     1078                                                        }
     1079                                                        assertAssign = assertAssignIt->second;
     1080                                                        //assignExpr->get_env()->add( formalTypeInstType->get_name(), actualType );
     1081                                                } else if ( ReferenceToType *formalReferenceType = dynamic_cast< ReferenceToType* >( formalType ) )  {
     1082                                                        ScopedMap< std::string, DeclarationWithType *>::const_iterator assertAssignIt = scopedAssignOps.find( formalReferenceType->get_name() );
     1083                                                        if ( assertAssignIt == scopedAssignOps.end() ) {
     1084                                                                throw SemanticError( "No assignment operation found for ", formalReferenceType );
     1085                                                        }
     1086                                                        assertAssign = assertAssignIt->second;
     1087                                                } else assert( false && "returning polymorphic types with non struct/polymorphic parameters not yet supported" );
     1088                                               
     1089
     1090                                                assignExpr->get_inferParams()[ actualDecl->get_uniqueId() ]
     1091                                                        = ParamEntry( assertAssign->get_uniqueId(), assertAssign->get_type()->clone(), actualDecl->get_type()->clone(), wrapFunctionDecl( assertAssign ) );
     1092                                        }
    10271093                                }
    1028                                 assert( assignDecl );
     1094                                assert( assignExpr );
    10291095
    10301096                                // replace return statement with appropriate assignment to out parameter
    1031                                 ApplicationExpr *assignExpr = new ApplicationExpr( new VariableExpr( assignDecl ) );
    10321097                                Expression *retParm = new NameExpr( retval->get_name() );
    10331098                                retParm->get_results().push_back( new PointerType( Type::Qualifiers(), retval->get_type()->clone() ) );
     
    11811246                        }
    11821247
    1183                         // add size/align for generic types to parameter list
     1248                        // add size/align for generic parameter types to parameter list
    11841249                        std::set< std::string > seenTypes; // sizeofName for generic types we've seen
    11851250                        for ( std::list< DeclarationWithType* >::const_iterator fnParm = last; fnParm != funcType->get_parameters().end(); ++fnParm ) {
     
    12961361
    12971362                                if ( DeclarationWithType *declWithType = dynamic_cast< DeclarationWithType* >( *decl ) ) {
    1298                                         if ( memberDecl->get_mangleName() == declWithType->get_mangleName() ) return i;
     1363                                        if ( memberDecl->get_mangleName().empty() || declWithType->get_mangleName().empty()
     1364                                             || memberDecl->get_mangleName() == declWithType->get_mangleName() ) return i;
    12991365                                        else continue;
    13001366                                } else return i;
     
    13421408                        if ( ! objectType ) return memberExpr;
    13431409
     1410                        Expression *newMemberExpr = 0;
    13441411                        if ( StructInstType *structType = dynamic_cast< StructInstType* >( objectType ) ) {
    13451412                                // look up offset index
     
    13511418                                fieldLoc->get_args().push_back( makeDerefdVar( varExpr->clone(), varDepth ) );
    13521419                                fieldLoc->get_args().push_back( makeOffsetIndex( objectType, i ) );
    1353 
    1354                                 delete memberExpr;
    1355                                 return fieldLoc;
    1356                         } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( objectType ) ) {
     1420                                newMemberExpr = fieldLoc;
     1421                        } else if ( dynamic_cast< UnionInstType* >( objectType ) ) {
    13571422                                // union members are all at offset zero, so build appropriately-dereferenced variable
    1358                                 Expression *derefdVar = makeDerefdVar( varExpr->clone(), varDepth );
    1359                                 delete memberExpr;
    1360                                 return derefdVar;
     1423                                newMemberExpr = makeDerefdVar( varExpr->clone(), varDepth );
    13611424                        } else return memberExpr;
     1425                        assert( newMemberExpr );
     1426
     1427                        // wrap pointer members in appropriate cast
     1428                        if ( dynamic_cast< PointerType* >( memberExpr->get_member()->get_type() ) ) {
     1429                                CastExpr *ptrCastExpr = new CastExpr( newMemberExpr, new PointerType( Type::Qualifiers(), new PointerType( Type::Qualifiers(), new VoidType( Type::Qualifiers() ) ) ) );
     1430                                UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );
     1431                                derefExpr->get_args().push_back( ptrCastExpr );
     1432                                newMemberExpr = derefExpr;
     1433                        }
     1434
     1435                        delete memberExpr;
     1436                        return newMemberExpr;
    13621437                }
    13631438
     
    13801455                                delete offsetofExpr;
    13811456                                return offsetInd;
    1382                         } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( ty ) ) {
     1457                        } else if ( dynamic_cast< UnionInstType* >( ty ) ) {
    13831458                                // all union members are at offset zero
    13841459                                delete offsetofExpr;
  • src/ResolvExpr/AlternativeFinder.cc

    r7528ba1 ra172972  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // AlternativeFinder.cc -- 
     7// AlternativeFinder.cc --
    88//
    99// Author           : Richard C. Bilson
    1010// Created On       : Sat May 16 23:52:08 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jul  3 17:58:39 2015
    13 // Update Count     : 22
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Wed Feb 10 17:00:04 2016
     13// Update Count     : 24
    1414//
    1515
     
    4141
    4242extern bool resolvep;
    43 #define PRINT( text ) if ( resolvep ) { text } 
     43#define PRINT( text ) if ( resolvep ) { text }
    4444//#define DEBUG_COST
    4545
     
    107107                                        if ( candidate->cost < mapPlace->second.candidate->cost ) {
    108108                                                PRINT(
    109                                                         std::cout << "cost " << candidate->cost << " beats " << mapPlace->second.candidate->cost << std::endl;
     109                                                        std::cerr << "cost " << candidate->cost << " beats " << mapPlace->second.candidate->cost << std::endl;
    110110                                                )
    111111                                                selected[ mangleName ] = current;
    112112                                        } else if ( candidate->cost == mapPlace->second.candidate->cost ) {
    113113                                                PRINT(
    114                                                         std::cout << "marking ambiguous" << std::endl;
     114                                                        std::cerr << "marking ambiguous" << std::endl;
    115115                                                )
    116116                                                mapPlace->second.isAmbiguous = true;
     
    122122
    123123                        PRINT(
    124                                 std::cout << "there are " << selected.size() << " alternatives before elimination" << std::endl;
     124                                std::cerr << "there are " << selected.size() << " alternatives before elimination" << std::endl;
    125125                        )
    126126
     
    182182                        begin++;
    183183                        PRINT(
    184                                 std::cout << "findSubExprs" << std::endl;
    185                                 printAlts( finder.alternatives, std::cout );
     184                                std::cerr << "findSubExprs" << std::endl;
     185                                printAlts( finder.alternatives, std::cerr );
    186186                        )
    187187                        *out++ = finder;
     
    204204                }
    205205                PRINT(
    206                         std::cout << "alternatives before prune:" << std::endl;
    207                         printAlts( alternatives, std::cout );
     206                        std::cerr << "alternatives before prune:" << std::endl;
     207                        printAlts( alternatives, std::cerr );
    208208                )
    209209                AltList::iterator oldBegin = alternatives.begin();
     
    221221                alternatives.erase( oldBegin, alternatives.end() );
    222222                PRINT(
    223                         std::cout << "there are " << alternatives.size() << " alternatives after elimination" << std::endl;
     223                        std::cerr << "there are " << alternatives.size() << " alternatives after elimination" << std::endl;
    224224                )
    225225        }
     
    261261                for ( std::list< Expression* >::iterator actualExpr = actuals.begin(); actualExpr != actuals.end(); ++actualExpr ) {
    262262                        PRINT(
    263                                 std::cout << "actual expression:" << std::endl;
    264                                 (*actualExpr)->print( std::cout, 8 );
    265                                 std::cout << "--- results are" << std::endl;
    266                                 printAll( (*actualExpr)->get_results(), std::cout, 8 );
     263                                std::cerr << "actual expression:" << std::endl;
     264                                (*actualExpr)->print( std::cerr, 8 );
     265                                std::cerr << "--- results are" << std::endl;
     266                                printAll( (*actualExpr)->get_results(), std::cerr, 8 );
    267267                        )
    268268                        std::list< DeclarationWithType* >::iterator startFormal = formal;
     
    278278                                }
    279279                                PRINT(
    280                                         std::cout << std::endl << "converting ";
    281                                         (*actual)->print( std::cout, 8 );
    282                                         std::cout << std::endl << " to ";
    283                                         (*formal)->get_type()->print( std::cout, 8 );
     280                                        std::cerr << std::endl << "converting ";
     281                                        (*actual)->print( std::cerr, 8 );
     282                                        std::cerr << std::endl << " to ";
     283                                        (*formal)->get_type()->print( std::cerr, 8 );
    284284                                )
    285285                                Cost newCost = conversionCost( *actual, (*formal)->get_type(), indexer, alt.env );
    286286                                PRINT(
    287                                         std::cout << std::endl << "cost is" << newCost << std::endl;
     287                                        std::cerr << std::endl << "cost is" << newCost << std::endl;
    288288                                )
    289289
     
    323323                for ( InferredParams::const_iterator assert = appExpr->get_inferParams().begin(); assert != appExpr->get_inferParams().end(); ++assert ) {
    324324                        PRINT(
    325                                 std::cout << std::endl << "converting ";
    326                                 assert->second.actualType->print( std::cout, 8 );
    327                                 std::cout << std::endl << " to ";
    328                                 assert->second.formalType->print( std::cout, 8 );
     325                                std::cerr << std::endl << "converting ";
     326                                assert->second.actualType->print( std::cerr, 8 );
     327                                std::cerr << std::endl << " to ";
     328                                assert->second.formalType->print( std::cerr, 8 );
    329329                                )
    330330                                Cost newCost = conversionCost( assert->second.actualType, assert->second.formalType, indexer, alt.env );
    331331                        PRINT(
    332                                 std::cout << std::endl << "cost of conversion is " << newCost << std::endl;
     332                                std::cerr << std::endl << "cost of conversion is " << newCost << std::endl;
    333333                                )
    334334                                if ( newCost == Cost::infinity ) {
     
    448448                std::list< DeclarationWithType* > candidates;
    449449                decls.lookupId( curDecl->get_name(), candidates );
    450 ///   if ( candidates.empty() ) { std::cout << "no candidates!" << std::endl; }
     450///   if ( candidates.empty() ) { std::cerr << "no candidates!" << std::endl; }
    451451                for ( std::list< DeclarationWithType* >::const_iterator candidate = candidates.begin(); candidate != candidates.end(); ++candidate ) {
    452452                        PRINT(
    453                                 std::cout << "inferRecursive: candidate is ";
    454                                 (*candidate)->print( std::cout );
    455                                 std::cout << std::endl;
     453                                std::cerr << "inferRecursive: candidate is ";
     454                                (*candidate)->print( std::cerr );
     455                                std::cerr << std::endl;
    456456                        )
    457457                        AssertionSet newHave, newerNeed( newNeed );
     
    482482                                varExpr->get_results().push_front( adjType->clone() );
    483483                                PRINT(
    484                                         std::cout << "satisfying assertion " << curDecl->get_uniqueId() << " ";
    485                                         curDecl->print( std::cout );
    486                                         std::cout << " with declaration " << (*candidate)->get_uniqueId() << " ";
    487                                         (*candidate)->print( std::cout );
    488                                         std::cout << std::endl;
     484                                        std::cerr << "satisfying assertion " << curDecl->get_uniqueId() << " ";
     485                                        curDecl->print( std::cerr );
     486                                        std::cerr << " with declaration " << (*candidate)->get_uniqueId() << " ";
     487                                        (*candidate)->print( std::cerr );
     488                                        std::cerr << std::endl;
    489489                                )
    490490                                ApplicationExpr *appExpr = static_cast< ApplicationExpr* >( newerAlt.expr );
     
    501501        void AlternativeFinder::inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out ) {
    502502//      PRINT(
    503 //          std::cout << "inferParameters: assertions needed are" << std::endl;
    504 //          printAll( need, std::cout, 8 );
     503//          std::cerr << "inferParameters: assertions needed are" << std::endl;
     504//          printAll( need, std::cerr, 8 );
    505505//          )
    506506                SymTab::Indexer decls( indexer );
    507507                PRINT(
    508                         std::cout << "============= original indexer" << std::endl;
    509                         indexer.print( std::cout );
    510                         std::cout << "============= new indexer" << std::endl;
    511                         decls.print( std::cout );
     508                        std::cerr << "============= original indexer" << std::endl;
     509                        indexer.print( std::cerr );
     510                        std::cerr << "============= new indexer" << std::endl;
     511                        decls.print( std::cerr );
    512512                )
    513513                addToIndexer( have, decls );
     
    515515                inferRecursive( need.begin(), need.end(), newAlt, openVars, decls, newNeed, 0, indexer, out );
    516516//      PRINT(
    517 //          std::cout << "declaration 14 is ";
     517//          std::cerr << "declaration 14 is ";
    518518//          Declaration::declFromId
    519519//          *out++ = newAlt;
     
    532532                        makeExprList( actualAlt, appExpr->get_args() );
    533533                        PRINT(
    534                                 std::cout << "need assertions:" << std::endl;
    535                                 printAssertionSet( resultNeed, std::cout, 8 );
     534                                std::cerr << "need assertions:" << std::endl;
     535                                printAssertionSet( resultNeed, std::cerr, 8 );
    536536                        )
    537537                        inferParameters( resultNeed, resultHave, newAlt, openVars, out );
     
    543543                AlternativeFinder funcOpFinder( indexer, env );
    544544
    545                 AlternativeFinder funcFinder( indexer, env ); {
     545                AlternativeFinder funcFinder( indexer, env );
     546
     547                {
    546548                        NameExpr *fname = 0;;
    547549                        if ( ( fname = dynamic_cast<NameExpr *>( untypedExpr->get_function()))
     
    573575                for ( AltList::const_iterator func = funcFinder.alternatives.begin(); func != funcFinder.alternatives.end(); ++func ) {
    574576                        PRINT(
    575                                 std::cout << "working on alternative: " << std::endl;
    576                                 func->print( std::cout, 8 );
     577                                std::cerr << "working on alternative: " << std::endl;
     578                                func->print( std::cerr, 8 );
    577579                        )
    578580                        // check if the type is pointer to function
     
    606608                                        }
    607609                                        PRINT(
    608                                                 std::cout << "known function ops:" << std::endl;
    609                                                 printAlts( funcOpFinder.alternatives, std::cout, 8 );
     610                                                std::cerr << "known function ops:" << std::endl;
     611                                                printAlts( funcOpFinder.alternatives, std::cerr, 8 );
    610612                                        )
    611613                                }
     
    639641                                FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() );
    640642                                assert( function );
    641                                 std::cout << "Case +++++++++++++" << std::endl;
    642                                 std::cout << "formals are:" << std::endl;
    643                                 printAll( function->get_parameters(), std::cout, 8 );
    644                                 std::cout << "actuals are:" << std::endl;
    645                                 printAll( appExpr->get_args(), std::cout, 8 );
    646                                 std::cout << "bindings are:" << std::endl;
    647                                 withFunc->env.print( std::cout, 8 );
    648                                 std::cout << "cost of conversion is:" << cvtCost << std::endl;
     643                                std::cerr << "Case +++++++++++++" << std::endl;
     644                                std::cerr << "formals are:" << std::endl;
     645                                printAll( function->get_parameters(), std::cerr, 8 );
     646                                std::cerr << "actuals are:" << std::endl;
     647                                printAll( appExpr->get_args(), std::cerr, 8 );
     648                                std::cerr << "bindings are:" << std::endl;
     649                                withFunc->env.print( std::cerr, 8 );
     650                                std::cerr << "cost of conversion is:" << cvtCost << std::endl;
    649651                        )
    650652                        if ( cvtCost != Cost::infinity ) {
     
    824826                }
    825827        }
    826        
     828
    827829        void AlternativeFinder::visit( UntypedOffsetofExpr *offsetofExpr ) {
    828830                AlternativeFinder funcFinder( indexer, env );
     
    833835                }
    834836        }
    835        
     837
    836838        void AlternativeFinder::visit( OffsetofExpr *offsetofExpr ) {
    837839                alternatives.push_back( Alternative( offsetofExpr->clone(), env, Cost::zero ) );
     
    843845                assert( function->get_parameters().size() == 1 );
    844846                PRINT(
    845                         std::cout << "resolvAttr: funcDecl is ";
    846                         funcDecl->print( std::cout );
    847                         std::cout << " argType is ";
    848                         argType->print( std::cout );
    849                         std::cout << std::endl;
     847                        std::cerr << "resolvAttr: funcDecl is ";
     848                        funcDecl->print( std::cerr );
     849                        std::cerr << " argType is ";
     850                        argType->print( std::cerr );
     851                        std::cerr << std::endl;
    850852                )
    851853                if ( typesCompatibleIgnoreQualifiers( argType, function->get_parameters().front()->get_type(), indexer, env ) ) {
  • src/ResolvExpr/CastCost.cc

    r7528ba1 ra172972  
    99// Author           : Richard C. Bilson
    1010// Created On       : Sun May 17 06:57:43 2015
    11 // Last Modified By : Rob Schluntz
    12 // Last Modified On : Mon Oct 05 14:48:45 2015
    13 // Update Count     : 5
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Tue Feb  2 15:34:36 2016
     13// Update Count     : 7
    1414//
    1515
     
    6969                PointerType *destAsPointer = dynamic_cast< PointerType* >( dest );
    7070                if ( destAsPointer && basicType->isInteger() ) {
    71                         cost = Cost( 1, 0, 0 );
     71                        //cost = Cost( 1, 0, 0 );
     72                        cost = Cost::infinity;
    7273                } else {
    7374                        ConversionCost::visit( basicType );
     
    8788                                        cost = Cost( 0, 0, 1 );
    8889                                } else if ( castResult < 0 ) {
    89                                         cost = Cost( 1, 0, 0 );
     90                                        cost = Cost::infinity;
     91                                        //cost = Cost( 1, 0, 0 );
    9092                                } // if
    9193                        } // if
    9294                } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
    9395                        if ( destAsBasic->isInteger() ) {
    94                                 cost = Cost( 1, 0, 0 );
     96                                //cost = Cost( 1, 0, 0 );
     97                                cost = Cost::infinity;
    9598                        } // if
    9699                }
  • src/ResolvExpr/Resolver.cc

    r7528ba1 ra172972  
    324324                                                                                                BasicType::SignedInt);
    325325                                } else {
    326                                         DeclarationWithType * decl = lookupId(n);
     326                                        DeclarationWithType * decl = lookupId( n );
    327327                                        initContext = decl->get_type();
    328328                                }
     
    346346                                        if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_results().front() ) ) {
    347347                                                if ( isCharType( pt->get_base() ) ) {
    348                                                         // strip cast if we're initializing a char[] with a char *, e.g.
    349                                                         // char x[] = "hello";
     348                                                        // strip cast if we're initializing a char[] with a char *, e.g.  char x[] = "hello";
    350349                                                        CastExpr *ce = dynamic_cast< CastExpr * >( newExpr );
    351350                                                        singleInit->set_value( ce->get_arg() );
  • src/SymTab/Validate.cc

    r7528ba1 ra172972  
    396396                // it's not a semantic error if the struct is not found, just an implicit forward declaration
    397397                if ( st ) {
    398                         assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() );
     398                        //assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() );
    399399                        structInst->set_baseStruct( st );
    400400                } // if
     
    673673        }
    674674
     675        /// Creates a new type decl that's the same as src, but renamed and with only the ?=? assertion (for complete types only)
     676        TypeDecl *cloneAndRename( TypeDecl *src, const std::string &name ) {
     677                TypeDecl *dst = new TypeDecl( name, src->get_storageClass(), 0, src->get_kind() );
     678
     679                if ( src->get_kind() == TypeDecl::Any ) {
     680                        // just include assignment operator assertion
     681                        TypeInstType *assignParamType = new TypeInstType( Type::Qualifiers(), name, dst );
     682                        FunctionType *assignFunctionType = new FunctionType( Type::Qualifiers(), false );
     683                        assignFunctionType->get_returnVals().push_back(
     684                                new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, assignParamType->clone(), 0 ) );
     685                        assignFunctionType->get_parameters().push_back(
     686                                new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), assignParamType->clone() ), 0 ) );
     687                        assignFunctionType->get_parameters().push_back(
     688                                new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, assignParamType, 0 ) );
     689                        FunctionDecl *assignAssert = new FunctionDecl( "?=?", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, assignFunctionType, 0, false, false );
     690                        dst->get_assertions().push_back( assignAssert );
     691                }
     692
     693                return dst;
     694        }
     695
    675696        Declaration *makeStructAssignment( StructDecl *aggregateDecl, StructInstType *refType, unsigned int functionNesting ) {
    676697                FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
     
    680701                std::list< TypeDecl* >& genericParams = aggregateDecl->get_parameters();
    681702                std::list< Expression* > structParams;  // List of matching parameters to put on types
     703                TypeSubstitution genericSubs; // Substitutions to make to member types of struct
    682704                for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) {
    683705                        isGeneric = true;
    684                         TypeDecl *typeParam = (*param)->clone();
     706                        TypeDecl *typeParam = cloneAndRename( *param, "_autoassign_" + aggregateDecl->get_name() + "_" + (*param)->get_name() );
    685707                        assignType->get_forall().push_back( typeParam );
    686                         structParams.push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam ) ) );
     708                        TypeInstType *newParamType = new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam );
     709                        genericSubs.add( (*param)->get_name(), newParamType );
     710                        structParams.push_back( new TypeExpr( newParamType ) );
    687711                }
    688712
     
    715739                                }
    716740
    717                                 if ( ArrayType *array = dynamic_cast< ArrayType * >( dwt->get_type() ) ) {
    718                                         makeArrayAssignment( srcParam, dstParam, dwt, array, back_inserter( assignDecl->get_statements()->get_kids() ) );
    719                                         if ( isGeneric ) makeArrayAssignment( srcParam, returnVal, dwt, array, back_inserter( assignDecl->get_statements()->get_kids() ) );
     741                                if ( isGeneric ) {
     742                                        // rewrite member type in terms of the type variables on this operator
     743                                        DeclarationWithType *fixedMember = dwt->clone();
     744                                        genericSubs.apply( fixedMember );
     745
     746                                        // assign to both destination and return value
     747                                        if ( ArrayType *array = dynamic_cast< ArrayType * >( fixedMember->get_type() ) ) {
     748                                                makeArrayAssignment( srcParam, dstParam, fixedMember, array, back_inserter( assignDecl->get_statements()->get_kids() ) );
     749                                                makeArrayAssignment( srcParam, returnVal, fixedMember, array, back_inserter( assignDecl->get_statements()->get_kids() ) );
     750                                        } else {
     751                                                makeScalarAssignment( srcParam, dstParam, fixedMember, back_inserter( assignDecl->get_statements()->get_kids() ) );
     752                                                makeScalarAssignment( srcParam, returnVal, fixedMember, back_inserter( assignDecl->get_statements()->get_kids() ) );
     753                                        } // if
    720754                                } else {
    721                                         makeScalarAssignment( srcParam, dstParam, dwt, back_inserter( assignDecl->get_statements()->get_kids() ) );
    722                                         if ( isGeneric ) makeScalarAssignment( srcParam, returnVal, dwt, back_inserter( assignDecl->get_statements()->get_kids() ) );
     755                                        // assign to destination
     756                                        if ( ArrayType *array = dynamic_cast< ArrayType * >( dwt->get_type() ) ) {
     757                                                makeArrayAssignment( srcParam, dstParam, dwt, array, back_inserter( assignDecl->get_statements()->get_kids() ) );
     758                                        } else {
     759                                                makeScalarAssignment( srcParam, dstParam, dwt, back_inserter( assignDecl->get_statements()->get_kids() ) );
     760                                        } // if
    723761                                } // if
    724762                        } // if
     
    799837                for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) {
    800838                        isGeneric = true;
    801                         TypeDecl *typeParam = (*param)->clone();
     839                        TypeDecl *typeParam = cloneAndRename( *param, "_autoassign_" + aggregateDecl->get_name() + "_" + (*param)->get_name() );
    802840                        assignType->get_forall().push_back( typeParam );
    803841                        unionParams.push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam ) ) );
  • src/Tests/Makefile

    r7528ba1 ra172972  
    88OUTPUTS = ${addprefix ${OUTPUTDIR}/,${EXAMPLES:.c=.txt}}
    99
    10 .SILENT :
     10#.SILENT :
    1111
    1212all :
     
    1919
    2020${OUTPUTDIR}/%.txt : %.c ${CFA} Makefile
    21         -${CFA} -n ${CFAOPT} < $< > $@ 2>&1
     21        -${CFA} -n ${CFAOPT} $< > $@ 2>&1
    2222
    2323${OUTPUTDIR}/report : ${OUTPUTS} ${EXPECTDIR}
  • src/examples/abs.c

    r7528ba1 ra172972  
    1010// Created On       : Thu Jan 28 18:26:16 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Feb  3 11:14:58 2016
    13 // Update Count     : 43
     12// Last Modified On : Wed Feb 17 09:32:04 2016
     13// Update Count     : 44
    1414//
    1515
     
    1818
    1919int main( void ) {
    20         ofstream *sout = ofstream_stdout();
    21 
    2220        char ch = -65;
    2321        sout | "char\t\t\t"                                     | ch     | "\tabs " | abs( ch ) | endl;
  • src/examples/alloc.c

    r7528ba1 ra172972  
    1111// Created On       : Wed Feb  3 07:56:22 2016
    1212// Last Modified By : Peter A. Buhr
    13 // Last Modified On : Wed Feb  3 16:32:04 2016
    14 // Update Count     : 38
     13// Last Modified On : Wed Feb 17 11:43:23 2016
     14// Update Count     : 40
    1515//
    1616
     
    2727
    2828int main( void ) {
    29     ofstream * sout = ofstream_stdout();
    30 
    3129    size_t size = 10;
    3230    int * p;
     
    10098    free( x );
    10199#endif
    102     free( sout );
    103100}
    104101
  • src/examples/fstream_test.c

    r7528ba1 ra172972  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Jan 26 17:11:33 2016
    13 // Update Count     : 42
     12// Last Modified On : Wed Feb 17 11:45:43 2016
     13// Update Count     : 43
    1414//
    1515
     
    1717
    1818int main( void ) {
    19         ofstream *sout = ofstream_stdout();
    20         ifstream *sin = ifstream_stdin();
    2119        int nombre;
    2220        sout | "Entrez un nombre, s'il vous plaît:\n";
  • src/examples/hello.c

    r7528ba1 ra172972  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Jan 26 17:11:49 2016
    13 // Update Count     : 7
     12// Last Modified On : Wed Feb 17 12:11:45 2016
     13// Update Count     : 8
    1414//
    1515
     
    1717
    1818int main() {
    19         ofstream *sout = ofstream_stdout();
    20         ifstream *sin = ifstream_stdin();
    2119        sout | "Bonjour au monde!\n";
    2220}
  • src/examples/identity.c

    r7528ba1 ra172972  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Jan 26 17:11:58 2016
    13 // Update Count     : 8
     12// Last Modified On : Wed Feb 17 12:17:32 2016
     13// Update Count     : 10
    1414//
    1515
     
    2222
    2323int main() {
    24         ofstream *sout = ofstream_stdout();
    2524        sout | "char\t\t\t"                                     | identity( 'z' ) | endl;
    2625        sout | "signed int\t\t"                         | identity( 4 ) | endl;
     
    3029        sout | "signed long long int\t"         | identity( 4ll ) | endl;
    3130        sout | "unsigned long long int\t"       | identity( 4ull ) | endl;
    32         sout | "float\t\t\t"                            | identity( 4.0f ) | endl;
    33         sout | "double\t\t\t"                           | identity( 4.0 ) | endl;
    34         sout | "long double\t\t"                        | identity( 4.0l ) | endl;
     31        sout | "float\t\t\t"                            | identity( 4.1f ) | endl;
     32        sout | "double\t\t\t"                           | identity( 4.1 ) | endl;
     33        sout | "long double\t\t"                        | identity( 4.1l ) | endl;
    3534}
    3635
  • src/examples/minmax.c

    r7528ba1 ra172972  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Feb  3 11:14:49 2016
    13 // Update Count     : 46
     12// Last Modified On : Wed Feb 17 12:17:53 2016
     13// Update Count     : 47
    1414//
    1515
     
    1818
    1919int main( void ) {
    20         ofstream *sout = ofstream_stdout();
    2120        // char does not have less or greater than.
    2221        int ?<?( char op1, char op2 ) { return (int)op1 < (int)op2; }
  • src/examples/quad.c

    r7528ba1 ra172972  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Jan 26 17:13:48 2016
    13 // Update Count     : 5
     12// Last Modified On : Wed Feb 17 12:19:24 2016
     13// Update Count     : 6
    1414//
    1515
     
    2727
    2828int main() {
    29         ofstream *sout = ofstream_stdout();
    3029        int N = 2;
    3130        sout | "result of quad of " | N | " is " | quad( N ) | endl;
  • src/examples/quoted_keyword.c

    r7528ba1 ra172972  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Jan 26 17:13:58 2016
    13 // Update Count     : 8
     12// Last Modified On : Wed Feb 17 12:19:45 2016
     13// Update Count     : 9
    1414//
    1515
     
    2828
    2929int main() {
    30         ofstream *sout = ofstream_stdout();
    3130        sout | `catch` + st.`type` + st.`struct` + `throw` | endl;
    3231}
  • src/examples/random.c

    r7528ba1 ra172972  
    77
    88int main() {
    9         ofstream *sout = ofstream_stdout();
    10 
    119        randseed( getpid() );                                                           // set random seed
    1210
  • src/examples/square.c

    r7528ba1 ra172972  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Jan 26 17:14:16 2016
    13 // Update Count     : 25
     12// Last Modified On : Wed Feb 17 12:21:58 2016
     13// Update Count     : 26
    1414//
    1515
     
    2323int main() {
    2424#if 0
    25         ofstream *sout = ofstream_stdout();
    2625        sout | "result of squaring 9 is " | endl;
    2726
  • src/examples/sum.c

    r7528ba1 ra172972  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Feb  5 16:47:44 2016
    13 // Update Count     : 139
     12// Last Modified On : Tue Feb 16 23:49:31 2016
     13// Update Count     : 189
    1414//
    1515
     
    3333
    3434// Required to satisfy sumable as char does not have addition.
    35 // const char 0;
    36 // char ?+?( char op1, char op2 ) { return (int)op1 + op2; } // cast forces integer addition or recursion
    37 // char ++?( char *op ) { *op += 1; return *op; }
    38 // char ?++( char *op ) { char temp = *op; *op += 1; return temp; }
     35const char 0;
     36char ?+?( char t1, char t2 ) { return (int)t1 + t2; }   // cast forces integer addition, otherwise recursion
     37char ?+=?( char *t1, char t2 ) { *t1 = *t1 + t2; return *t1; }
     38char ++?( char *t ) { *t += 1; return *t; }
     39char ?++( char *t ) { char temp = *t; *t += 1; return temp; }
    3940
    4041int main( void ) {
    4142        const int low = 5, High = 15, size = High - low;
    42         ofstream *sout = ofstream_stdout();
    43 #if 0
    4443
    45         char s = 0, a[size];
    46         char v = low;
     44        char s = 0, a[size], v = low;
    4745        for ( int i = 0; i < size; i += 1, v += 1 ) {
    4846                s += v;
    4947                a[i] = v;
    50         }
     48        } // for
    5149        sout | "sum from " | low | " to " | High | " is "
    5250                 | (int)sum( size, a ) | ", check " | (int)s | endl;
    5351
    54         int s = 0, a[size];
    55         int v = low;
     52        int s = 0, a[size], v = low;
    5653        for ( int i = 0; i < size; i += 1, v += 1 ) {
    5754                s += (int)v;
    5855                a[i] = (int)v;
    59         }
     56        } // for
    6057        sout | "sum from " | low | " to " | High | " is "
    6158                 | sum( size, (int *)a ) | ", check " | (int)s | endl;
    6259
    63         float s = 0.0, a[size];
    64         float v = low / 10.0;
     60        float s = 0.0, a[size], v = low / 10.0;
    6561        for ( int i = 0; i < size; i += 1, v += 0.1f ) {
    6662                s += (float)v;
    6763                a[i] = (float)v;
    68         }
     64        } // for
    6965        sout | "sum from " | low / 10.0 | " to " | High / 10.0 | " is "
    7066                 | sum( size, (float *)a ) | ", check " | (float)s | endl;
    71 #endif
    72         double s = 0, a[size];
    73         double v = low / 10.0;
    7467
     68        double s = 0, a[size], v = low / 10.0;
    7569        for ( int i = 0; i < size; i += 1, v += 0.1 ) {
    7670                s += (double)v;
    7771                a[i] = (double)v;
    78         }
     72        } // for
    7973        sout | "sum from " | low / 10.0 | " to " | High / 10.0 | " is "
    8074                 | sum( size, (double *)a ) | ", check " | (double)s | endl;
    8175
    82         // struct S { int i, j; } sarr[size];
    83         // struct S 0 = { 0, 0 };
    84         // struct S 1 = { 1, 1 };
    85         // S ?+?( S t1, S t2 ) { S s = { t1.i + t1.j, t2.i + t2.j }; return s; }
    86         // S ?+=?( S *t1, S t2 ) { *t1 = *t1 + t2; return *t1; }
    87         // S ++?( S *t ) { *t += 1; return *t; }
    88         // S ?++( S *t ) { S temp = *t; *t += 1; return temp; }
    89         // sum( size, sarr );
     76        struct S { int i, j; } 0 = { 0, 0 }, 1 = { 1, 1 };
     77        S ?+?( S t1, S t2 ) { S s = { t1.i + t2.i, t1.j + t2.j }; return s; }
     78        S ?+=?( S *t1, S t2 ) { *t1 = *t1 + t2; return *t1; }
     79        S ++?( S *t ) { *t += 1; return *t; }
     80        S ?++( S *t ) { S temp = *t; *t += 1; return temp; }
     81        ofstream * ?|?( ofstream * os, S v ) { return os | v.i | ' ' | v.j; }
     82
     83        S s = 0, a[size], v = { low, low };
     84        for ( int i = 0; i < size; i += 1, v += (S)1 ) {
     85                s += (S)v;
     86                a[i] = (S)v;
     87        } // for
     88        sout | "sum from " | low | " to " | High | " is "
     89                 | sum( size, (S *)a ) | ", check " | (S)s | endl;
    9090} // main
    9191
  • src/examples/swap.c

    r7528ba1 ra172972  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Feb  3 11:14:04 2016
    13 // Update Count     : 63
     12// Last Modified On : Wed Feb 17 12:22:12 2016
     13// Update Count     : 64
    1414//
    1515
     
    1818
    1919int main( void ) {
    20         ofstream *sout = ofstream_stdout();
    21 
    2220        char c1 = 'a', c2 = 'b';
    2321        sout | "char\t\t\t" | c1 | ' ' | c2 | "\t\t\tswap ";
  • src/examples/twice.c

    r7528ba1 ra172972  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Jan 26 17:14:44 2016
    13 // Update Count     : 12
     12// Last Modified On : Wed Feb 17 12:23:25 2016
     13// Update Count     : 13
    1414//
    1515
     
    2727        char ?++( char *op ) { char temp = *op; *op += 1; return temp; }
    2828
    29         ofstream *sout = ofstream_stdout();
    3029        sout | twice( 'a' ) | ' ' | twice( 1 ) | ' ' | twice( 3.2 ) | endl;
    3130}
  • src/examples/vector_test.c

    r7528ba1 ra172972  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Jan 26 17:14:52 2016
    13 // Update Count     : 17
     12// Last Modified On : Wed Feb 17 12:23:55 2016
     13// Update Count     : 18
    1414//
    1515
     
    2020
    2121int main( void ) {
    22         ofstream *sout = ofstream_stdout();
    23         ifstream *sin = ifstream_stdin();
    2422        vector_int vec = vector_int_allocate();
    2523
  • src/libcfa/fstream

    r7528ba1 ra172972  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Jan 27 23:47:41 2016
    13 // Update Count     : 3
     12// Last Modified On : Wed Feb 17 14:02:01 2016
     13// Update Count     : 22
    1414//
    1515
     
    1919#include "iostream"
    2020
    21 typedef struct ofstream ofstream;
     21// implement context ostream
     22struct ofstream;
    2223
    23 // implement context ostream
    24 ofstream *write( ofstream *, const char *, streamsize_type );
    25 int fail( ofstream * );
     24int fail( ofstream * os );
     25int flush( ofstream * os );
     26void open( ofstream ** os, const char * name, const char * mode );
     27void close( ofstream * os );
     28ofstream * write( ofstream * os, const char * data, streamsize_type size );
    2629
    27 ofstream *ofstream_stdout();
    28 ofstream *ofstream_stderr();
    29 ofstream *ofstream_fromfile( const char *name );
    30 void ofstream_close( ofstream *os );
    31 
    32 typedef struct ifstream ifstream;
     30extern ofstream * sout, * serr;
    3331
    3432// implement context istream
    35 ifstream *read( ifstream *, char *, streamsize_type );
    36 ifstream *unread( ifstream *, char );
    37 int fail( ifstream * );
    38 int eof( ifstream * );
     33struct ifstream;
    3934
    40 ifstream *ifstream_stdin();
    41 ifstream *ifstream_fromfile( const char *name );
     35int fail( ifstream * is );
     36int eof( ifstream * is );
     37void open( ifstream ** is, const char * name, const char * mode );
     38void close( ifstream * is );
     39ifstream * get( ifstream * is, int * data );
     40ifstream * read( ifstream * is, char * data, streamsize_type size );
     41ifstream * ungetc( ifstream * is, char c );
     42
     43extern ifstream *sin;
    4244
    4345#endif // __FSTREAM_H__
  • src/libcfa/fstream.c

    r7528ba1 ra172972  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Jan 26 17:12:59 2016
    13 // Update Count     : 6
     12// Last Modified On : Wed Feb 17 14:03:05 2016
     13// Update Count     : 76
    1414//
    1515
     
    2323struct ofstream {
    2424        FILE *file;
    25         int fail;
    2625};
    2726
    28 ofstream *write( ofstream *os, const char *data, streamsize_type size ) {
    29         if ( ! os->fail ) {
    30                 fwrite( data, size, 1, os->file );
    31                 os->fail = ferror( os->file );
     27#define IO_MSG "I/O error "
     28
     29int fail( ofstream * os ) {
     30        return ferror( os->file );
     31} // fail
     32
     33int flush( ofstream * os ) {
     34        return fflush( os->file );
     35} // flush
     36
     37void open( ofstream ** os, const char * name, const char * mode ) {
     38        FILE *t = fopen( name, mode );
     39        if ( t == 0 ) {                                                                         // do not change unless successful
     40                perror( IO_MSG "open output" );
     41                exit( EXIT_FAILURE );
     42        } // if
     43        (*os)->file = t;
     44} // open
     45
     46void close( ofstream * os ) {
     47        if ( os->file == stdout || os->file == stderr ) return;
     48
     49        if ( fclose( os->file ) == EOF ) {
     50                perror( IO_MSG "close output" );
     51        } // if
     52} // close
     53
     54ofstream * write( ofstream * os, const char * data, streamsize_type size ) {
     55        if ( fail( os ) ) {
     56                fprintf( stderr, "attempt write I/O on failed stream\n" );
     57                exit( EXIT_FAILURE );
     58        } // if
     59
     60        if ( fwrite( data, 1, size, os->file ) != size ) {
     61                perror( IO_MSG "write" );
     62                exit( EXIT_FAILURE );
    3263        } // if
    3364        return os;
    3465} // write
    3566
    36 int fail( ofstream *os ) {
    37         return os->fail;
    38 } // fail
     67static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_) };
     68ofstream *sout = &soutFile;
     69static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_) };
     70ofstream *serr = &serrFile;
    3971
    40 static ofstream *make_ofstream() {
    41         ofstream *new_stream = malloc( sizeof( ofstream ) );
    42         new_stream->fail = 0;
    43         return new_stream;
    44 } // make_ofstream
    45 
    46 ofstream *ofstream_stdout() {
    47         ofstream *stdout_stream = make_ofstream();
    48         stdout_stream->file = stdout;
    49         return stdout_stream;
    50 } // ofstream_stdout
    51 
    52 ofstream *ofstream_stderr() {
    53         ofstream *stderr_stream = make_ofstream();
    54         stderr_stream->file = stderr;
    55         return stderr_stream;
    56 } // ofstream_stderr
    57 
    58 ofstream *ofstream_fromfile( const char *name ) {
    59         ofstream *file_stream = make_ofstream();
    60         file_stream->file = fopen( name, "w" );
    61         file_stream->fail = file_stream->file == 0;
    62         return file_stream;
    63 }
    64 
    65 void ofstream_close( ofstream *os ) {
    66         if ( os->file != stdout && os->file != stderr ) {
    67                 os->fail = fclose( os->file );
    68         }
    69         free( os );
    70 }
     72//---------------------------------------
    7173
    7274struct ifstream {
    7375        FILE *file;
    74         int fail;
    75         int eof;
    7676};
    7777
    78 ifstream *read( ifstream *is, char *data, streamsize_type size ) {
    79         if ( ! is->fail && ! is->eof ) {
    80                 fread( data, size, 1, is->file );
    81                 is->fail = ferror( is->file );
    82                 is->eof = feof( is->file );
    83         }
     78int fail( ifstream * is ) {
     79        return ferror( is->file );
     80} // fail
     81
     82int eof( ifstream * is ) {
     83        return feof( is->file );
     84} // eof
     85
     86ifstream * get( ifstream * is, int * data ) {
     87        if ( fscanf( is->file, "%d", data ) == EOF ) {
     88                if ( ferror( is->file ) ) {
     89                        fprintf( stderr, "invalid int read\n" );
     90                        exit( EXIT_FAILURE );
     91                } // if
     92        } // if
    8493        return is;
    85 }
     94} // read
     95
     96ifstream * read( ifstream * is, char * data, streamsize_type size ) {
     97        if ( fail( is ) ) {
     98                fprintf( stderr, "attempt read I/O on failed stream\n" );
     99                exit( EXIT_FAILURE );
     100        } // if
     101
     102        if ( fread( data, size, 1, is->file ) == 0 ) {
     103                perror( IO_MSG "read" );
     104                exit( EXIT_FAILURE );
     105        } // if
     106        return is;
     107} // read
    86108 
    87 ifstream *unread( ifstream *is, char c ) {
    88         if ( ! is->fail ) {
    89                 if ( ! EOF == ungetc( c, is->file ) ) {
    90                         is->fail = 1;
    91                 }
    92         }
     109ifstream *ungetc( ifstream * is, char c ) {
     110        if ( fail( is ) ) {
     111                fprintf( stderr, "attempt ungetc I/O on failed stream\n" );
     112                exit( EXIT_FAILURE );
     113        } // if
     114
     115        if ( ungetc( c, is->file ) == EOF ) {
     116                perror( IO_MSG "ungetc" );
     117                exit( EXIT_FAILURE );
     118        } // if
    93119        return is;
    94 }
     120} // ungetc
    95121
    96 int fail( ifstream *is ) {
    97         return is->fail;
    98 }
     122void open( ifstream ** is, const char * name, const char * mode ) {
     123        FILE *t = fopen( name, mode );
     124        if ( t == 0 ) {                                                                         // do not change unless successful
     125                perror( IO_MSG "open input" );
     126                exit( EXIT_FAILURE );
     127        } // if
     128        (*is)->file = t;
     129} // open
    99130
    100 int eof( ifstream *is ) {
    101         return is->eof;
    102 }
     131void close( ifstream * is ) {
     132        if ( is->file == stdin ) return;
    103133
    104 static ifstream *make_ifstream() {
    105         ifstream *new_stream = malloc( sizeof( ifstream ) );
    106         new_stream->fail = 0;
    107         new_stream->eof = 0;
    108         return new_stream;
    109 }
     134        if ( fclose( is->file ) == EOF ) {
     135                perror( IO_MSG "close input" );
     136        } // if
     137} // close
    110138
    111 ifstream *ifstream_stdin() {
    112         ifstream *stdin_stream = make_ifstream();
    113         stdin_stream->file = stdin;
    114         return stdin_stream;
    115 }
    116 
    117 ifstream *ifstream_fromfile( const char *name ) {
    118         ifstream *file_stream = make_ifstream();
    119         file_stream->file = fopen( name, "r" );
    120         file_stream->fail = file_stream->file == 0;
    121         return file_stream;
    122 }
     139static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
     140ifstream *sin = &sinFile;
    123141
    124142// Local Variables: //
  • src/libcfa/iostream

    r7528ba1 ra172972  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jan 29 15:50:36 2016
    13 // Update Count     : 29
     12// Last Modified On : Wed Feb 17 14:04:24 2016
     13// Update Count     : 32
    1414//
    1515
     
    2222
    2323context ostream( dtype ostype ) {
    24         ostype *write( ostype *, const char *, streamsize_type );
    2524        int fail( ostype * );
     25        int flush( ostype * );
     26        ostype * write( ostype *, const char *, streamsize_type );
    2627};
    27 
    2828context writeable( type T ) {
    2929        forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, T );
     
    5252
    5353// writes the range [begin, end) to the given stream
    54 forall( type elt_type | writeable( elt_type ),
    55                 type iterator_type | iterator( iterator_type, elt_type ),
    56                 dtype os_type | ostream( os_type ) )
     54forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ), dtype os_type | ostream( os_type ) )
    5755void write( iterator_type begin, iterator_type end, os_type *os );
    5856
    59 forall( type elt_type | writeable( elt_type ),
    60                 type iterator_type | iterator( iterator_type, elt_type ),
    61                 dtype os_type | ostream( os_type ) )
     57forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ), dtype os_type | ostream( os_type ) )
    6258void write_reverse( iterator_type begin, iterator_type end, os_type *os );
    6359
    64 //******************************************************************************
     60//---------------------------------------
    6561
    6662context istream( dtype istype ) {
    67         istype *read( istype *, char *, streamsize_type );
    68         istype *unread( istype *, char );
    6963        int fail( istype * );
    7064        int eof( istype * );
     65        istype * get( istype *, int * );
     66        istype * read( istype *, char *, streamsize_type );
     67        istype * ungetc( istype *, char );
    7168};
    7269
  • src/libcfa/iostream.c

    r7528ba1 ra172972  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Feb  1 14:20:30 2016
    13 // Update Count     : 60
     12// Last Modified On : Wed Feb 17 14:19:56 2016
     13// Update Count     : 76
    1414//
    1515
     
    1919#include <stdio.h>
    2020#include <string.h>                                                                             // strlen
     21#include <float.h>                                                                              // DBL_DIG, LDBL_DIG
    2122#include <complex.h>                                                                    // creal, cimag
    2223}
     
    7273ostype * ?|?( ostype *os, double d ) {
    7374        char buffer[32];
    74         return write( os, buffer, sprintf( buffer, "%g", d ) );
     75        return write( os, buffer, sprintf( buffer, "%.*lg", DBL_DIG, d ) );
    7576} // ?|?
    7677
     
    7879ostype * ?|?( ostype *os, long double d ) {
    7980        char buffer[32];
    80         return write( os, buffer, sprintf( buffer, "%Lg", d ) );
     81        return write( os, buffer, sprintf( buffer, "%.*Lg", LDBL_DIG, d ) );
    8182} // ?|?
    8283
     
    110111forall( dtype ostype, dtype retostype | ostream( ostype ) | ostream( retostype ) )
    111112retostype * ?|?( ostype *os, retostype * (*manip)(ostype*) ) {
    112   return manip(os);
     113  return manip( os );
    113114}
    114115
    115116forall( dtype ostype | ostream( ostype ) )
    116117ostype * endl( ostype * os ) {
    117   os | "\n";
    118   // flush
    119   return os;
     118        os | "\n";
     119        flush( os );
     120        return os;
    120121} // endl
    121122
    122 forall( type elt_type | writeable( elt_type ),
    123                 type iterator_type | iterator( iterator_type, elt_type ),
     123//---------------------------------------
     124
     125forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ),
    124126                dtype os_type | ostream( os_type ) )
    125127void write( iterator_type begin, iterator_type end, os_type *os ) {
    126         void print( elt_type i ) {
    127                 os | i | ' ';
    128         }
     128        void print( elt_type i ) { os | i | ' '; }
    129129        for_each( begin, end, print );
    130130} // ?|?
    131131
    132 forall( type elt_type | writeable( elt_type ),
    133                 type iterator_type | iterator( iterator_type, elt_type ),
     132forall( type elt_type | writeable( elt_type ), type iterator_type | iterator( iterator_type, elt_type ),
    134133                dtype os_type | ostream( os_type ) )
    135134void write_reverse( iterator_type begin, iterator_type end, os_type *os ) {
     
    138137} // ?|?
    139138
     139//---------------------------------------
    140140
    141141forall( dtype istype | istream( istype ) )
     
    146146forall( dtype istype | istream( istype ) )
    147147istype * ?|?( istype *is, int *ip ) {
    148         char cur;
    149  
    150         // skip some whitespace
    151         do {
    152                 is | &cur;
    153                 if ( fail( is ) || eof( is ) ) return is;
    154         } while ( !( cur >= '0' && cur <= '9' ) );
    155  
    156         // accumulate digits
    157         *ip = 0;
    158         while ( cur >= '0' && cur <= '9' ) {
    159                 *ip = *ip * 10 + ( cur - '0' );
    160                 is | &cur;
    161                 if ( fail( is ) || eof( is ) ) return is;
    162         }
    163  
    164         unread( is, cur );
    165         return is;
     148        return get( is, ip );
    166149} // ?|?
    167150
  • src/libcfa/stdlib.c

    r7528ba1 ra172972  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Feb  5 15:41:24 2016
    13 // Update Count     : 128
     12// Last Modified On : Wed Feb 10 15:45:56 2016
     13// Update Count     : 140
    1414//
    1515
     
    2323#include <string.h>                                                                             // memset
    2424#include <malloc.h>                                                                             // malloc_usable_size
    25 #include <stdio.h>
    2625#include <math.h>                                                                               // fabsf, fabs, fabsl
    2726#include <complex.h>                                                                    // _Complex_I, cabsf, cabs, cabsl
     
    106105long int ato( const char * ptr ) {
    107106        long int li;
    108         if ( sscanf( ptr, "%ld", &li ) == EOF ) {};                     // check return code
     107        if ( sscanf( ptr, "%ld", &li ) == EOF ) {}                      // check return code
    109108        return li;
    110109}
    111110unsigned long int ato( const char * ptr ) {
    112111        unsigned long int uli;
    113         if ( sscanf( ptr, "%lu", &uli ) == EOF ) {};            // check return code
     112        if ( sscanf( ptr, "%lu", &uli ) == EOF ) {}                     // check return code
    114113        return uli;
    115114}
    116115long long int ato( const char * ptr ) {
    117116        long long int lli;
    118         if ( sscanf( ptr, "%lld", &lli ) == EOF ) {};           // check return code
     117        if ( sscanf( ptr, "%lld", &lli ) == EOF ) {}            // check return code
    119118        return lli;
    120119}
    121120unsigned long long int ato( const char * ptr ) {
    122121        unsigned long long int ulli;
    123         if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {};          // check return code
     122        if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {}           // check return code
    124123        return ulli;
    125124}
    126125float ato( const char * ptr ) {
    127126        float f;
    128         if ( sscanf( ptr, "%f", &f ) == EOF ) {};                       // check return code
     127        if ( sscanf( ptr, "%f", &f ) == EOF ) {}                        // check return code
    129128        return f;
    130129}
    131130double ato( const char * ptr ) {
    132131        double d;
    133         if ( sscanf( ptr, "%lf", &d ) == EOF ) {};                      // check return code
     132        if ( sscanf( ptr, "%lf", &d ) == EOF ) {}                       // check return code
    134133        return d;
    135134}
    136135long double ato( const char * ptr ) {
    137136        long double ld;
    138         printf( "FRED " );
    139         if ( sscanf( ptr, "%.32Lf", &ld ) == EOF ) {};          // check return code
     137        if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {}                      // check return code
    140138        return ld;
    141139}
    142140float _Complex ato( const char * ptr ) {
    143141        float re, im;
    144         if ( sscanf( ptr, "%g%g", &re, &im ) == EOF ) {};       // check return code
     142        if ( sscanf( ptr, "%g%gi", &re, &im ) == EOF ) {}       // check return code
    145143        return re + im * _Complex_I;
    146144}
    147145double _Complex ato( const char * ptr ) {
    148146        double re, im;
    149         if ( sscanf( ptr, "%.16lg%.16lg", &re, &im ) == EOF ) {}; // check return code
     147        if ( sscanf( ptr, "%lf%lfi", &re, &im ) == EOF ) {} // check return code
    150148        return re + im * _Complex_I;
    151149}
    152150long double _Complex ato( const char * ptr ) {
    153151        long double re, im;
    154         if ( sscanf( ptr, "%.32Lg%.32Lg", &re, &im ) == EOF ) {}; // check return code
     152        if ( sscanf( ptr, "%Lf%Lfi", &re, &im ) == EOF ) {}     // check return code
    155153        return re + im * _Complex_I;
    156154}       
Note: See TracChangeset for help on using the changeset viewer.