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