- Timestamp:
- Jul 22, 2021, 10:59:23 AM (3 years ago)
- Branches:
- ADT, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 12782a9
- Parents:
- 209dfe2
- git-author:
- Henry Xue <y58xue@…> (07/22/21 10:57:48)
- git-committer:
- Henry Xue <y58xue@…> (07/22/21 10:59:23)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ControlStruct/ExceptDecl.cc
r209dfe2 raa882e7e 10 10 // Created On : Tue Jul 20 04:10:50 2021 11 11 // Last Modified By : Henry Xue 12 // Last Modified On : T ue Jul 20 04:10:50202113 // Update Count : 112 // Last Modified On : Thu Jul 22 10:40:55 2021 13 // Update Count : 2 14 14 // 15 15 … … 214 214 } 215 215 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 } 222 return new ObjectDecl( 223 tableName, 224 Type::Extern, 225 LinkageSpec::Cforall, 226 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 ) ); 260 return new FunctionDecl( 261 "copy", 262 noStorageClasses, 263 LinkageSpec::Cforall, 264 copyFnType, 265 new CompoundStmt( { 266 new ExprStmt( new UntypedExpr( new NameExpr( "?=?" ), { 267 new UntypedExpr( new NameExpr( "*?" ), { new NameExpr( "this" ) } ), 268 new UntypedExpr( new NameExpr( "*?" ), { new NameExpr( "that" ) } ) 269 } ) ) 270 } ) 271 ); 272 } 273 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 ) ); 293 return new FunctionDecl( 294 "msg", 295 noStorageClasses, 296 LinkageSpec::Cforall, 297 msgFnType, 298 new CompoundStmt( { 299 new ReturnStmt( new ConstantExpr( Constant::from_string( exceptionName ) ) ) 300 } ) 301 ); 302 } 303 304 ObjectDecl * ehmVirtualTable( const std::string & exceptionName, const std::string & tableName ) { 305 std::list< Initializer *> inits { 306 new SingleInit( new AddressExpr( 307 new NameExpr( Virtual::typeIdName( exceptionName ) ) ) ), 308 new SingleInit( new SizeofExpr( 309 new StructInstType( noQualifiers, exceptionName ) ) ), 310 new SingleInit( new NameExpr( "copy" ) ), 311 new SingleInit( new NameExpr( "^?{}" ) ), 312 new SingleInit( new NameExpr( "msg" ) ) 313 }; 314 std::list< Designation *> desig { 315 new Designation( { new NameExpr( "__cfavir_typeid" ) } ), 316 new Designation( { new NameExpr( "size" ) } ), 317 new Designation( { new NameExpr( "copy" ) } ), 318 new Designation( { new NameExpr( "^?{}" ) } ), 319 new Designation( { new NameExpr( "msg" ) } ) 320 }; 321 return new ObjectDecl( 322 tableName, 323 noStorageClasses, 324 LinkageSpec::Cforall, 325 nullptr, 326 new StructInstType( Type::Const, Virtual::vtableTypeName( exceptionName ) ), 327 new ListInit( inits, desig ) 328 ); 329 } 330 216 331 class ExceptDeclCore : public WithDeclsToAdd { 217 332 public: 218 Declaration * postmutate( StructDecl * structDecl ); 333 Declaration * postmutate( StructDecl * structDecl ); // translates exception decls 334 DeclarationWithType * postmutate( ObjectDecl * objectDecl ); // translates vtable decls 219 335 }; 220 336 221 337 Declaration * ExceptDeclCore::postmutate( StructDecl * structDecl ) { 222 338 if ( structDecl->is_exception() ) { 223 const std::string & exceptionName = structDecl-> name;339 const std::string & exceptionName = structDecl->get_name(); 224 340 declsToAddBefore.push_back( ehmTypeIdStruct( exceptionName, nullptr ) ); 225 341 declsToAddBefore.push_back( ehmTypeIdValue( exceptionName, nullptr ) ); … … 231 347 } 232 348 349 DeclarationWithType * ExceptDeclCore::postmutate( ObjectDecl * objectDecl ) { 350 // Check if it is VTableType 351 VTableType * vtableType = dynamic_cast< VTableType *>( objectDecl->get_type() ); 352 if ( vtableType ) { 353 TypeInstType * base = dynamic_cast< TypeInstType *>( vtableType->get_base() ); 354 assert( base ); // should be a TypeInstType 355 const std::string & exceptionName = base->get_name(); 356 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 ); 363 } 364 } 365 return objectDecl; 366 } 367 233 368 void translateExcept( std::list< Declaration *> & translationUnit ) { 234 369 PassVisitor<ExceptDeclCore> translator;
Note: See TracChangeset
for help on using the changeset viewer.