|
8 | 8 | #include "Boolean.h" |
9 | 9 | #include "Interp.h" |
10 | 10 | #include "PrimType.h" |
| 11 | +#include "clang/AST/RecordLayout.h" |
11 | 12 | #include "clang/Basic/Builtins.h" |
12 | 13 | #include "clang/Basic/TargetInfo.h" |
13 | 14 |
|
@@ -519,5 +520,79 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F, |
519 | 520 | return false; |
520 | 521 | } |
521 | 522 |
|
| 523 | +bool InterpretOffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E, |
| 524 | + llvm::ArrayRef<int64_t> ArrayIndices, |
| 525 | + int64_t &IntResult) { |
| 526 | + CharUnits Result; |
| 527 | + unsigned N = E->getNumComponents(); |
| 528 | + assert(N > 0); |
| 529 | + |
| 530 | + unsigned ArrayIndex = 0; |
| 531 | + QualType CurrentType = E->getTypeSourceInfo()->getType(); |
| 532 | + for (unsigned I = 0; I != N; ++I) { |
| 533 | + const OffsetOfNode &Node = E->getComponent(I); |
| 534 | + switch (Node.getKind()) { |
| 535 | + case OffsetOfNode::Field: { |
| 536 | + const FieldDecl *MemberDecl = Node.getField(); |
| 537 | + const RecordType *RT = CurrentType->getAs<RecordType>(); |
| 538 | + if (!RT) |
| 539 | + return false; |
| 540 | + RecordDecl *RD = RT->getDecl(); |
| 541 | + if (RD->isInvalidDecl()) |
| 542 | + return false; |
| 543 | + const ASTRecordLayout &RL = S.getCtx().getASTRecordLayout(RD); |
| 544 | + unsigned FieldIndex = MemberDecl->getFieldIndex(); |
| 545 | + assert(FieldIndex < RL.getFieldCount() && "offsetof field in wrong type"); |
| 546 | + Result += S.getCtx().toCharUnitsFromBits(RL.getFieldOffset(FieldIndex)); |
| 547 | + CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 548 | + break; |
| 549 | + } |
| 550 | + case OffsetOfNode::Array: { |
| 551 | + // When generating bytecode, we put all the index expressions as Sint64 on |
| 552 | + // the stack. |
| 553 | + int64_t Index = ArrayIndices[ArrayIndex]; |
| 554 | + const ArrayType *AT = S.getCtx().getAsArrayType(CurrentType); |
| 555 | + if (!AT) |
| 556 | + return false; |
| 557 | + CurrentType = AT->getElementType(); |
| 558 | + CharUnits ElementSize = S.getCtx().getTypeSizeInChars(CurrentType); |
| 559 | + Result += Index * ElementSize; |
| 560 | + ++ArrayIndex; |
| 561 | + break; |
| 562 | + } |
| 563 | + case OffsetOfNode::Base: { |
| 564 | + const CXXBaseSpecifier *BaseSpec = Node.getBase(); |
| 565 | + if (BaseSpec->isVirtual()) |
| 566 | + return false; |
| 567 | + |
| 568 | + // Find the layout of the class whose base we are looking into. |
| 569 | + const RecordType *RT = CurrentType->getAs<RecordType>(); |
| 570 | + if (!RT) |
| 571 | + return false; |
| 572 | + const RecordDecl *RD = RT->getDecl(); |
| 573 | + if (RD->isInvalidDecl()) |
| 574 | + return false; |
| 575 | + const ASTRecordLayout &RL = S.getCtx().getASTRecordLayout(RD); |
| 576 | + |
| 577 | + // Find the base class itself. |
| 578 | + CurrentType = BaseSpec->getType(); |
| 579 | + const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
| 580 | + if (!BaseRT) |
| 581 | + return false; |
| 582 | + |
| 583 | + // Add the offset to the base. |
| 584 | + Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); |
| 585 | + break; |
| 586 | + } |
| 587 | + case OffsetOfNode::Identifier: |
| 588 | + llvm_unreachable("Dependent OffsetOfExpr?"); |
| 589 | + } |
| 590 | + } |
| 591 | + |
| 592 | + IntResult = Result.getQuantity(); |
| 593 | + |
| 594 | + return true; |
| 595 | +} |
| 596 | + |
522 | 597 | } // namespace interp |
523 | 598 | } // namespace clang |
0 commit comments