1 //===--- TypeLocBuilder.h - Type Source Info collector ----------*- C++ -*-===//
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 file defines TypeLocBuilder, a class for building TypeLocs
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LIB_SEMA_TYPELOCBUILDER_H
15 #define LLVM_CLANG_LIB_SEMA_TYPELOCBUILDER_H
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/TypeLoc.h"
22 class TypeLocBuilder {
23 enum { InlineCapacity = 8 * sizeof(SourceLocation) };
25 /// The underlying location-data buffer. Data grows from the end
26 /// of the buffer backwards.
29 /// The capacity of the current buffer.
32 /// The index of the first occupied byte in the buffer.
36 /// The last type pushed on this builder.
40 /// The inline buffer.
41 enum { BufferMaxAlignment = alignof(void *) };
42 alignas(BufferMaxAlignment) char InlineBuffer[InlineCapacity];
43 unsigned NumBytesAtAlign4, NumBytesAtAlign8;
47 : Buffer(InlineBuffer), Capacity(InlineCapacity), Index(InlineCapacity),
48 NumBytesAtAlign4(0), NumBytesAtAlign8(0) {}
51 if (Buffer != InlineBuffer)
55 /// Ensures that this buffer has at least as much capacity as described.
56 void reserve(size_t Requested) {
57 if (Requested > Capacity)
58 // For now, match the request exactly.
62 /// Pushes a copy of the given TypeLoc onto this builder. The builder
63 /// must be empty for this to work.
64 void pushFullCopy(TypeLoc L);
66 /// Pushes space for a typespec TypeLoc. Invalidates any TypeLocs
67 /// previously retrieved from this builder.
68 TypeSpecTypeLoc pushTypeSpec(QualType T) {
69 size_t LocalSize = TypeSpecTypeLoc::LocalDataSize;
70 unsigned LocalAlign = TypeSpecTypeLoc::LocalDataAlignment;
71 return pushImpl(T, LocalSize, LocalAlign).castAs<TypeSpecTypeLoc>();
74 /// Resets this builder to the newly-initialized state.
80 NumBytesAtAlign4 = NumBytesAtAlign8 = 0;
83 /// Tell the TypeLocBuilder that the type it is storing has been
84 /// modified in some safe way that doesn't affect type-location information.
85 void TypeWasModifiedSafely(QualType T) {
91 /// Pushes space for a new TypeLoc of the given type. Invalidates
92 /// any TypeLocs previously retrieved from this builder.
93 template <class TyLocType> TyLocType push(QualType T) {
94 TyLocType Loc = TypeLoc(T, nullptr).castAs<TyLocType>();
95 size_t LocalSize = Loc.getLocalDataSize();
96 unsigned LocalAlign = Loc.getLocalDataAlignment();
97 return pushImpl(T, LocalSize, LocalAlign).castAs<TyLocType>();
100 /// Creates a TypeSourceInfo for the given type.
101 TypeSourceInfo *getTypeSourceInfo(ASTContext& Context, QualType T) {
103 assert(T == LastTy && "type doesn't match last type pushed!");
106 size_t FullDataSize = Capacity - Index;
107 TypeSourceInfo *DI = Context.CreateTypeSourceInfo(T, FullDataSize);
108 memcpy(DI->getTypeLoc().getOpaqueData(), &Buffer[Index], FullDataSize);
112 /// Copies the type-location information to the given AST context and
113 /// returns a \c TypeLoc referring into the AST context.
114 TypeLoc getTypeLocInContext(ASTContext &Context, QualType T) {
116 assert(T == LastTy && "type doesn't match last type pushed!");
119 size_t FullDataSize = Capacity - Index;
120 void *Mem = Context.Allocate(FullDataSize);
121 memcpy(Mem, &Buffer[Index], FullDataSize);
122 return TypeLoc(T, Mem);
127 TypeLoc pushImpl(QualType T, size_t LocalSize, unsigned LocalAlignment);
129 /// Grow to the given capacity.
130 void grow(size_t NewCapacity);
132 /// Retrieve a temporary TypeLoc that refers into this \c TypeLocBuilder
135 /// The resulting \c TypeLoc should only be used so long as the
136 /// \c TypeLocBuilder is active and has not had more type information
138 TypeLoc getTemporaryTypeLoc(QualType T) {
140 assert(LastTy == T && "type doesn't match last type pushed!");
142 return TypeLoc(T, &Buffer[Index]);