Changeset 7dc0246d
- Timestamp:
- Jan 15, 2018, 1:32:38 PM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- c0b9f5d
- Parents:
- bee7f04
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/GenType.cc
rbee7f04 r7dc0246d 26 26 27 27 namespace CodeGen { 28 class GenType : public Visitor { 29 public: 28 struct GenType : public WithVisitorRef<GenType>, public WithShortCircuiting { 30 29 GenType( const std::string &typeString, bool pretty = false, bool genC = false, bool lineMarks = false ); 31 30 std::string get_typeString() const { return typeString; } 32 31 void set_typeString( const std::string &newValue ) { typeString = newValue; } 33 32 34 virtual void visit( FunctionType *funcType ); 35 virtual void visit( VoidType *voidType ); 36 virtual void visit( BasicType *basicType ); 37 virtual void visit( PointerType *pointerType ); 38 virtual void visit( ArrayType *arrayType ); 39 virtual void visit( ReferenceType *refType ); 40 virtual void visit( StructInstType *structInst ); 41 virtual void visit( UnionInstType *unionInst ); 42 virtual void visit( EnumInstType *enumInst ); 43 virtual void visit( TypeInstType *typeInst ); 44 virtual void visit( TupleType * tupleType ); 45 virtual void visit( VarArgsType *varArgsType ); 46 virtual void visit( ZeroType *zeroType ); 47 virtual void visit( OneType *oneType ); 33 void previsit( BaseSyntaxNode * ); 34 void postvisit( BaseSyntaxNode * ); 35 36 void postvisit( FunctionType * funcType ); 37 void postvisit( VoidType * voidType ); 38 void postvisit( BasicType * basicType ); 39 void postvisit( PointerType * pointerType ); 40 void postvisit( ArrayType * arrayType ); 41 void postvisit( ReferenceType * refType ); 42 void postvisit( StructInstType * structInst ); 43 void postvisit( UnionInstType * unionInst ); 44 void postvisit( EnumInstType * enumInst ); 45 void postvisit( TypeInstType * typeInst ); 46 void postvisit( TupleType * tupleType ); 47 void postvisit( VarArgsType * varArgsType ); 48 void postvisit( ZeroType * zeroType ); 49 void postvisit( OneType * oneType ); 48 50 49 51 private: … … 59 61 60 62 std::string genType( Type *type, const std::string &baseString, bool pretty, bool genC , bool lineMarks ) { 61 GenTypegt( baseString, pretty, genC, lineMarks );63 PassVisitor<GenType> gt( baseString, pretty, genC, lineMarks ); 62 64 std::ostringstream os; 63 65 … … 68 70 69 71 type->accept( gt ); 70 return os.str() + gt. get_typeString();72 return os.str() + gt.pass.get_typeString(); 71 73 } 72 74 … … 77 79 GenType::GenType( const std::string &typeString, bool pretty, bool genC, bool lineMarks ) : typeString( typeString ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {} 78 80 79 void GenType::visit( VoidType *voidType ) { 81 // *** BaseSyntaxNode 82 void GenType::previsit( BaseSyntaxNode * ) { 83 // turn off automatic recursion for all nodes, to allow each visitor to 84 // precisely control the order in which its children are visited. 85 visit_children = false; 86 } 87 88 void GenType::postvisit( BaseSyntaxNode * node ) { 89 std::stringstream ss; 90 node->print( ss ); 91 assertf( false, "Unhandled node reached in GenType: %s", ss.str().c_str() ); 92 } 93 94 void GenType::postvisit( VoidType * voidType ) { 80 95 typeString = "void " + typeString; 81 96 handleQualifiers( voidType ); 82 97 } 83 98 84 void GenType:: visit( BasicType *basicType ) {85 BasicType::Kind kind = basicType-> get_kind();99 void GenType::postvisit( BasicType * basicType ) { 100 BasicType::Kind kind = basicType->kind; 86 101 assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES ); 87 102 typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString; … … 89 104 } 90 105 91 void GenType::genArray( const Type::Qualifiers & qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ) {106 void GenType::genArray( const Type::Qualifiers & qualifiers, Type * base, Expression *dimension, bool isVarLen, bool isStatic ) { 92 107 std::ostringstream os; 93 108 if ( typeString != "" ) { … … 126 141 typeString = os.str(); 127 142 128 base->accept( * this);129 } 130 131 void GenType:: visit( PointerType *pointerType ) {132 assert( pointerType-> get_base()!= 0);133 if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType-> get_dimension()) {134 genArray( pointerType->get_qualifiers(), pointerType-> get_base(), pointerType->get_dimension(), pointerType->get_isVarLen(), pointerType->get_isStatic() );143 base->accept( *visitor ); 144 } 145 146 void GenType::postvisit( PointerType * pointerType ) { 147 assert( pointerType->base != 0); 148 if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->dimension ) { 149 genArray( pointerType->get_qualifiers(), pointerType->base, pointerType->dimension, pointerType->get_isVarLen(), pointerType->get_isStatic() ); 135 150 } else { 136 151 handleQualifiers( pointerType ); … … 140 155 typeString = "*" + typeString; 141 156 } // if 142 pointerType-> get_base()->accept( *this);143 } // if 144 } 145 146 void GenType:: visit( ArrayType *arrayType ) {147 genArray( arrayType->get_qualifiers(), arrayType-> get_base(), arrayType->get_dimension(), arrayType->get_isVarLen(), arrayType->get_isStatic() );148 } 149 150 void GenType:: visit( ReferenceType *refType ) {151 assert( refType-> get_base()!= 0);157 pointerType->base->accept( *visitor ); 158 } // if 159 } 160 161 void GenType::postvisit( ArrayType * arrayType ) { 162 genArray( arrayType->get_qualifiers(), arrayType->base, arrayType->dimension, arrayType->get_isVarLen(), arrayType->get_isStatic() ); 163 } 164 165 void GenType::postvisit( ReferenceType * refType ) { 166 assert( refType->base != 0); 152 167 assertf( ! genC, "Reference types should not reach code generation." ); 153 168 handleQualifiers( refType ); 154 169 typeString = "&" + typeString; 155 refType-> get_base()->accept( *this);156 } 157 158 void GenType:: visit( FunctionType *funcType ) {170 refType->base->accept( *visitor ); 171 } 172 173 void GenType::postvisit( FunctionType * funcType ) { 159 174 std::ostringstream os; 160 175 … … 169 184 /************* parameters ***************/ 170 185 171 const std::list<DeclarationWithType *> &pars = funcType-> get_parameters();186 const std::list<DeclarationWithType *> &pars = funcType->parameters; 172 187 173 188 if ( pars.empty() ) { … … 191 206 typeString = os.str(); 192 207 193 if ( funcType-> get_returnVals().size() == 0 ) {208 if ( funcType->returnVals.size() == 0 ) { 194 209 typeString = "void " + typeString; 195 210 } else { 196 funcType-> get_returnVals().front()->get_type()->accept( *this);211 funcType->returnVals.front()->get_type()->accept( *visitor ); 197 212 } // if 198 213 199 214 // add forall 200 if( ! funcType-> get_forall().empty() && ! genC ) {215 if( ! funcType->forall.empty() && ! genC ) { 201 216 // assertf( ! genC, "Aggregate type parameters should not reach code generation." ); 202 217 std::ostringstream os; 203 218 PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks ); 204 219 os << "forall("; 205 cg.pass.genCommaList( funcType-> get_forall().begin(), funcType->get_forall().end() );220 cg.pass.genCommaList( funcType->forall.begin(), funcType->forall.end() ); 206 221 os << ")" << std::endl; 207 222 typeString = os.str() + typeString; … … 221 236 } 222 237 223 void GenType:: visit( StructInstType *structInst ) {224 typeString = structInst-> get_name()+ handleGeneric( structInst ) + " " + typeString;238 void GenType::postvisit( StructInstType * structInst ) { 239 typeString = structInst->name + handleGeneric( structInst ) + " " + typeString; 225 240 if ( genC ) typeString = "struct " + typeString; 226 241 handleQualifiers( structInst ); 227 242 } 228 243 229 void GenType:: visit( UnionInstType *unionInst ) {230 typeString = unionInst-> get_name()+ handleGeneric( unionInst ) + " " + typeString;244 void GenType::postvisit( UnionInstType * unionInst ) { 245 typeString = unionInst->name + handleGeneric( unionInst ) + " " + typeString; 231 246 if ( genC ) typeString = "union " + typeString; 232 247 handleQualifiers( unionInst ); 233 248 } 234 249 235 void GenType:: visit( EnumInstType *enumInst ) {236 typeString = enumInst-> get_name()+ " " + typeString;250 void GenType::postvisit( EnumInstType * enumInst ) { 251 typeString = enumInst->name + " " + typeString; 237 252 if ( genC ) typeString = "enum " + typeString; 238 253 handleQualifiers( enumInst ); 239 254 } 240 255 241 void GenType:: visit( TypeInstType *typeInst ) {242 typeString = typeInst-> get_name()+ " " + typeString;256 void GenType::postvisit( TypeInstType * typeInst ) { 257 typeString = typeInst->name + " " + typeString; 243 258 handleQualifiers( typeInst ); 244 259 } 245 260 246 void GenType:: visit( TupleType * tupleType ) {261 void GenType::postvisit( TupleType * tupleType ) { 247 262 assertf( ! genC, "Tuple types should not reach code generation." ); 248 263 unsigned int i = 0; … … 257 272 } 258 273 259 void GenType:: visit( VarArgsType *varArgsType ) {274 void GenType::postvisit( VarArgsType * varArgsType ) { 260 275 typeString = "__builtin_va_list " + typeString; 261 276 handleQualifiers( varArgsType ); 262 277 } 263 278 264 void GenType:: visit( ZeroType *zeroType ) {279 void GenType::postvisit( ZeroType * zeroType ) { 265 280 // ideally these wouldn't hit codegen at all, but should be safe to make them ints 266 281 typeString = (pretty ? "zero_t " : "long int ") + typeString; … … 268 283 } 269 284 270 void GenType:: visit( OneType *oneType ) {285 void GenType::postvisit( OneType * oneType ) { 271 286 // ideally these wouldn't hit codegen at all, but should be safe to make them ints 272 287 typeString = (pretty ? "one_t " : "long int ") + typeString; … … 274 289 } 275 290 276 void GenType::handleQualifiers( Type * type ) {291 void GenType::handleQualifiers( Type * type ) { 277 292 if ( type->get_const() ) { 278 293 typeString = "const " + typeString;
Note: See TracChangeset
for help on using the changeset viewer.