Ted Kremenek | 503524a | 2010-03-08 20:56:29 +0000 | [diff] [blame] | 1 | //===-- RecordLayout.cpp - Layout information for a struct/union -*- C++ -*-==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the RecordLayout interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/RecordLayout.h" |
Eli Friedman | 2fe3636 | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 16 | #include "clang/Basic/TargetInfo.h" |
Ted Kremenek | 503524a | 2010-03-08 20:56:29 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace clang; |
| 19 | |
| 20 | void ASTRecordLayout::Destroy(ASTContext &Ctx) { |
| 21 | if (FieldOffsets) |
| 22 | Ctx.Deallocate(FieldOffsets); |
Argyrios Kyrtzidis | 42f81e6 | 2010-08-25 00:32:19 +0000 | [diff] [blame] | 23 | if (CXXInfo) { |
Ted Kremenek | 503524a | 2010-03-08 20:56:29 +0000 | [diff] [blame] | 24 | Ctx.Deallocate(CXXInfo); |
Argyrios Kyrtzidis | 42f81e6 | 2010-08-25 00:32:19 +0000 | [diff] [blame] | 25 | CXXInfo->~CXXRecordLayoutInfo(); |
| 26 | } |
Ted Kremenek | 503524a | 2010-03-08 20:56:29 +0000 | [diff] [blame] | 27 | this->~ASTRecordLayout(); |
| 28 | Ctx.Deallocate(this); |
| 29 | } |
| 30 | |
Ken Dyck | 5f022d8 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 31 | ASTRecordLayout::ASTRecordLayout(const ASTContext &Ctx, CharUnits size, |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 32 | CharUnits alignment, CharUnits datasize, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 33 | const uint64_t *fieldoffsets, |
| 34 | unsigned fieldcount) |
John McCall | 441c623 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 35 | : Size(size), DataSize(datasize), Alignment(alignment), FieldOffsets(0), |
Ted Kremenek | 503524a | 2010-03-08 20:56:29 +0000 | [diff] [blame] | 36 | FieldCount(fieldcount), CXXInfo(0) { |
| 37 | if (FieldCount > 0) { |
| 38 | FieldOffsets = new (Ctx) uint64_t[FieldCount]; |
Benjamin Kramer | bb9357c | 2010-03-09 12:53:38 +0000 | [diff] [blame] | 39 | memcpy(FieldOffsets, fieldoffsets, FieldCount * sizeof(*FieldOffsets)); |
Ted Kremenek | 503524a | 2010-03-08 20:56:29 +0000 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
| 43 | // Constructor for C++ records. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 44 | ASTRecordLayout::ASTRecordLayout(const ASTContext &Ctx, |
Ken Dyck | dac54c1 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 45 | CharUnits size, CharUnits alignment, |
John McCall | 441c623 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 46 | bool hasOwnVFPtr, CharUnits vbptroffset, |
Eli Friedman | 227e483 | 2011-10-21 22:49:56 +0000 | [diff] [blame] | 47 | CharUnits datasize, |
Anders Carlsson | 147b5dd | 2010-03-11 04:10:39 +0000 | [diff] [blame] | 48 | const uint64_t *fieldoffsets, |
| 49 | unsigned fieldcount, |
Ken Dyck | 5c3633f | 2011-02-01 01:52:10 +0000 | [diff] [blame] | 50 | CharUnits nonvirtualsize, |
Ken Dyck | 68cf1a5 | 2011-02-08 02:02:47 +0000 | [diff] [blame] | 51 | CharUnits nonvirtualalign, |
Anders Carlsson | a3d4380 | 2010-10-31 22:13:23 +0000 | [diff] [blame] | 52 | CharUnits SizeOfLargestEmptySubobject, |
Anders Carlsson | 28fdd0a | 2010-05-26 05:20:58 +0000 | [diff] [blame] | 53 | const CXXRecordDecl *PrimaryBase, |
Anders Carlsson | 0b9fb2f | 2010-11-24 23:20:19 +0000 | [diff] [blame] | 54 | bool IsPrimaryBaseVirtual, |
Anders Carlsson | 147b5dd | 2010-03-11 04:10:39 +0000 | [diff] [blame] | 55 | const BaseOffsetsMapTy& BaseOffsets, |
John McCall | 441c623 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 56 | const VBaseOffsetsMapTy& VBaseOffsets) |
| 57 | : Size(size), DataSize(datasize), Alignment(alignment), FieldOffsets(0), |
Ted Kremenek | 503524a | 2010-03-08 20:56:29 +0000 | [diff] [blame] | 58 | FieldCount(fieldcount), CXXInfo(new (Ctx) CXXRecordLayoutInfo) |
| 59 | { |
| 60 | if (FieldCount > 0) { |
| 61 | FieldOffsets = new (Ctx) uint64_t[FieldCount]; |
Benjamin Kramer | bb9357c | 2010-03-09 12:53:38 +0000 | [diff] [blame] | 62 | memcpy(FieldOffsets, fieldoffsets, FieldCount * sizeof(*FieldOffsets)); |
Ted Kremenek | 503524a | 2010-03-08 20:56:29 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Anders Carlsson | 0b9fb2f | 2010-11-24 23:20:19 +0000 | [diff] [blame] | 65 | CXXInfo->PrimaryBase.setPointer(PrimaryBase); |
| 66 | CXXInfo->PrimaryBase.setInt(IsPrimaryBaseVirtual); |
Ted Kremenek | 503524a | 2010-03-08 20:56:29 +0000 | [diff] [blame] | 67 | CXXInfo->NonVirtualSize = nonvirtualsize; |
| 68 | CXXInfo->NonVirtualAlign = nonvirtualalign; |
Anders Carlsson | c3fddeb | 2010-05-08 22:35:05 +0000 | [diff] [blame] | 69 | CXXInfo->SizeOfLargestEmptySubobject = SizeOfLargestEmptySubobject; |
Anders Carlsson | 147b5dd | 2010-03-11 04:10:39 +0000 | [diff] [blame] | 70 | CXXInfo->BaseOffsets = BaseOffsets; |
| 71 | CXXInfo->VBaseOffsets = VBaseOffsets; |
John McCall | 441c623 | 2012-05-01 08:55:32 +0000 | [diff] [blame] | 72 | CXXInfo->HasOwnVFPtr = hasOwnVFPtr; |
Eli Friedman | 2fe3636 | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 73 | CXXInfo->VBPtrOffset = vbptroffset; |
Anders Carlsson | b6ee4b0 | 2010-03-11 01:49:18 +0000 | [diff] [blame] | 74 | |
| 75 | #ifndef NDEBUG |
| 76 | if (const CXXRecordDecl *PrimaryBase = getPrimaryBase()) { |
Eli Friedman | 2fe3636 | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 77 | if (isPrimaryBaseVirtual()) { |
| 78 | // Microsoft ABI doesn't have primary virtual base |
| 79 | if (Ctx.getTargetInfo().getCXXABI() != CXXABI_Microsoft) { |
Anders Carlsson | 3069a0d | 2010-10-31 23:45:59 +0000 | [diff] [blame] | 80 | assert(getVBaseClassOffset(PrimaryBase).isZero() && |
Anders Carlsson | b6ee4b0 | 2010-03-11 01:49:18 +0000 | [diff] [blame] | 81 | "Primary virtual base must be at offset 0!"); |
Eli Friedman | 2fe3636 | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 82 | } |
| 83 | } else { |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 84 | assert(getBaseClassOffset(PrimaryBase).isZero() && |
Anders Carlsson | b6ee4b0 | 2010-03-11 01:49:18 +0000 | [diff] [blame] | 85 | "Primary base must be at offset 0!"); |
Eli Friedman | 2fe3636 | 2011-09-27 19:12:27 +0000 | [diff] [blame] | 86 | } |
Anders Carlsson | b6ee4b0 | 2010-03-11 01:49:18 +0000 | [diff] [blame] | 87 | } |
| 88 | #endif |
Ted Kremenek | 503524a | 2010-03-08 20:56:29 +0000 | [diff] [blame] | 89 | } |