Changeset 4da152a
- Timestamp:
- Nov 8, 2022, 4:12:00 PM (2 years ago)
- Branches:
- ADT, ast-experimental, master
- Children:
- 6411b7d
- Parents:
- 44547b0
- Location:
- src/GenPoly
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/GenPoly/Box.cc
r44547b0 r4da152a 58 58 namespace GenPoly { 59 59 namespace { 60 FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars );60 FunctionType *makeAdapterType( FunctionType const *adaptee, const TyVarMap &tyVars ); 61 61 62 62 class BoxPass { … … 124 124 /// `mangleName` as the base name for the adapter. `tyVars` is the map of 125 125 /// type variables for the function type of the adapted expression. 126 FunctionDecl *makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars );126 FunctionDecl *makeAdapter( FunctionType const *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ); 127 127 /// Replaces intrinsic operator functions with their arithmetic desugaring 128 128 Expression *handleIntrinsics( ApplicationExpr *appExpr ); … … 190 190 ObjectDecl *makeVar( const std::string &name, Type *type, Initializer *init = 0 ); 191 191 /// returns true if the type has a dynamic layout; such a layout will be stored in appropriately-named local variables when the function returns 192 bool findGeneric( Type *ty );192 bool findGeneric( Type const *ty ); 193 193 /// adds type parameters to the layout call; will generate the appropriate parameters if needed 194 194 void addOtypeParamsToLayoutCall( UntypedExpr *layoutCall, const std::list< Type* > &otypeParams ); … … 426 426 427 427 namespace { 428 std::string makePolyMonoSuffix( FunctionType * function, const TyVarMap &tyVars ) {428 std::string makePolyMonoSuffix( FunctionType const * function, const TyVarMap &tyVars ) { 429 429 std::stringstream name; 430 430 … … 435 435 // to take those polymorphic types as pointers. Therefore, there can be two different functions 436 436 // with the same mangled name, so we need to further mangle the names. 437 for ( DeclarationWithType * const ret : function->get_returnVals()) {437 for ( DeclarationWithType const * const ret : function->returnVals ) { 438 438 if ( isPolyType( ret->get_type(), tyVars ) ) { 439 439 name << "P"; … … 443 443 } 444 444 name << "_"; 445 for ( DeclarationWithType * const arg : function->get_parameters()) {445 for ( DeclarationWithType const * const arg : function->parameters ) { 446 446 if ( isPolyType( arg->get_type(), tyVars ) ) { 447 447 name << "P"; … … 453 453 } 454 454 455 std::string mangleAdapterName( FunctionType * function, const TyVarMap &tyVars ) {455 std::string mangleAdapterName( FunctionType const * function, const TyVarMap &tyVars ) { 456 456 return SymTab::Mangler::mangle( function ) + makePolyMonoSuffix( function, tyVars ); 457 457 } … … 489 489 490 490 std::list< DeclarationWithType *> ¶mList = functionType->parameters; 491 std::list< FunctionType *> functions;491 std::list< FunctionType const *> functions; 492 492 for ( TypeDecl * const tyVar : functionType->forall ) { 493 493 for ( DeclarationWithType * const assert : tyVar->assertions ) { … … 499 499 } // for 500 500 501 for ( FunctionType * const funType : functions ) {501 for ( FunctionType const * const funType : functions ) { 502 502 std::string mangleName = mangleAdapterName( funType, scopeTyVars ); 503 503 if ( adapters.find( mangleName ) == adapters.end() ) { … … 794 794 } 795 795 796 FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars ) {796 FunctionType *makeAdapterType( FunctionType const *adaptee, const TyVarMap &tyVars ) { 797 797 // actually make the adapter type 798 798 FunctionType *adapter = adaptee->clone(); … … 804 804 } 805 805 806 Expression *makeAdapterArg( DeclarationWithType *param, DeclarationWithType *arg, DeclarationWithType *realParam, const TyVarMap &tyVars ) { 806 Expression *makeAdapterArg( 807 DeclarationWithType *param, 808 DeclarationWithType const *arg, 809 DeclarationWithType const *realParam, 810 const TyVarMap &tyVars ) { 807 811 assert( param ); 808 812 assert( arg ); … … 817 821 } 818 822 819 void addAdapterParams( ApplicationExpr *adapteeApp, std::list< DeclarationWithType *>::iterator arg, std::list< DeclarationWithType *>::iterator param, std::list< DeclarationWithType *>::iterator paramEnd, std::list< DeclarationWithType *>::iterator realParam, const TyVarMap &tyVars ) { 823 void addAdapterParams( 824 ApplicationExpr *adapteeApp, 825 std::list< DeclarationWithType *>::const_iterator arg, 826 std::list< DeclarationWithType *>::const_iterator param, 827 std::list< DeclarationWithType *>::const_iterator paramEnd, 828 std::list< DeclarationWithType *>::const_iterator realParam, 829 const TyVarMap &tyVars ) { 820 830 UniqueName paramNamer( "_p" ); 821 831 for ( ; param != paramEnd; ++param, ++arg, ++realParam ) { … … 828 838 } 829 839 830 FunctionDecl *Pass1::makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ) {840 FunctionDecl *Pass1::makeAdapter( FunctionType const *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ) { 831 841 FunctionType *adapterType = makeAdapterType( adaptee, tyVars ); 832 842 adapterType = ScrubTyVars::scrub( adapterType, tyVars ); … … 845 855 Statement *bodyStmt; 846 856 847 Type::ForallList:: iterator tyArg = realType->get_forall().begin();848 Type::ForallList:: iterator tyParam = adapterType->get_forall().begin();849 Type::ForallList:: iterator realTyParam = adaptee->get_forall().begin();857 Type::ForallList::const_iterator tyArg = realType->forall.begin(); 858 Type::ForallList::const_iterator tyParam = adapterType->forall.begin(); 859 Type::ForallList::const_iterator realTyParam = adaptee->forall.begin(); 850 860 for ( ; tyParam != adapterType->get_forall().end(); ++tyArg, ++tyParam, ++realTyParam ) { 851 861 assert( tyArg != realType->get_forall().end() ); 852 std::list< DeclarationWithType *>:: iterator assertArg = (*tyArg)->get_assertions().begin();853 std::list< DeclarationWithType *>:: iterator assertParam = (*tyParam)->get_assertions().begin();854 std::list< DeclarationWithType *>:: iterator realAssertParam = (*realTyParam)->get_assertions().begin();862 std::list< DeclarationWithType *>::const_iterator assertArg = (*tyArg)->get_assertions().begin(); 863 std::list< DeclarationWithType *>::const_iterator assertParam = (*tyParam)->get_assertions().begin(); 864 std::list< DeclarationWithType *>::const_iterator realAssertParam = (*realTyParam)->get_assertions().begin(); 855 865 for ( ; assertParam != (*tyParam)->get_assertions().end(); ++assertArg, ++assertParam, ++realAssertParam ) { 856 866 assert( assertArg != (*tyArg)->get_assertions().end() ); … … 859 869 } // for 860 870 861 std::list< DeclarationWithType *>:: iterator arg = realType->get_parameters().begin();862 std::list< DeclarationWithType *>:: iterator param = adapterType->get_parameters().begin();863 std::list< DeclarationWithType *>:: iterator realParam = adaptee->get_parameters().begin();871 std::list< DeclarationWithType *>::const_iterator arg = realType->parameters.begin(); 872 std::list< DeclarationWithType *>::const_iterator param = adapterType->parameters.begin(); 873 std::list< DeclarationWithType *>::const_iterator realParam = adaptee->parameters.begin(); 864 874 param++; // skip adaptee parameter in the adapter type 865 875 if ( realType->get_returnVals().empty() ) { … … 867 877 addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars ); 868 878 bodyStmt = new ExprStmt( adapteeApp ); 869 } else if ( isDynType( adaptee-> get_returnVals().front()->get_type(), tyVars ) ) {879 } else if ( isDynType( adaptee->returnVals.front()->get_type(), tyVars ) ) { 870 880 // return type T 871 881 if ( (*param)->get_name() == "" ) { … … 892 902 void Pass1::passAdapters( ApplicationExpr * appExpr, FunctionType * functionType, const TyVarMap & exprTyVars ) { 893 903 // collect a list of function types passed as parameters or implicit parameters (assertions) 894 std::list< FunctionType*> functions;904 std::list<FunctionType const *> functions; 895 905 for ( TypeDecl * const tyVar : functionType->get_forall() ) { 896 906 for ( DeclarationWithType * const assert : tyVar->get_assertions() ) { … … 906 916 std::set< std::string > adaptersDone; 907 917 908 for ( FunctionType * const funType : functions ) {918 for ( FunctionType const * const funType : functions ) { 909 919 FunctionType *originalFunction = funType->clone(); 910 920 FunctionType *realFunction = funType->clone(); … … 940 950 941 951 Expression *makeIncrDecrExpr( ApplicationExpr *appExpr, Type *polyType, bool isIncr ) { 942 NameExpr *opExpr; 943 if ( isIncr ) { 944 opExpr = new NameExpr( "?+=?" ); 945 } else { 946 opExpr = new NameExpr( "?-=?" ); 947 } // if 952 NameExpr *opExpr = new NameExpr( ( isIncr ) ? "?+=?" : "?-=?" ); 948 953 UntypedExpr *addAssign = new UntypedExpr( opExpr ); 949 954 if ( AddressExpr *address = dynamic_cast< AddressExpr *>( appExpr->get_args().front() ) ) { … … 1146 1151 } 1147 1152 1148 bool isPolyDeref( UntypedExpr * expr, TyVarMap const & scopeTyVars, TypeSubstitution const * env ) {1153 bool isPolyDeref( UntypedExpr const * expr, TyVarMap const & scopeTyVars, TypeSubstitution const * env ) { 1149 1154 if ( expr->result && isPolyType( expr->result, scopeTyVars, env ) ) { 1150 if ( NameExpr *name = dynamic_cast< NameExpr*>( expr->function ) ) {1155 if ( auto name = dynamic_cast<NameExpr const *>( expr->function ) ) { 1151 1156 if ( name->name == "*?" ) { 1152 1157 return true; … … 1229 1234 void Pass2::addAdapters( FunctionType *functionType ) { 1230 1235 std::list< DeclarationWithType *> ¶mList = functionType->parameters; 1231 std::list< FunctionType *> functions;1236 std::list< FunctionType const *> functions; 1232 1237 for ( DeclarationWithType * const arg : functionType->parameters ) { 1233 1238 Type *orig = arg->get_type(); … … 1236 1241 } 1237 1242 std::set< std::string > adaptersDone; 1238 for ( FunctionType * const funType : functions ) {1243 for ( FunctionType const * const funType : functions ) { 1239 1244 std::string mangleName = mangleAdapterName( funType, scopeTyVars ); 1240 1245 if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) { … … 1471 1476 1472 1477 /// converts polymorphic type T into a suitable monomorphic representation, currently: __attribute__((aligned(8)) char[size_T] 1473 Type * polyToMonoType( Type * declType ) {1478 Type * polyToMonoType( Type const * declType ) { 1474 1479 Type * charType = new BasicType( Type::Qualifiers(), BasicType::Kind::Char); 1475 1480 Expression * size = new NameExpr( sizeofName( mangleType(declType) ) ); … … 1568 1573 1569 1574 /// Returns an index expression into the offset array for a type 1570 Expression *makeOffsetIndex( Type *objectType, long i ) {1575 Expression *makeOffsetIndex( Type const *objectType, long i ) { 1571 1576 ConstantExpr *fieldIndex = new ConstantExpr( Constant::from_ulong( i ) ); 1572 1577 UntypedExpr *fieldOffset = new UntypedExpr( new NameExpr( "?[?]" ) ); … … 1675 1680 1676 1681 /// returns true if any of the otype parameters have a dynamic layout and puts all otype parameters in the output list 1677 bool findGenericParams( std::list< TypeDecl* > &baseParams, std::list< Expression* >&typeParams, std::list< Type* > &out ) {1682 bool findGenericParams( std::list< TypeDecl* > const &baseParams, std::list< Expression* > const &typeParams, std::list< Type* > &out ) { 1678 1683 bool hasDynamicLayout = false; 1679 1684 … … 1695 1700 } 1696 1701 1697 bool PolyGenericCalculator::findGeneric( Type *ty ) {1702 bool PolyGenericCalculator::findGeneric( Type const *ty ) { 1698 1703 ty = replaceTypeInst( ty, env ); 1699 1704 1700 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( ty ) ) {1705 if ( auto typeInst = dynamic_cast< TypeInstType const * >( ty ) ) { 1701 1706 if ( scopeTyVars.find( typeInst->get_name() ) != scopeTyVars.end() ) { 1702 1707 // NOTE assumes here that getting put in the scopeTyVars included having the layout variables set … … 1704 1709 } 1705 1710 return false; 1706 } else if ( StructInstType *structTy = dynamic_cast< StructInstType* >( ty ) ) {1711 } else if ( auto structTy = dynamic_cast< StructInstType const * >( ty ) ) { 1707 1712 // check if this type already has a layout generated for it 1708 1713 std::string typeName = mangleType( ty ); … … 1711 1716 // check if any of the type parameters have dynamic layout; if none do, this type is (or will be) monomorphized 1712 1717 std::list< Type* > otypeParams; 1713 if ( ! findGenericParams( *structTy->get_baseParameters(), structTy-> get_parameters(), otypeParams ) ) return false;1718 if ( ! findGenericParams( *structTy->get_baseParameters(), structTy->parameters, otypeParams ) ) return false; 1714 1719 1715 1720 // insert local variables for layout and generate call to layout function … … 1741 1746 1742 1747 return true; 1743 } else if ( UnionInstType *unionTy = dynamic_cast< UnionInstType* >( ty ) ) {1748 } else if ( auto unionTy = dynamic_cast< UnionInstType const * >( ty ) ) { 1744 1749 // check if this type already has a layout generated for it 1745 1750 std::string typeName = mangleType( ty ); … … 1748 1753 // check if any of the type parameters have dynamic layout; if none do, this type is (or will be) monomorphized 1749 1754 std::list< Type* > otypeParams; 1750 if ( ! findGenericParams( *unionTy->get_baseParameters(), unionTy-> get_parameters(), otypeParams ) ) return false;1755 if ( ! findGenericParams( *unionTy->get_baseParameters(), unionTy->parameters, otypeParams ) ) return false; 1751 1756 1752 1757 // insert local variables for layout and generate call to layout function -
src/GenPoly/FindFunction.cc
r44547b0 r4da152a 29 29 class FindFunction : public WithGuards, public WithVisitorRef<FindFunction>, public WithShortCircuiting { 30 30 public: 31 FindFunction( std::list< FunctionType * > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate );31 FindFunction( std::list< FunctionType const* > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate ); 32 32 33 33 void premutate( FunctionType * functionType ); … … 37 37 void handleForall( const Type::ForallList &forall ); 38 38 39 std::list< FunctionType * > &functions;39 std::list< FunctionType const * > & functions; 40 40 TyVarMap tyVars; 41 41 bool replaceMode; … … 43 43 }; 44 44 45 void findFunction( Type *type, std::list< FunctionType * > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) {45 void findFunction( Type *type, std::list< FunctionType const * > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) { 46 46 PassVisitor<FindFunction> finder( functions, tyVars, false, predicate ); 47 47 type->acceptMutator( finder ); 48 48 } 49 49 50 void findAndReplaceFunction( Type *&type, std::list< FunctionType * > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) {50 void findAndReplaceFunction( Type *&type, std::list< FunctionType const * > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) { 51 51 PassVisitor<FindFunction> finder( functions, tyVars, true, predicate ); 52 52 type = type->acceptMutator( finder ); 53 53 } 54 54 55 FindFunction::FindFunction( std::list< FunctionType * > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate )55 FindFunction::FindFunction( std::list< FunctionType const * > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate ) 56 56 : functions( functions ), tyVars( tyVars ), replaceMode( replaceMode ), predicate( predicate ) { 57 57 } -
src/GenPoly/FindFunction.h
r44547b0 r4da152a 27 27 28 28 /// recursively walk `type`, placing all functions that match `predicate` under `tyVars` into `functions` 29 void findFunction( Type *type, std::list< FunctionType * > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate );29 void findFunction( Type *type, std::list< FunctionType const * > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ); 30 30 /// like `findFunction`, but also replaces the function type with void ()(void) 31 void findAndReplaceFunction( Type *&type, std::list< FunctionType * > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate );31 void findAndReplaceFunction( Type *&type, std::list< FunctionType const * > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ); 32 32 } // namespace GenPoly 33 33 -
src/GenPoly/GenPoly.cc
r44547b0 r4da152a 112 112 if ( ! env ) return type; 113 113 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) { 114 Type *newType = env->lookup( typeInst->get_name() ); 115 if ( newType ) return newType; 116 } 117 return type; 118 } 119 120 const Type* replaceTypeInst( const Type* type, const TypeSubstitution* env ) { 121 if ( ! env ) return type; 122 if ( auto typeInst = dynamic_cast< const TypeInstType* >( type ) ) { 114 123 Type *newType = env->lookup( typeInst->get_name() ); 115 124 if ( newType ) return newType; -
src/GenPoly/GenPoly.h
r44547b0 r4da152a 34 34 /// Replaces a TypeInstType by its referrent in the environment, if applicable 35 35 Type* replaceTypeInst( Type* type, const TypeSubstitution* env ); 36 const Type* replaceTypeInst( const Type* type, const TypeSubstitution* env ); 36 37 const ast::Type * replaceTypeInst( const ast::Type *, const ast::TypeSubstitution * ); 37 38 … … 116 117 117 118 /// Gets the mangled name of this type; alias for SymTab::Mangler::mangleType(). 118 inline std::string mangleType( Type *ty ) { return SymTab::Mangler::mangleType( ty ); }119 inline std::string mangleType( const Type *ty ) { return SymTab::Mangler::mangleType( ty ); } 119 120 120 121 /// Gets the name of the sizeof parameter for the type, given its mangled name
Note: See TracChangeset
for help on using the changeset viewer.