Changeset 98233b3
- Timestamp:
- Jul 26, 2021, 1:02:30 PM (3 years ago)
- Branches:
- ADT, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 04141f8, d83b266
- Parents:
- 866cad3
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ControlStruct/ExceptDecl.cc
r866cad3 r98233b3 10 10 // Created On : Tue Jul 20 04:10:50 2021 11 11 // Last Modified By : Henry Xue 12 // Last Modified On : Thu Jul 22 10:40:55202113 // Update Count : 212 // Last Modified On : Mon Jul 26 12:55:28 2021 13 // Update Count : 3 14 14 // 15 15 16 16 #include "ExceptDecl.h" 17 17 18 #include <cassert> // for assert 19 #include <string> // for string 20 #include <sstream> // for stringstream 21 18 22 #include "Common/PassVisitor.h" // for PassVisitor 23 #include "Common/utility.h" // for cloneAll 19 24 #include "SynTree/Mutator.h" // for mutateAll 20 25 #include "Virtual/Tables.h" // for helpers … … 22 27 namespace ControlStruct { 23 28 24 StructDecl * ehmTypeIdStruct( const std::string & exceptionName, std::list< TypeDecl *> * parameters ) { 29 const std::list< Expression *> & makeParameters( 30 const std::list< TypeDecl *> & forallClause 31 ) { 32 auto parameters = new std::list< Expression *>(); 33 for ( auto it = forallClause.begin(); it != forallClause.end(); it++ ) { 34 parameters->emplace_back( new TypeExpr( 35 new TypeInstType( noQualifiers, ( *it )->get_name(), false ) 36 ) ); 37 } 38 return *parameters; 39 } 40 41 TypeInstType * makeExceptInstType( 42 const std::string & exceptionName, 43 const std::list< Expression *> & parameters 44 ) { 45 TypeInstType * exceptInstType = new TypeInstType( 46 noQualifiers, 47 exceptionName, 48 false 49 ); 50 cloneAll( parameters, exceptInstType->parameters ); 51 return exceptInstType; 52 } 53 54 // void (*copy)(exception_name parameters * this, exception_name parameters * that); 55 FunctionType * makeCopyFnType( 56 const std::string & exceptionName, 57 const std::list< Expression *> & parameters 58 ) { 59 FunctionType * copyFnType = new FunctionType( noQualifiers, false ); 60 copyFnType->get_parameters().push_back( new ObjectDecl( 61 "this", 62 noStorageClasses, 63 LinkageSpec::Cforall, 64 nullptr, 65 new PointerType( noQualifiers, 66 makeExceptInstType( exceptionName, parameters ) ), 67 nullptr 68 ) ); 69 copyFnType->get_parameters().push_back( new ObjectDecl( 70 "that", 71 noStorageClasses, 72 LinkageSpec::Cforall, 73 nullptr, 74 new PointerType( noQualifiers, 75 makeExceptInstType( exceptionName, parameters ) ), 76 nullptr 77 ) ); 78 copyFnType->get_returnVals().push_back( new ObjectDecl( 79 "", 80 noStorageClasses, 81 LinkageSpec::Cforall, 82 nullptr, 83 new VoidType( noQualifiers ), 84 nullptr 85 ) ); 86 return copyFnType; 87 } 88 89 // void (*^?{})(exception_name parameters & this); 90 FunctionType * makeDtorFnType( 91 const std::string & exceptionName, 92 const std::list< Expression *> & parameters 93 ) { 94 FunctionType * dtorFnType = new FunctionType( noQualifiers, false ); 95 dtorFnType->get_parameters().push_back( new ObjectDecl( 96 "this", 97 noStorageClasses, 98 LinkageSpec::Cforall, 99 nullptr, 100 new ReferenceType( noQualifiers, 101 makeExceptInstType( exceptionName, parameters ) ), 102 nullptr 103 ) ); 104 dtorFnType->get_returnVals().push_back( new ObjectDecl( 105 "", 106 noStorageClasses, 107 LinkageSpec::Cforall, 108 nullptr, 109 new VoidType( noQualifiers ), 110 nullptr 111 ) ); 112 return dtorFnType; 113 } 114 115 // const char * (*msg)(exception_name parameters * this); 116 FunctionType * makeMsgFnType( 117 const std::string & exceptionName, 118 const std::list< Expression *> & parameters 119 ) { 120 FunctionType * msgFnType = new FunctionType( noQualifiers, false ); 121 msgFnType->get_parameters().push_back( new ObjectDecl( 122 "this", 123 noStorageClasses, 124 LinkageSpec::Cforall, 125 nullptr, 126 new PointerType( noQualifiers, 127 makeExceptInstType( exceptionName, parameters ) ), 128 nullptr 129 ) ); 130 msgFnType->get_returnVals().push_back( new ObjectDecl( 131 "", 132 noStorageClasses, 133 LinkageSpec::Cforall, 134 nullptr, 135 new PointerType( noQualifiers, 136 new BasicType( Type::Const, BasicType::Char ) ), 137 nullptr 138 ) ); 139 return msgFnType; 140 } 141 142 StructDecl * ehmTypeIdStruct( 143 const std::string & exceptionName, 144 const std::list< TypeDecl *> & forallClause 145 ) { 25 146 StructDecl * structDecl = new StructDecl( Virtual::typeIdType( exceptionName ) ); 26 147 structDecl->members.push_back( new ObjectDecl( … … 34 155 ) ); 35 156 structDecl->set_body( true ); 36 if ( parameters ) { 37 structDecl->parameters = *parameters; 38 } 157 cloneAll( forallClause, structDecl->parameters ); 39 158 return structDecl; 40 159 } 41 160 42 ObjectDecl * ehmTypeIdValue( const std::string & exceptionName, std::list< Expression *> * parameters ) { 43 StructInstType * structInstType = new StructInstType( Type::Const, Virtual::typeIdType( exceptionName ) ); 44 if ( parameters ) { 45 structInstType->parameters = *parameters; 46 } 161 ObjectDecl * ehmTypeIdValue( 162 const std::string & exceptionName, 163 const std::list< Expression *> & parameters 164 ) { 165 StructInstType * typeIdType = new StructInstType( 166 Type::Const, 167 Virtual::typeIdType( exceptionName ) 168 ); 169 cloneAll( parameters, typeIdType->parameters ); 47 170 return new ObjectDecl( 48 171 Virtual::typeIdName( exceptionName ), … … 50 173 LinkageSpec::Cforall, 51 174 nullptr, 52 structInstType,175 typeIdType, 53 176 new ListInit( { new SingleInit( 54 177 new AddressExpr( new NameExpr( "__cfatid_exception_t" ) ) … … 58 181 } 59 182 60 StructDecl * ehmExceptionStructDecl( const std::string & exceptionName, std::list< TypeDecl *> * parameters ) { 183 StructDecl * ehmExceptionStructDecl( 184 const std::string & exceptionName, 185 const std::list< TypeDecl *> & forallClause 186 ) { 61 187 StructDecl * structDecl = new StructDecl( exceptionName ); 62 if ( parameters ) { 63 structDecl->parameters = *parameters; 64 } 188 cloneAll( forallClause, structDecl->parameters ); 65 189 return structDecl; 66 190 } 67 191 68 StructDecl * ehmVirtualTableStruct( const std::string & exceptionName, std::list< TypeDecl *> * parameters ) { 69 // _EHM_TYPE_ID_TYPE(exception_name) parameters const * __cfavir_typeid; 192 StructDecl * ehmVirtualTableStruct( 193 const std::string & exceptionName, 194 const std::list< TypeDecl *> & forallClause, 195 const std::list< Expression *> & parameters 196 ) { 197 StructInstType * typeIdType = new StructInstType( 198 Type::Const, 199 Virtual::typeIdType( exceptionName ) 200 ); 201 cloneAll( parameters, typeIdType->parameters ); 70 202 ObjectDecl * typeId = new ObjectDecl( 71 203 "__cfavir_typeid", … … 73 205 LinkageSpec::Cforall, 74 206 nullptr, 75 new PointerType( noQualifiers, 76 new StructInstType( Type::Const, Virtual::typeIdType( exceptionName ) ) ), 77 nullptr 78 ); 79 80 // size_t size; 207 new PointerType( noQualifiers, typeIdType ), 208 nullptr 209 ); 81 210 ObjectDecl * size = new ObjectDecl( 82 211 "size", … … 87 216 nullptr 88 217 ); 89 90 // void (*copy)(exception_name parameters * this, exception_name parameters * other);91 FunctionType * copyFnType = new FunctionType( noQualifiers, false );92 copyFnType->get_parameters().push_back( new ObjectDecl(93 "this",94 noStorageClasses,95 LinkageSpec::Cforall,96 nullptr,97 new PointerType( noQualifiers,98 new TypeInstType( noQualifiers, exceptionName, false ) ),99 nullptr100 ) );101 copyFnType->get_parameters().push_back( new ObjectDecl(102 "other",103 noStorageClasses,104 LinkageSpec::Cforall,105 nullptr,106 new PointerType( noQualifiers,107 new TypeInstType( noQualifiers, exceptionName, false ) ),108 nullptr109 ) );110 copyFnType->get_returnVals().push_back( new ObjectDecl(111 "",112 noStorageClasses,113 LinkageSpec::Cforall,114 nullptr,115 new VoidType( noQualifiers ),116 nullptr117 ) );118 218 ObjectDecl * copy = new ObjectDecl( 119 219 "copy", … … 121 221 LinkageSpec::Cforall, 122 222 nullptr, 123 new PointerType( noQualifiers, copyFnType ), 124 nullptr 125 ); 126 127 // void (*^?{})(exception_name parameters & this); 128 FunctionType * dtorFnType = new FunctionType( noQualifiers, false ); 129 dtorFnType->get_parameters().push_back( new ObjectDecl( 130 "this", 131 noStorageClasses, 132 LinkageSpec::Cforall, 133 nullptr, 134 new ReferenceType( noQualifiers, 135 new TypeInstType( noQualifiers, exceptionName, false ) ), 136 nullptr 137 ) ); 138 dtorFnType->get_returnVals().push_back( new ObjectDecl( 139 "", 140 noStorageClasses, 141 LinkageSpec::Cforall, 142 nullptr, 143 new VoidType( noQualifiers ), 144 nullptr 145 ) ); 223 new PointerType( noQualifiers, 224 makeCopyFnType( exceptionName, parameters ) ), 225 nullptr 226 ); 146 227 ObjectDecl * dtor = new ObjectDecl( 147 228 "^?{}", … … 149 230 LinkageSpec::Cforall, 150 231 nullptr, 151 new PointerType( noQualifiers, dtorFnType ), 152 nullptr 153 ); 154 155 // const char * (*msg)(exception_name parameters * this); 156 FunctionType * msgFnType = new FunctionType( noQualifiers, false ); 157 msgFnType->get_parameters().push_back( new ObjectDecl( 158 "this", 159 noStorageClasses, 160 LinkageSpec::Cforall, 161 nullptr, 162 new PointerType( noQualifiers, 163 new TypeInstType( noQualifiers, exceptionName, false ) ), 164 nullptr 165 ) ); 166 msgFnType->get_returnVals().push_back( new ObjectDecl( 167 "", 168 noStorageClasses, 169 LinkageSpec::Cforall, 170 nullptr, 171 new PointerType( noQualifiers, new BasicType( Type::Const, BasicType::Char ) ), 172 nullptr 173 ) ); 232 new PointerType( noQualifiers, 233 makeDtorFnType( exceptionName, parameters ) ), 234 nullptr 235 ); 174 236 ObjectDecl * msg = new ObjectDecl( 175 237 "msg", … … 177 239 LinkageSpec::Cforall, 178 240 nullptr, 179 new PointerType( noQualifiers, msgFnType ),180 nullptr181 );182 241 new PointerType( noQualifiers, 242 makeMsgFnType( exceptionName, parameters ) ), 243 nullptr 244 ); 183 245 StructDecl * structDecl = new StructDecl( Virtual::vtableTypeName( exceptionName ) ); 184 246 structDecl->members.push_back( typeId ); … … 188 250 structDecl->members.push_back( msg ); 189 251 structDecl->set_body( true ); 190 if ( parameters ) { 191 structDecl->parameters = *parameters; 192 } 252 cloneAll( forallClause, structDecl->parameters ); 193 253 return structDecl; 194 254 } 195 255 196 StructDecl * ehmExceptionStruct( const std::string & exceptionName, const std::list<Declaration*> & members, 197 std::list<TypeDecl *> * parameters ) { 256 StructDecl * ehmExceptionStruct( 257 const std::string & exceptionName, 258 const std::list< TypeDecl *> & forallClause, 259 const std::list< Expression *> & parameters, 260 const std::list< Declaration *> & members 261 ) { 262 StructInstType * vtableType = new StructInstType( 263 Type::Const, 264 Virtual::vtableTypeName( exceptionName ) 265 ); 266 cloneAll( parameters, vtableType->parameters ); 198 267 StructDecl * structDecl = new StructDecl( exceptionName ); 199 268 structDecl->members = members; … … 203 272 LinkageSpec::Cforall, 204 273 nullptr, 205 new PointerType( noQualifiers, 206 new StructInstType( Type::Const, Virtual::vtableTypeName( exceptionName ) ) ), 274 new PointerType( noQualifiers, vtableType ), 207 275 nullptr 208 276 ) ); 209 277 structDecl->set_body( true ); 210 if ( parameters ) { 211 structDecl->parameters = *parameters; 212 } 278 cloneAll( forallClause, structDecl->parameters ); 213 279 return structDecl; 214 280 } 215 281 216 ObjectDecl * ehmExternVtable( const std::string & exceptionName, const std::string & tableName, 217 std::list< Expression *> * parameters ) { 218 StructInstType * structInstType = new StructInstType( Type::Const, Virtual::vtableTypeName(exceptionName) ); 219 if ( parameters ) { 220 structInstType->parameters = *parameters; 221 } 282 ObjectDecl * ehmExternVtable( 283 const std::string & exceptionName, 284 const std::list< Expression *> & parameters, 285 const std::string & tableName 286 ) { 287 StructInstType * vtableType = new StructInstType( 288 Type::Const, 289 Virtual::vtableTypeName( exceptionName ) 290 ); 291 cloneAll( parameters, vtableType->parameters ); 222 292 return new ObjectDecl( 223 293 tableName, … … 225 295 LinkageSpec::Cforall, 226 296 nullptr, 227 structInstType, 228 nullptr 229 ); 230 } 231 232 FunctionDecl * ehmDefineCopy( const std::string & exceptionName ) { 233 FunctionType * copyFnType = new FunctionType( noQualifiers, false ); 234 copyFnType->get_parameters().push_back( new ObjectDecl( 235 "this", 236 noStorageClasses, 237 LinkageSpec::Cforall, 238 nullptr, 239 new PointerType( noQualifiers, 240 new TypeInstType( noQualifiers, exceptionName, false ) ), 241 nullptr 242 ) ); 243 copyFnType->get_parameters().push_back( new ObjectDecl( 244 "that", 245 noStorageClasses, 246 LinkageSpec::Cforall, 247 nullptr, 248 new PointerType( noQualifiers, 249 new TypeInstType( noQualifiers, exceptionName, false ) ), 250 nullptr 251 ) ); 252 copyFnType->get_returnVals().push_back( new ObjectDecl( 253 "", 254 noStorageClasses, 255 LinkageSpec::Cforall, 256 nullptr, 257 new VoidType( noQualifiers ), 258 nullptr 259 ) ); 297 vtableType, 298 nullptr 299 ); 300 } 301 302 FunctionDecl * ehmDefineCopy( 303 const std::string & exceptionName, 304 const std::list< Expression *> & parameters 305 ) { 260 306 return new FunctionDecl( 261 307 "copy", 262 308 noStorageClasses, 263 309 LinkageSpec::Cforall, 264 copyFnType,310 makeCopyFnType( exceptionName, parameters ), 265 311 new CompoundStmt( { 266 312 new ExprStmt( new UntypedExpr( new NameExpr( "?=?" ), { … … 272 318 } 273 319 274 FunctionDecl * ehmDefineMsg( const std::string & exceptionName ) { 275 FunctionType * msgFnType = new FunctionType( noQualifiers, false ); 276 msgFnType->get_parameters().push_back( new ObjectDecl( 277 "this", 278 noStorageClasses, 279 LinkageSpec::Cforall, 280 nullptr, 281 new PointerType( noQualifiers, 282 new TypeInstType( noQualifiers, exceptionName, false ) ), 283 nullptr 284 ) ); 285 msgFnType->get_returnVals().push_back( new ObjectDecl( 286 "", 287 noStorageClasses, 288 LinkageSpec::Cforall, 289 nullptr, 290 new PointerType( noQualifiers, new BasicType( Type::Const, BasicType::Char ) ), 291 nullptr 292 ) ); 320 FunctionDecl * ehmDefineMsg( 321 const std::string & exceptionName, 322 const std::list< Expression *> & parameters 323 ) { 324 std::stringstream msg; 325 msg << exceptionName; 326 if ( !parameters.empty() ) { // forall variant, add parameters 327 msg << "("; 328 for ( auto it = parameters.begin(); it != parameters.end(); it++ ) { 329 ( *it )->print( msg ); 330 if ( it + 1 == parameters.end() ) { 331 msg << ")"; // end of list, close bracket 332 } else { 333 msg << ", "; // otherwise use comma as separator 334 } 335 } 336 } 293 337 return new FunctionDecl( 294 338 "msg", 295 339 noStorageClasses, 296 340 LinkageSpec::Cforall, 297 m sgFnType,341 makeMsgFnType( exceptionName, parameters ), 298 342 new CompoundStmt( { 299 new ReturnStmt( new ConstantExpr( Constant::from_string( exceptionName) ) )343 new ReturnStmt( new ConstantExpr( Constant::from_string( msg.str() ) ) ) 300 344 } ) 301 345 ); 302 346 } 303 347 304 ObjectDecl * ehmVirtualTable( const std::string & exceptionName, const std::string & tableName ) { 348 ObjectDecl * ehmVirtualTable( 349 const std::string & exceptionName, 350 const std::list< Expression *> & parameters, 351 const std::string & tableName 352 ) { 353 StructInstType * sizeofType = new StructInstType( noQualifiers, exceptionName ); 354 cloneAll( parameters, sizeofType->parameters ); 305 355 std::list< Initializer *> inits { 306 356 new SingleInit( new AddressExpr( 307 357 new NameExpr( Virtual::typeIdName( exceptionName ) ) ) ), 308 new SingleInit( new SizeofExpr( 309 new StructInstType( noQualifiers, exceptionName ) ) ), 358 new SingleInit( new SizeofExpr( sizeofType ) ), 310 359 new SingleInit( new NameExpr( "copy" ) ), 311 360 new SingleInit( new NameExpr( "^?{}" ) ), … … 319 368 new Designation( { new NameExpr( "msg" ) } ) 320 369 }; 370 StructInstType * vtableType = new StructInstType( 371 Type::Const, 372 Virtual::vtableTypeName( exceptionName ) 373 ); 374 cloneAll( parameters, vtableType->parameters ); 321 375 return new ObjectDecl( 322 376 tableName, … … 324 378 LinkageSpec::Cforall, 325 379 nullptr, 326 new StructInstType( Type::Const, Virtual::vtableTypeName( exceptionName ) ),380 vtableType, 327 381 new ListInit( inits, desig ) 328 382 ); … … 331 385 class ExceptDeclCore : public WithDeclsToAdd { 332 386 public: 333 Declaration * postmutate( StructDecl * structDecl ); // translates exception decls 334 DeclarationWithType * postmutate( ObjectDecl * objectDecl ); // translates vtable decls 387 // translates exception decls 388 Declaration * postmutate( StructDecl * structDecl ); 389 390 // translates vtable decls 391 DeclarationWithType * postmutate( ObjectDecl * objectDecl ); 335 392 }; 336 393 … … 338 395 if ( structDecl->is_exception() ) { 339 396 const std::string & exceptionName = structDecl->get_name(); 340 declsToAddBefore.push_back( ehmTypeIdStruct( exceptionName, nullptr ) ); 341 declsToAddBefore.push_back( ehmTypeIdValue( exceptionName, nullptr ) ); 342 declsToAddBefore.push_back( ehmExceptionStructDecl( exceptionName, nullptr ) ); 343 declsToAddBefore.push_back( ehmVirtualTableStruct( exceptionName, nullptr ) ); 344 return ehmExceptionStruct( exceptionName, structDecl->get_members(), nullptr ); 397 const std::list< TypeDecl *> & forallClause = structDecl->get_parameters(); 398 const std::list< Expression *> & parameters = makeParameters( forallClause ); 399 const std::list< Declaration *> & members = structDecl->get_members(); 400 401 declsToAddBefore.push_back( ehmTypeIdStruct( exceptionName, forallClause ) ); 402 if ( forallClause.empty() ) { // non-forall variant 403 declsToAddBefore.push_back( ehmTypeIdValue( exceptionName, parameters ) ); 404 } 405 declsToAddBefore.push_back( ehmExceptionStructDecl( exceptionName, forallClause ) ); 406 declsToAddBefore.push_back( ehmVirtualTableStruct( exceptionName, forallClause, parameters ) ); 407 return ehmExceptionStruct( exceptionName, forallClause, parameters, members ); 345 408 } 346 409 return structDecl; … … 355 418 const std::string & exceptionName = base->get_name(); 356 419 const std::string & tableName = objectDecl->get_name(); 357 if ( objectDecl->get_storageClasses().is_extern ) { 358 return ehmExternVtable( exceptionName, tableName, nullptr ); 359 } else { 360 declsToAddBefore.push_back( ehmDefineCopy( exceptionName ) ); 361 declsToAddBefore.push_back( ehmDefineMsg( exceptionName ) ); 362 return ehmVirtualTable( exceptionName, tableName ); 420 const std::list< Expression *> parameters = base->get_parameters(); 421 422 if ( objectDecl->get_storageClasses().is_extern ) { // if extern 423 return ehmExternVtable( exceptionName, parameters, tableName ); 363 424 } 425 // else, non-extern 426 if ( !parameters.empty() ) { // forall variant 427 declsToAddBefore.push_back( ehmTypeIdValue( exceptionName, parameters ) ); 428 } 429 declsToAddBefore.push_back( ehmDefineCopy( exceptionName, parameters ) ); 430 declsToAddBefore.push_back( ehmDefineMsg( exceptionName, parameters ) ); 431 return ehmVirtualTable( exceptionName, parameters, tableName ); 364 432 } 365 433 return objectDecl;
Note: See TracChangeset
for help on using the changeset viewer.