Changeset cdec5af
- Timestamp:
- Dec 14, 2015, 1:54:46 PM (9 years ago)
- 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, string, with_gc
- Children:
- 56fcd77
- Parents:
- 63db3d76
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/GenPoly/Box.cc
r63db3d76 rcdec5af 14 14 // 15 15 16 #include <map> 16 17 #include <set> 18 #include <sstream> 17 19 #include <stack> 18 20 #include <string> … … 20 22 #include <algorithm> 21 23 #include <cassert> 24 #include <utility> 22 25 23 26 #include "Box.h" … … 28 31 #include "Parser/ParseNode.h" 29 32 33 #include "SynTree/Constant.h" 30 34 #include "SynTree/Type.h" 31 35 #include "SynTree/Expression.h" … … 50 54 FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars ); 51 55 56 /// Cache for layout struct decls 57 class LayoutStructDecls { 58 public: 59 /// Gets the layout struct declaration for a given number of members (0 for non-generic); creates declarations as needed. 60 /// Declarations are allocated with "new", not freed on LayoutStructDecls destruction 61 StructDecl* get( int members ); 62 /// adds all struct declarations to the translation unit 63 void addToTranslationUnit( std::list< Declaration* >& translationUnit ); 64 private: 65 /// Layout struct declarations, indexed by number of members in the generic struct (0 for non-generic) 66 std::map< int, StructDecl* > decls; 67 }; 68 52 69 /// Replaces polymorphic return types with out-parameters, replaces calls to polymorphic functions with adapter calls as needed, and adds appropriate type variables to the function call 53 70 class Pass1 : public PolyMutator { 54 71 public: 55 Pass1( );72 Pass1( LayoutStructDecls &layoutDecls ); 56 73 virtual Expression *mutate( ApplicationExpr *appExpr ); 57 74 virtual Expression *mutate( AddressExpr *addrExpr ); … … 87 104 bool useRetval; 88 105 UniqueName tempNamer; 106 LayoutStructDecls &layoutDecls; 89 107 }; 90 108 … … 92 110 class Pass2 : public PolyMutator { 93 111 public: 94 Pass2( );112 Pass2( LayoutStructDecls &layoutDecls ); 95 113 template< typename DeclClass > 96 114 DeclClass *handleDecl( DeclClass *decl, Type *type ); … … 105 123 106 124 std::map< UniqueId, std::string > adapterName; 125 LayoutStructDecls &layoutDecls; 107 126 }; 108 127 … … 110 129 class Pass3 : public PolyMutator { 111 130 public: 131 Pass3( LayoutStructDecls &layoutDecls ); 112 132 template< typename DeclClass > 113 133 DeclClass *handleDecl( DeclClass *decl, Type *type ); … … 120 140 virtual Type *mutate( FunctionType *funcType ); 121 141 private: 142 LayoutStructDecls &layoutDecls; 122 143 }; 123 144 … … 134 155 135 156 void box( std::list< Declaration *>& translationUnit ) { 136 Pass1 pass1; 137 Pass2 pass2; 138 Pass3 pass3; 157 LayoutStructDecls layoutStructs; 158 Pass1 pass1( layoutStructs ); 159 Pass2 pass2( layoutStructs ); 160 Pass3 pass3( layoutStructs ); 139 161 mutateAll( translationUnit, pass1 ); 140 162 mutateAll( translationUnit, pass2 ); 141 163 mutateAll( translationUnit, pass3 ); 164 layoutStructs.addToTranslationUnit( translationUnit ); 165 } 166 167 //////////////////////////////////// LayoutStructDecls ////////////////////////////////////////////// 168 169 namespace { 170 StructDecl* LayoutStructDecls::get( int members ) { 171 // Attempt to read declaration already in map 172 std::map< int, StructDecl* >::iterator decl = decls.find( members ); 173 if ( decl != decls.end() ) return decl->second; 174 175 // Insert if not found 176 std::string member_str; 177 if ( members > 0 ) { 178 std::stringstream ss; 179 ss << members; 180 member_str = ss.str(); 181 } 182 StructDecl *newDecl = new StructDecl( std::string("_layout") + member_str ); 183 184 newDecl->get_members().push_back( 185 new ObjectDecl( "size", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0 ) ); 186 newDecl->get_members().push_back( 187 new ObjectDecl( "align", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0 ) ); 188 if ( members > 0 ) { 189 ArrayType *aType = 190 new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 191 new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), member_str ) ), false, false ); 192 newDecl->get_members().push_back( 193 new ObjectDecl( "offsets", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, aType, 0 ) ); 194 } 195 196 decls.insert( std::make_pair( members, newDecl ) ); 197 198 return newDecl; 199 } 200 201 void LayoutStructDecls::addToTranslationUnit( std::list< Declaration* >& translationUnit ) { 202 for ( std::map< int, StructDecl* >::reverse_iterator decl = decls.rbegin(); decl != decls.rend(); ++decl ) { 203 translationUnit.push_front( decl->second ); 204 } 205 } 142 206 } 143 207 … … 181 245 } 182 246 183 Pass1::Pass1( )184 : useRetval( false ), tempNamer( "_temp" ) {247 Pass1::Pass1( LayoutStructDecls &layoutDecls ) 248 : useRetval( false ), tempNamer( "_temp" ), layoutDecls( layoutDecls ) { 185 249 adapters.push(AdapterMap()); 186 250 } … … 936 1000 ////////////////////////////////////////// Pass2 //////////////////////////////////////////////////// 937 1001 938 Pass2::Pass2( ) {}1002 Pass2::Pass2( LayoutStructDecls &layoutDecls ) : layoutDecls(layoutDecls) {} 939 1003 940 1004 void Pass2::addAdapters( FunctionType *functionType ) { … … 1040 1104 ////////////////////////////////////////// Pass3 //////////////////////////////////////////////////// 1041 1105 1106 Pass3::Pass3( LayoutStructDecls &layoutDecls ) : layoutDecls(layoutDecls) {} 1107 1042 1108 template< typename DeclClass > 1043 1109 DeclClass * Pass3::handleDecl( DeclClass *decl, Type *type ) {
Note: See TracChangeset
for help on using the changeset viewer.