Changes in / [5189888:66a2a61]
- Location:
- src
- Files:
-
- 17 edited
-
GenPoly/Box.cc (modified) (16 diffs)
-
GenPoly/Box.h (modified) (1 diff)
-
GenPoly/GenPoly.cc (modified) (2 diffs)
-
GenPoly/GenPoly.h (modified) (2 diffs)
-
SymTab/Validate.cc (modified) (1 diff)
-
SynTree/Mutator.h (modified) (2 diffs)
-
SynTree/Type.h (modified) (4 diffs)
-
examples/Makefile.am (modified) (2 diffs)
-
examples/Makefile.in (modified) (2 diffs)
-
examples/fstream.c (modified) (5 diffs)
-
examples/hello.c (modified) (2 diffs)
-
examples/iostream.c (modified) (4 diffs)
-
examples/iostream.h (modified) (2 diffs)
-
examples/iterator.c (modified) (1 diff)
-
examples/iterator.h (modified) (3 diffs)
-
examples/vector_test.c (modified) (2 diffs)
-
main.cc (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/GenPoly/Box.cc
r5189888 r66a2a61 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : T hu Nov 19 17:40:51201513 // Update Count : 13311 // Last Modified By : Rob Schluntz 12 // Last Modified On : Tue Aug 11 16:22:35 2015 13 // Update Count : 89 14 14 // 15 15 … … 47 47 namespace { 48 48 const std::list<Label> noLabels; 49 50 FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars );51 49 52 50 class Pass1 : public PolyMutator { … … 80 78 ObjectDecl *makeTemporary( Type *type ); 81 79 82 typedef std::map< std::string, DeclarationWithType*> AdapterMap;80 typedef std::map< std::string, FunctionDecl *> AdapterMap; 83 81 std::map< std::string, DeclarationWithType *> assignOps; 84 82 std::stack< AdapterMap > adapters; … … 146 144 } 147 145 146 bool isPolyRet( FunctionType *function, std::string &name, const TyVarMap &otherTyVars ) { 147 bool doTransform = false; 148 if ( ! function->get_returnVals().empty() ) { 149 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( function->get_returnVals().front()->get_type() ) ) { 150 151 // figure out if the return type is specified by a type parameter 152 for ( std::list< TypeDecl *>::const_iterator tyVar = function->get_forall().begin(); tyVar != function->get_forall().end(); ++tyVar ) { 153 if ( (*tyVar)->get_name() == typeInst->get_name() ) { 154 doTransform = true; 155 name = typeInst->get_name(); 156 break; 157 } // if 158 } // for 159 if ( ! doTransform && otherTyVars.find( typeInst->get_name() ) != otherTyVars.end() ) { 160 doTransform = true; 161 } // if 162 } // if 163 } // if 164 return doTransform; 165 } 166 167 bool isPolyRet( FunctionType *function, std::string &name ) { 168 TyVarMap dummyTyVars; 169 return isPolyRet( function, name, dummyTyVars ); 170 } 171 172 bool isPolyRet( FunctionType *function, const TyVarMap &otherTyVars ) { 173 std::string dummyString; 174 return isPolyRet( function, dummyString, otherTyVars ); 175 } 176 148 177 Pass1::Pass1() 149 178 : useRetval( false ), tempNamer( "_temp" ) { 150 adapters.push(AdapterMap());151 179 } 152 180 … … 182 210 183 211 DeclarationWithType *Pass1::mutate( FunctionDecl *functionDecl ) { 184 if ( functionDecl->get_statements() ) { // empty routine body ? 185 doBeginScope(); 212 if ( functionDecl->get_statements() ) { 186 213 TyVarMap oldtyVars = scopeTyVars; 187 214 DeclarationWithType *oldRetval = retval; 188 215 bool oldUseRetval = useRetval; 189 190 // process polymorphic return value 216 191 217 retval = 0; 192 218 std::string typeName; … … 201 227 } // if 202 228 203 FunctionType *functionType = functionDecl->get_functionType(); 229 scopeTyVars.clear(); 230 /// std::cerr << "clear\n"; 204 231 makeTyVarMap( functionDecl->get_functionType(), scopeTyVars ); 205 232 findAssignOps( functionDecl->get_functionType()->get_forall() ); 206 207 std::list< DeclarationWithType *> ¶mList = functionType->get_parameters();208 std::list< FunctionType *> functions;209 for ( std::list< TypeDecl *>::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) {210 for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) {211 findFunction( (*assert)->get_type(), functions, scopeTyVars, needsAdapter );212 } // for213 } // for214 for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) {215 findFunction( (*arg)->get_type(), functions, scopeTyVars, needsAdapter );216 } // for217 AdapterMap & adapters = Pass1::adapters.top();218 for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {219 std::string mangleName = SymTab::Mangler::mangle( *funType );220 if ( isPolyRet( *funType, scopeTyVars ) ) {221 // if the return type involved polymorphic types, then the adapter will need to take those222 // polymorphic types as pointers. Therefore, there can be two different functions with the same223 // mangled name, so we need the mangled names to be different.224 mangleName += "polyret_";225 } // if226 if ( adapters.find( mangleName ) == adapters.end() ) {227 std::string adapterName = makeAdapterName( mangleName );228 adapters.insert( std::pair< std::string, DeclarationWithType *>( mangleName, new ObjectDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) ) );229 } // if230 } // for231 232 233 functionDecl->set_statements( functionDecl->get_statements()->acceptMutator( *this ) ); 233 234 234 235 scopeTyVars = oldtyVars; 235 //std::cerr << "end FunctionDecl: ";236 //for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {237 //std::cerr << i->first << " ";238 //}239 //std::cerr << "\n";236 /// std::cerr << "end FunctionDecl: "; 237 /// for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) { 238 /// std::cerr << i->first << " "; 239 /// } 240 /// std::cerr << "\n"; 240 241 retval = oldRetval; 241 242 useRetval = oldUseRetval; 242 doEndScope();243 // doEndScope(); 243 244 } // if 244 245 return functionDecl; … … 348 349 } // if 349 350 std::string mangleName = SymTab::Mangler::mangle( function ); 350 if ( isPolyRet( function, tyVars ) ) {351 mangleName += "polyret_";352 } // if353 351 std::string adapterName = makeAdapterName( mangleName ); 354 352 … … 459 457 /// return new CastExpr( new VariableExpr( param ), arg->get_type()->clone() ); 460 458 /// } else { 461 if ( dynamic_cast<TypeInstType *>(arg->get_type()) == NULL ) { 462 UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); 463 deref->get_args().push_back( new CastExpr( new VariableExpr( param ), new PointerType( Type::Qualifiers(), arg->get_type()->clone() ) ) ); 464 deref->get_results().push_back( arg->get_type()->clone() ); 465 return deref; 466 } // if 459 UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); 460 deref->get_args().push_back( new CastExpr( new VariableExpr( param ), new PointerType( Type::Qualifiers(), arg->get_type()->clone() ) ) ); 461 deref->get_results().push_back( arg->get_type()->clone() ); 462 return deref; 467 463 /// } 468 464 } // if … … 548 544 } // for 549 545 550 // parameter function types for which an appropriate adapter has been generated. we cannot use the types 551 // after applying substitutions, since two different parameter types may be unified to the same type 546 // parameter function types for which an appropriate adapter has been generated. 547 // we cannot use the types after applying substitutions, since two different 548 // parameter types may be unified to the same type 552 549 std::set< std::string > adaptersDone; 553 550 … … 557 554 std::string mangleName = SymTab::Mangler::mangle( realFunction ); 558 555 559 // only attempt to create an adapter or pass one as a parameter if we haven't already done so for this560 // pre-substitution parameter function type.556 // only attempt to create an adapter or pass one as a parameter if we haven't 557 // already done so for this pre-substitution parameter function type. 561 558 if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) { 559 std::string mangleName = SymTab::Mangler::mangle( realFunction ); 562 560 adaptersDone.insert( adaptersDone.begin(), mangleName ); 563 561 564 // apply substitution to type variables to figure out what the adapter's type should look like 562 // apply substitution to type variables to figure out what the 563 // adapter's type should look like 565 564 assert( env ); 566 565 env->apply( realFunction ); 567 566 mangleName = SymTab::Mangler::mangle( realFunction ); 568 567 569 if ( isPolyRet( originalFunction, exprTyVars ) ) { 570 mangleName += "polyret_"; 568 if ( needsAdapter( realFunction, exprTyVars, true ) ) { 569 // the function still contains type variables, which means we are in a polymorphic 570 // context and the adapter function is a parameter - call the parameter and don't 571 // create a new adapter. 572 appExpr->get_args().push_front( new NameExpr( makeAdapterName ( mangleName ) ) ); 573 } else { 574 if ( isPolyRet( originalFunction, exprTyVars ) ) { 575 // if the return type involved polymorphic types, then 576 // the adapter will need to take those polymorphic types 577 // as pointers. Therefore, there can be two different 578 // functions with the same mangled name, so we need two adapter map 579 // stacks and also we need the mangled names to be different. 580 mangleName += "polyret_"; 581 } 582 583 AdapterMap & adapters = Pass1::adapters.top(); 584 AdapterMap::iterator adapter = adapters.find( mangleName ); 585 if ( adapter == adapters.end() ) { 586 // adapter has not been created yet in the current scope, so define it 587 FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars ); 588 adapter = adapters.insert( adapters.begin(), std::pair< std::string, FunctionDecl *>( mangleName, newAdapter ) ); 589 stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) ); 590 } // if 591 assert( adapter != adapters.end() ); 592 593 // add the appropriate adapter as a parameter 594 appExpr->get_args().push_front( new VariableExpr( adapter->second ) ); 571 595 } // if 572 573 AdapterMap & adapters = Pass1::adapters.top();574 AdapterMap::iterator adapter = adapters.find( mangleName );575 if ( adapter == adapters.end() ) {576 // adapter has not been created yet in the current scope, so define it577 FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars );578 adapter = adapters.insert( adapters.begin(), std::pair< std::string, DeclarationWithType *>( mangleName, newAdapter ) );579 stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) );580 } // if581 assert( adapter != adapters.end() );582 583 // add the appropriate adapter as a parameter584 appExpr->get_args().push_front( new VariableExpr( adapter->second ) );585 596 } // if 586 597 } // for 587 } // passAdapters598 } 588 599 589 600 TypeInstType *isPolyPtr( Type *type, const TypeSubstitution *env, const TyVarMap &tyVars ) { … … 758 769 759 770 Expression *Pass1::mutate( ApplicationExpr *appExpr ) { 760 //std::cerr << "mutate appExpr: ";761 //for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {762 //std::cerr << i->first << " ";763 //}764 //std::cerr << "\n";771 /// std::cerr << "mutate appExpr: "; 772 /// for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) { 773 /// std::cerr << i->first << " "; 774 /// } 775 /// std::cerr << "\n"; 765 776 bool oldUseRetval = useRetval; 766 777 useRetval = false; … … 788 799 ret = addPolyRetParam( appExpr, function, typeName, arg ); 789 800 } else if ( needsAdapter( function, scopeTyVars ) ) { 790 //std::cerr << "needs adapter: ";791 //for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {792 //std::cerr << i->first << " ";793 //}794 //std::cerr << "\n";801 /// std::cerr << "needs adapter: "; 802 /// for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) { 803 /// std::cerr << i->first << " "; 804 /// } 805 /// std::cerr << "\n"; 795 806 // change the application so it calls the adapter rather than the passed function 796 807 ret = applyAdapter( appExpr, function, arg, scopeTyVars ); … … 902 913 // actually, maybe this could (should?) push 903 914 // a copy of the current map 904 adapters.push( adapters.top());915 adapters.push(AdapterMap()); 905 916 } 906 917 … … 924 935 for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) { 925 936 std::string mangleName = SymTab::Mangler::mangle( *funType ); 926 if ( isPolyRet( *funType, scopeTyVars ) ) {927 mangleName += "polyret_";928 } // if929 937 if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) { 930 938 std::string adapterName = makeAdapterName( mangleName ); … … 992 1000 for ( std::list< TypeDecl *>::const_iterator tyParm = funcType->get_forall().begin(); tyParm != funcType->get_forall().end(); ++tyParm ) { 993 1001 ObjectDecl *thisParm; 994 // add all size parameters to parameter list995 1002 if ( (*tyParm)->get_kind() == TypeDecl::Any ) { 996 1003 thisParm = newObj->clone(); … … 999 1006 ++last; 1000 1007 } 1001 // move all assertions into parameter list1002 1008 for ( std::list< DeclarationWithType *>::iterator assert = (*tyParm)->get_assertions().begin(); assert != (*tyParm)->get_assertions().end(); ++assert ) { 1003 1009 /// *assert = (*assert)->acceptMutator( *this ); -
src/GenPoly/Box.h
r5189888 r66a2a61 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : T hu Nov 19 17:24:01201513 // Update Count : 512 // Last Modified On : Tue May 19 07:32:33 2015 13 // Update Count : 2 14 14 // 15 15 -
src/GenPoly/GenPoly.cc
r5189888 r66a2a61 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : T hu Nov 19 17:23:44201513 // Update Count : 1 012 // Last Modified On : Tue May 19 07:37:46 2015 13 // Update Count : 1 14 14 // 15 15 … … 21 21 22 22 namespace GenPoly { 23 // A function needs an adapter if it returns a polymorphic value or if any of its 24 // parameters have polymorphic type 25 bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) { 26 if ( ! adaptee->get_returnVals().empty() && isPolyVal( adaptee->get_returnVals().front()->get_type(), tyVars ) ) { 27 return true; 28 } // if 29 for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); innerArg != adaptee->get_parameters().end(); ++innerArg ) { 30 if ( isPolyVal( (*innerArg)->get_type(), tyVars ) ) { 23 // interface functions 24 bool isPolyVal( Type *type, const TyVarMap &tyVars ) { 25 return isPolyVal( type, tyVars, false ); 26 } 27 28 bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) { 29 return needsAdapter( adaptee, tyVars, false ); 30 } 31 32 bool isPolyVal( Type *type, const TyVarMap &tyVars, bool considerAllTyVars ) { 33 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) { 34 if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) { 31 35 return true; 32 36 } // if 33 } // for 37 return considerAllTyVars; 38 } // if 34 39 return false; 35 40 } 36 41 37 bool isPolyRet( FunctionType *function, std::string &name, const TyVarMap &otherTyVars ) { 38 bool doTransform = false; 39 if ( ! function->get_returnVals().empty() ) { 40 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( function->get_returnVals().front()->get_type() ) ) { 41 42 // figure out if the return type is specified by a type parameter 43 for ( std::list< TypeDecl *>::const_iterator tyVar = function->get_forall().begin(); tyVar != function->get_forall().end(); ++tyVar ) { 44 if ( (*tyVar)->get_name() == typeInst->get_name() ) { 45 doTransform = true; 46 name = typeInst->get_name(); 47 break; 48 } // if 49 } // for 50 if ( ! doTransform && otherTyVars.find( typeInst->get_name() ) != otherTyVars.end() ) { 51 doTransform = true; 52 } // if 42 // A function needs an adapter if it returns a polymorphic value or if any of its 43 // parameters have polymorphic type 44 bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars, bool considerAllTyVars ) { 45 bool needsAdapter = false; 46 if ( ! adaptee->get_returnVals().empty() && isPolyVal( adaptee->get_returnVals().front()->get_type(), tyVars, considerAllTyVars ) ) { 47 needsAdapter = true; 48 } // if 49 for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); ! needsAdapter && innerArg != adaptee->get_parameters().end(); ++innerArg ) { 50 if ( isPolyVal( (*innerArg)->get_type(), tyVars, considerAllTyVars ) ) { 51 needsAdapter = true; 53 52 } // if 54 } // if 55 return doTransform; 56 } 57 58 bool isPolyRet( FunctionType *function, std::string &name ) { 59 TyVarMap dummyTyVars; 60 return isPolyRet( function, name, dummyTyVars ); 61 } 62 63 bool isPolyRet( FunctionType *function, const TyVarMap &otherTyVars ) { 64 std::string dummyString; 65 return isPolyRet( function, dummyString, otherTyVars ); 66 } 67 68 bool isPolyVal( Type *type, const TyVarMap &tyVars ) { 69 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) { 70 if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) { 71 return true; 72 } // if 73 } // if 74 return false; 53 } // for 54 return needsAdapter; 75 55 } 76 56 -
src/GenPoly/GenPoly.h
r5189888 r66a2a61 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : T hu Nov 19 17:24:03201513 // Update Count : 412 // Last Modified On : Tue May 19 07:38:34 2015 13 // Update Count : 1 14 14 // 15 15 … … 25 25 typedef std::map< std::string, TypeDecl::Kind > TyVarMap; 26 26 27 // considerAllTyVars allows ignoring the contents of the TyVarMap parameter, for the situations where 28 // it is important only that a TypeInstType node exists. 29 27 30 bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVarr ); 28 bool isPolyRet( FunctionType *function, std::string &name, const TyVarMap &otherTyVars ); 29 bool isPolyRet( FunctionType *function, std::string &name ); 30 bool isPolyRet( FunctionType *function, const TyVarMap &otherTyVars ); 31 // bool isPolyFun( FunctionType *fun, const TyVarMap &tyVars ); 31 bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars, bool considerAllTyVars ); 32 bool isPolyFun( FunctionType *fun, const TyVarMap &tyVars ); 32 33 bool isPolyVal( Type *type, const TyVarMap &tyVars ); 34 bool isPolyVal( Type *type, const TyVarMap &tyVars, bool considerAllTyVars ); 33 35 void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ); 34 36 } // namespace GenPoly -
src/SymTab/Validate.cc
r5189888 r66a2a61 10 10 // Created On : Sun May 17 21:50:04 2015 11 11 // Last Modified By : Rob Schluntz 12 // Last Modified On : Fri Nov 20 16:33:52201513 // Update Count : 20112 // Last Modified On : Thu Nov 19 10:44:55 2015 13 // Update Count : 199 14 14 // 15 15 -
src/SynTree/Mutator.h
r5189888 r66a2a61 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Nov 19 22:26:16201513 // Update Count : 812 // Last Modified On : Thu Jul 23 23:24:18 2015 13 // Update Count : 7 14 14 // 15 15 #include <cassert> … … 103 103 inline TreeType *maybeMutate( TreeType *tree, MutatorType &mutator ) { 104 104 if ( tree ) { 105 TreeType *newnode = dynamic_cast< TreeType * >( tree->acceptMutator( mutator ) );105 TreeType *newnode = dynamic_cast< TreeType* >( tree->acceptMutator( mutator ) ); 106 106 assert( newnode ); 107 107 return newnode; -
src/SynTree/Type.h
r5189888 r66a2a61 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Nov 20 12:54:09201513 // Update Count : 1 512 // Last Modified On : Thu Jul 9 16:46:15 2015 13 // Update Count : 14 14 14 // 15 15 … … 201 201 std::list<DeclarationWithType*> parameters; 202 202 203 // Does the function accept a variable number of arguments following the arguments specified in the parameters list.204 // This could be because of203 // does the function accept a variable number of arguments following the arguments 204 // specified in the parameters list. This could be because of 205 205 // - an ellipsis in a prototype declaration 206 206 // - an unprototyped declaration … … 239 239 240 240 /// Accesses generic parameters of base struct (NULL if none such) 241 std::list<TypeDecl*> * get_baseParameters();241 std::list<TypeDecl*>* get_baseParameters(); 242 242 243 243 /// Looks up the members of this struct named "name" and places them into "foundDecls". … … 266 266 267 267 /// Accesses generic parameters of base union (NULL if none such) 268 std::list<TypeDecl*> * get_baseParameters();268 std::list<TypeDecl*>* get_baseParameters(); 269 269 270 270 /// looks up the members of this union named "name" and places them into "foundDecls" -
src/examples/Makefile.am
r5189888 r66a2a61 11 11 ## Created On : Sun May 31 09:08:15 2015 12 12 ## Last Modified By : Peter A. Buhr 13 ## Last Modified On : Fri Nov 20 16:03:46201514 ## Update Count : 2 413 ## Last Modified On : Thu Jun 4 23:13:10 2015 14 ## Update Count : 22 15 15 ############################################################################### 16 16 … … 19 19 CC = @CFA_BINDIR@/cfa 20 20 21 noinst_PROGRAMS = fstream_test vector_test # build but do not install22 fstream_test_SOURCES = iostream.c fstream.c fstream_test.c iterator.c21 noinst_PROGRAMS = fstream_test vector_test 22 fstream_test_SOURCES = iostream.c fstream.c fstream_test.c 23 23 vector_test_SOURCES = vector_int.c fstream.c iostream.c array.c iterator.c vector_test.c -
src/examples/Makefile.in
r5189888 r66a2a61 49 49 PROGRAMS = $(noinst_PROGRAMS) 50 50 am_fstream_test_OBJECTS = iostream.$(OBJEXT) fstream.$(OBJEXT) \ 51 fstream_test.$(OBJEXT) iterator.$(OBJEXT)51 fstream_test.$(OBJEXT) 52 52 fstream_test_OBJECTS = $(am_fstream_test_OBJECTS) 53 53 fstream_test_LDADD = $(LDADD) … … 176 176 top_builddir = @top_builddir@ 177 177 top_srcdir = @top_srcdir@ 178 fstream_test_SOURCES = iostream.c fstream.c fstream_test.c iterator.c178 fstream_test_SOURCES = iostream.c fstream.c fstream_test.c 179 179 vector_test_SOURCES = vector_int.c fstream.c iostream.c array.c iterator.c vector_test.c 180 180 all: all-am -
src/examples/fstream.c
r5189888 r66a2a61 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Nov 19 22:43:31201513 // Update Count : 412 // Last Modified On : Wed May 27 18:12:33 2015 13 // Update Count : 2 14 14 // 15 15 … … 30 30 fwrite( data, size, 1, os->file ); 31 31 os->fail = ferror( os->file ); 32 } // if32 } 33 33 return os; 34 } // write34 } 35 35 36 36 int fail( ofstream *os ) { 37 37 return os->fail; 38 } // fail38 } 39 39 40 40 static ofstream *make_ofstream() { … … 42 42 new_stream->fail = 0; 43 43 return new_stream; 44 } // make_ofstream44 } 45 45 46 46 ofstream *ofstream_stdout() { … … 48 48 stdout_stream->file = stdout; 49 49 return stdout_stream; 50 } // ofstream_stdout50 } 51 51 52 52 ofstream *ofstream_stderr() { … … 54 54 stderr_stream->file = stderr; 55 55 return stderr_stream; 56 } // ofstream_stderr56 } 57 57 58 58 ofstream *ofstream_fromfile( const char *name ) { -
src/examples/hello.c
r5189888 r66a2a61 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Nov 20 16:02:50201513 // Update Count : 312 // Last Modified On : Wed May 27 18:14:58 2015 13 // Update Count : 1 14 14 // 15 15 … … 28 28 // Local Variables: // 29 29 // tab-width: 4 // 30 // compile-command: "cfa hello.c fstream.o iostream.o iterator.o" //30 // compile-command: "cfa hello.c fstream.o iostream.o" // 31 31 // End: // -
src/examples/iostream.c
r5189888 r66a2a61 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Nov 20 13:19:19201513 // Update Count : 912 // Last Modified On : Wed May 27 18:18:13 2015 13 // Update Count : 2 14 14 // 15 15 … … 17 17 extern "C" { 18 18 #include <stdio.h> 19 #include <string.h> // strlen 19 //#include <string.h> 20 //#include <ctype.h> 21 typedef long unsigned int size_t; 22 size_t strlen(const char *s); 20 23 } 21 24 … … 23 26 ostype * ?<<?( ostype *os, char c ) { 24 27 return write( os, &c, 1 ); 25 } // ?<<?28 } 26 29 27 30 forall( dtype ostype | ostream( ostype ) ) 28 31 ostype * ?<<?( ostype *os, int i ) { 29 char buffer[ 32];// larger than the largest integer32 char buffer[20]; // larger than the largest integer 30 33 sprintf( buffer, "%d", i ); 31 34 return write( os, buffer, strlen( buffer ) ); 32 } // ?<<?35 } 33 36 34 37 forall( dtype ostype | ostream( ostype ) ) 35 38 ostype * ?<<?( ostype *os, double d ) { 36 char buffer[32]; // larger than the largest double39 char buffer[32]; // larger than the largest double 37 40 sprintf( buffer, "%g", d ); 38 41 return write( os, buffer, strlen( buffer ) ); 39 } // ?<<?42 } 40 43 41 44 forall( dtype ostype | ostream( ostype ) ) 42 45 ostype * ?<<?( ostype *os, const char *cp ) { 43 46 return write( os, cp, strlen( cp ) ); 44 } // ?<<? 45 46 forall( dtype ostype | ostream( ostype ) ) 47 ostype * ?<<?( ostype *os, const void *p ) { 48 char buffer[32]; // larger than the largest pointer 49 sprintf( buffer, "%p", p ); 50 return write( os, buffer, strlen( buffer ) ); 51 } // ?<<? 52 53 forall( type elt_type | writeable( elt_type ), 54 type iterator_type | iterator( iterator_type, elt_type ), 55 dtype os_type | ostream( os_type ) ) 56 void write( iterator_type begin, iterator_type end, os_type *os ) { 57 void print( elt_type i ) { 58 os << i << ' '; 59 } 60 for_each( begin, end, print ); 61 } // ?<<? 62 63 forall( type elt_type | writeable( elt_type ), 64 type iterator_type | iterator( iterator_type, elt_type ), 65 dtype os_type | ostream( os_type ) ) 66 void write_reverse( iterator_type begin, iterator_type end, os_type *os ) { 67 void print( elt_type i ) { 68 os << i << ' '; 69 } 70 for_each_reverse( begin, end, print ); 71 } // ?<<? 72 47 } 73 48 74 49 forall( dtype istype | istream( istype ) ) 75 50 istype * ?>>?( istype *is, char *cp ) { 76 51 return read( is, cp, 1 ); 77 } // ?>>?52 } 78 53 79 54 forall( dtype istype | istream( istype ) ) … … 97 72 unread( is, cur ); 98 73 return is; 99 } // ?>>?74 } 100 75 101 76 // Local Variables: // -
src/examples/iostream.h
r5189888 r66a2a61 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Nov 19 17:56:51201513 // Update Count : 512 // Last Modified On : Wed May 27 18:18:46 2015 13 // Update Count : 1 14 14 // 15 15 16 16 #ifndef IOSTREAM_H 17 17 #define IOSTREAM_H 18 19 #include "iterator.h"20 18 21 19 typedef unsigned long streamsize_type; … … 36 34 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, double ); 37 35 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, const char * ); 38 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, void * );39 40 // writes the range [begin, end) to the given stream41 forall( type elt_type | writeable( elt_type ),42 type iterator_type | iterator( iterator_type, elt_type ),43 dtype os_type | ostream( os_type ) )44 void write( iterator_type begin, iterator_type end, os_type *os );45 46 forall( type elt_type | writeable( elt_type ),47 type iterator_type | iterator( iterator_type, elt_type ),48 dtype os_type | ostream( os_type ) )49 void write_reverse( iterator_type begin, iterator_type end, os_type *os );50 36 51 37 -
src/examples/iterator.c
r5189888 r66a2a61 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Nov 19 17:54:37201513 // Update Count : 2412 // Last Modified On : Wed May 27 18:41:41 2015 13 // Update Count : 3 14 14 // 15 15 16 16 #include "iterator.h" 17 17 18 forall( type iterator_type, type elt_type | iterator( iterator_type, elt_type ) ) 19 void for_each( iterator_type begin, iterator_type end, void (*func)( elt_type ) ) { 20 for ( iterator_type i = begin; i != end; ++i ) { 21 func( *i ); 18 /// forall( type iterator_type, type elt_type | iterator( iterator_type, elt_type ) ) 19 /// void 20 /// for_each( iterator_type begin, iterator_type end, void (*func)( elt_type ) ) 21 /// { 22 /// iterator_type i; 23 /// for ( i = begin; i != end; ++i ) { 24 /// func( *i ); 25 /// } 26 /// } 27 28 forall( type elt_type | writeable( elt_type ), 29 type iterator_type | iterator( iterator_type, elt_type ), 30 dtype os_type | ostream( os_type ) ) 31 void write_all( iterator_type begin, iterator_type end, os_type *os ) { 32 iterator_type i; 33 for ( i = begin; i != end; ++i ) { 34 os << *i << ' '; 22 35 } 23 36 } 24 37 25 forall( type iterator_type, type elt_type | iterator( iterator_type, elt_type ) ) 26 void for_each_reverse( iterator_type begin, iterator_type end, void (*func)( elt_type ) ) { 27 for ( iterator_type i = end; i != begin; ) { 38 forall( type elt_type | writeable( elt_type ), 39 type iterator_type | iterator( iterator_type, elt_type ), 40 dtype os_type | ostream( os_type ) ) 41 void write_reverse( iterator_type begin, iterator_type end, os_type *os ) { 42 iterator_type i; // "= end;" does not work 43 i = end; 44 do { 28 45 --i; 29 func( *i );30 } 46 os << *i << ' '; 47 } while ( i != begin ); 31 48 } 32 49 -
src/examples/iterator.h
r5189888 r66a2a61 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Nov 19 17:58:28201513 // Update Count : 612 // Last Modified On : Wed May 27 18:41:57 2015 13 // Update Count : 3 14 14 // 15 15 16 16 #ifndef ITERATOR_H 17 17 #define ITERATOR_H 18 19 #include "iostream.h" 18 20 19 21 // An iterator can be used to traverse a data structure. … … 32 34 }; 33 35 34 context iterator_for ( type iterator_type, type collection_type, type elt_type | iterator( iterator_type, elt_type ) ) {36 context iterator_for ( type iterator_type, type collection_type, type elt_type | iterator( iterator_type, elt_type ) ) { 35 37 // [ iterator_type begin, iterator_type end ] get_iterators( collection_type ); 36 38 iterator_type begin( collection_type ); … … 41 43 void for_each( iterator_type begin, iterator_type end, void (*func)( elt_type ) ); 42 44 43 forall( type iterator_type, type elt_type | iterator( iterator_type, elt_type ) ) 44 void for_each_reverse( iterator_type begin, iterator_type end, void (*func)( elt_type ) ); 45 // writes the range [begin, end) to the given stream 46 forall( type elt_type | writeable( elt_type ), 47 type iterator_type | iterator( iterator_type, elt_type ), 48 dtype os_type | ostream( os_type ) ) 49 void write_all( iterator_type begin, iterator_type end, os_type *os ); 50 51 forall( type elt_type | writeable( elt_type ), 52 type iterator_type | iterator( iterator_type, elt_type ), 53 dtype os_type | ostream( os_type ) ) 54 void write_reverse( iterator_type begin, iterator_type end, os_type *os ); 45 55 46 56 #endif // ITERATOR_H -
src/examples/vector_test.c
r5189888 r66a2a61 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Nov 19 17:54:34201513 // Update Count : 912 // Last Modified On : Wed May 27 18:42:55 2015 13 // Update Count : 2 14 14 // 15 15 … … 29 29 sout << "enter N elements and C-d on a separate line:\n"; 30 30 for ( ;; ) { 31 sin >> #31 sin >> # 32 32 if ( fail( sin ) || eof( sin ) ) break; 33 append( &vec, num );33 append( &vec, num ); 34 34 } 35 35 // write out the numbers 36 36 37 37 sout << "Array elements:\n"; 38 write( begin( vec ), end( vec ), sout ); 38 // write_all( begin( vec ), end( vec ), sout ); 39 // sout << "\n"; 40 for ( int index = 0; index <= last( vec ); index += 1 ) { 41 sout << vec[ index ] << " "; 42 } 39 43 sout << "\n"; 40 44 #if 1 41 45 sout << "Array elements reversed:\n"; 42 46 write_reverse( begin( vec ), end( vec ), sout ); 43 47 sout << "\n"; 48 #endif 44 49 } 50 51 // ../bin/cfa vector_test.c fstream.o iostream.o vector_int.o iterator.o array.o 45 52 46 53 // Local Variables: // -
src/main.cc
r5189888 r66a2a61 9 9 // Author : Richard C. Bilson 10 10 // Created On : Fri May 15 23:12:02 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Thu Nov 19 22:31:40201513 // Update Count : 16 811 // Last Modified By : Rob Schluntz 12 // Last Modified On : Thu Jul 30 16:08:18 2015 13 // Update Count : 167 14 14 // 15 15 … … 227 227 } // if 228 228 229 // add the assignment statement after the initialization of a type parameter 229 // add the assignment statement after the 230 // initialization of a type parameter 230 231 OPTPRINT( "validate" ) 231 232 SymTab::validate( translationUnit, symtabp );
Note:
See TracChangeset
for help on using the changeset viewer.