1 //===--- TypeLocBuilder.cpp - Type Source Info collector ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This files defines TypeLocBuilder, a class for building TypeLocs
13 //===----------------------------------------------------------------------===//
15 #include "TypeLocBuilder.h"
17 using namespace clang;
19 void TypeLocBuilder::pushFullCopy(TypeLoc L) {
20 size_t Size = L.getFullDataSize();
23 SmallVector<TypeLoc, 4> TypeLocs;
26 TypeLocs.push_back(CurTL);
27 CurTL = CurTL.getNextTypeLoc();
30 for (unsigned i = 0, e = TypeLocs.size(); i < e; ++i) {
31 TypeLoc CurTL = TypeLocs[e-i-1];
32 switch (CurTL.getTypeLocClass()) {
33 #define ABSTRACT_TYPELOC(CLASS, PARENT)
34 #define TYPELOC(CLASS, PARENT) \
35 case TypeLoc::CLASS: { \
36 CLASS##TypeLoc NewTL = push<class CLASS##TypeLoc>(CurTL.getType()); \
37 memcpy(NewTL.getOpaqueData(), CurTL.getOpaqueData(), NewTL.getLocalDataSize()); \
40 #include "clang/AST/TypeLocNodes.def"
45 void TypeLocBuilder::grow(size_t NewCapacity) {
46 assert(NewCapacity > Capacity);
48 // Allocate the new buffer and copy the old data into it.
49 char *NewBuffer = new char[NewCapacity];
50 unsigned NewIndex = Index + NewCapacity - Capacity;
51 memcpy(&NewBuffer[NewIndex],
55 if (Buffer != InlineBuffer.buffer)
59 Capacity = NewCapacity;
63 TypeLoc TypeLocBuilder::pushImpl(QualType T, size_t LocalSize, unsigned LocalAlignment) {
65 QualType TLast = TypeLoc(T, nullptr).getNextTypeLoc().getType();
66 assert(TLast == LastTy &&
67 "mismatch between last type and new type's inner type");
71 assert(LocalAlignment <= BufferMaxAlignment && "Unexpected alignment");
73 // If we need to grow, grow by a factor of 2.
74 if (LocalSize > Index) {
75 size_t RequiredCapacity = Capacity + (LocalSize - Index);
76 size_t NewCapacity = Capacity * 2;
77 while (RequiredCapacity > NewCapacity)
82 // Because we're adding elements to the TypeLoc backwards, we have to
83 // do some extra work to keep everything aligned appropriately.
84 // FIXME: This algorithm is a absolute mess because every TypeLoc returned
85 // needs to be valid. Partial TypeLocs are a terrible idea.
86 // FIXME: 4 and 8 are sufficient at the moment, but it's pretty ugly to
88 if (LocalAlignment == 4) {
89 if (NumBytesAtAlign8 == 0) {
90 NumBytesAtAlign4 += LocalSize;
92 unsigned Padding = NumBytesAtAlign4 % 8;
94 if (LocalSize % 8 == 0) {
95 // Everything is set: there's no padding and we don't need to add
98 assert(LocalSize % 8 == 4);
99 // No existing padding; add in 4 bytes padding
100 memmove(&Buffer[Index - 4], &Buffer[Index], NumBytesAtAlign4);
104 assert(Padding == 4);
105 if (LocalSize % 8 == 0) {
106 // Everything is set: there's 4 bytes padding and we don't need
109 assert(LocalSize % 8 == 4);
110 // There are 4 bytes padding, but we don't need any; remove it.
111 memmove(&Buffer[Index + 4], &Buffer[Index], NumBytesAtAlign4);
115 NumBytesAtAlign4 += LocalSize;
117 } else if (LocalAlignment == 8) {
118 if (NumBytesAtAlign8 == 0) {
119 // We have not seen any 8-byte aligned element yet. We insert a padding
120 // only if the new Index is not 8-byte-aligned.
121 if ((Index - LocalSize) % 8 != 0) {
122 memmove(&Buffer[Index - 4], &Buffer[Index], NumBytesAtAlign4);
126 unsigned Padding = NumBytesAtAlign4 % 8;
128 if (LocalSize % 8 == 0) {
129 // Everything is set: there's no padding and we don't need to add
132 assert(LocalSize % 8 == 4);
133 // No existing padding; add in 4 bytes padding
134 memmove(&Buffer[Index - 4], &Buffer[Index], NumBytesAtAlign4);
138 assert(Padding == 4);
139 if (LocalSize % 8 == 0) {
140 // Everything is set: there's 4 bytes padding and we don't need
143 assert(LocalSize % 8 == 4);
144 // There are 4 bytes padding, but we don't need any; remove it.
145 memmove(&Buffer[Index + 4], &Buffer[Index], NumBytesAtAlign4);
151 // Forget about any padding.
152 NumBytesAtAlign4 = 0;
153 NumBytesAtAlign8 += LocalSize;
155 assert(LocalSize == 0);
160 assert(Capacity - Index == TypeLoc::getFullDataSizeForType(T) &&
161 "incorrect data size provided to CreateTypeSourceInfo!");
163 return getTemporaryTypeLoc(T);