1 //===--- TypeLocBuilder.cpp - Type Source Info collector ------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This files defines TypeLocBuilder, a class for building TypeLocs
12 //===----------------------------------------------------------------------===//
14 #include "TypeLocBuilder.h"
16 using namespace clang;
18 void TypeLocBuilder::pushFullCopy(TypeLoc L) {
19 size_t Size = L.getFullDataSize();
22 SmallVector<TypeLoc, 4> TypeLocs;
25 TypeLocs.push_back(CurTL);
26 CurTL = CurTL.getNextTypeLoc();
29 for (unsigned i = 0, e = TypeLocs.size(); i < e; ++i) {
30 TypeLoc CurTL = TypeLocs[e-i-1];
31 switch (CurTL.getTypeLocClass()) {
32 #define ABSTRACT_TYPELOC(CLASS, PARENT)
33 #define TYPELOC(CLASS, PARENT) \
34 case TypeLoc::CLASS: { \
35 CLASS##TypeLoc NewTL = push<class CLASS##TypeLoc>(CurTL.getType()); \
36 memcpy(NewTL.getOpaqueData(), CurTL.getOpaqueData(), NewTL.getLocalDataSize()); \
39 #include "clang/AST/TypeLocNodes.def"
44 void TypeLocBuilder::grow(size_t NewCapacity) {
45 assert(NewCapacity > Capacity);
47 // Allocate the new buffer and copy the old data into it.
48 char *NewBuffer = new char[NewCapacity];
49 unsigned NewIndex = Index + NewCapacity - Capacity;
50 memcpy(&NewBuffer[NewIndex],
54 if (Buffer != InlineBuffer)
58 Capacity = NewCapacity;
62 TypeLoc TypeLocBuilder::pushImpl(QualType T, size_t LocalSize, unsigned LocalAlignment) {
64 QualType TLast = TypeLoc(T, nullptr).getNextTypeLoc().getType();
65 assert(TLast == LastTy &&
66 "mismatch between last type and new type's inner type");
70 assert(LocalAlignment <= BufferMaxAlignment && "Unexpected alignment");
72 // If we need to grow, grow by a factor of 2.
73 if (LocalSize > Index) {
74 size_t RequiredCapacity = Capacity + (LocalSize - Index);
75 size_t NewCapacity = Capacity * 2;
76 while (RequiredCapacity > NewCapacity)
81 // Because we're adding elements to the TypeLoc backwards, we have to
82 // do some extra work to keep everything aligned appropriately.
83 // FIXME: This algorithm is a absolute mess because every TypeLoc returned
84 // needs to be valid. Partial TypeLocs are a terrible idea.
85 // FIXME: 4 and 8 are sufficient at the moment, but it's pretty ugly to
87 if (LocalAlignment == 4) {
88 if (NumBytesAtAlign8 == 0) {
89 NumBytesAtAlign4 += LocalSize;
91 unsigned Padding = NumBytesAtAlign4 % 8;
93 if (LocalSize % 8 == 0) {
94 // Everything is set: there's no padding and we don't need to add
97 assert(LocalSize % 8 == 4);
98 // No existing padding; add in 4 bytes padding
99 memmove(&Buffer[Index - 4], &Buffer[Index], NumBytesAtAlign4);
103 assert(Padding == 4);
104 if (LocalSize % 8 == 0) {
105 // Everything is set: there's 4 bytes padding and we don't need
108 assert(LocalSize % 8 == 4);
109 // There are 4 bytes padding, but we don't need any; remove it.
110 memmove(&Buffer[Index + 4], &Buffer[Index], NumBytesAtAlign4);
114 NumBytesAtAlign4 += LocalSize;
116 } else if (LocalAlignment == 8) {
117 if (NumBytesAtAlign8 == 0) {
118 // We have not seen any 8-byte aligned element yet. We insert a padding
119 // only if the new Index is not 8-byte-aligned.
120 if ((Index - LocalSize) % 8 != 0) {
121 memmove(&Buffer[Index - 4], &Buffer[Index], NumBytesAtAlign4);
125 unsigned Padding = NumBytesAtAlign4 % 8;
127 if (LocalSize % 8 == 0) {
128 // Everything is set: there's no padding and we don't need to add
131 assert(LocalSize % 8 == 4);
132 // No existing padding; add in 4 bytes padding
133 memmove(&Buffer[Index - 4], &Buffer[Index], NumBytesAtAlign4);
137 assert(Padding == 4);
138 if (LocalSize % 8 == 0) {
139 // Everything is set: there's 4 bytes padding and we don't need
142 assert(LocalSize % 8 == 4);
143 // There are 4 bytes padding, but we don't need any; remove it.
144 memmove(&Buffer[Index + 4], &Buffer[Index], NumBytesAtAlign4);
150 // Forget about any padding.
151 NumBytesAtAlign4 = 0;
152 NumBytesAtAlign8 += LocalSize;
154 assert(LocalSize == 0);
159 assert(Capacity - Index == TypeLoc::getFullDataSizeForType(T) &&
160 "incorrect data size provided to CreateTypeSourceInfo!");
162 return getTemporaryTypeLoc(T);