[docs] Fix build-docs.sh
[llvm-project.git] / clang / lib / Sema / TypeLocBuilder.h
blob9e7422ec9906c1cb8e975d806a21c7cad0975646
1 //===--- TypeLocBuilder.h - Type Source Info collector ----------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines TypeLocBuilder, a class for building TypeLocs
10 // bottom-up.
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"
20 namespace clang {
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.
27 char *Buffer;
29 /// The capacity of the current buffer.
30 size_t Capacity;
32 /// The index of the first occupied byte in the buffer.
33 size_t Index;
35 #ifndef NDEBUG
36 /// The last type pushed on this builder.
37 QualType LastTy;
38 #endif
40 /// The inline buffer.
41 enum { BufferMaxAlignment = alignof(void *) };
42 alignas(BufferMaxAlignment) char InlineBuffer[InlineCapacity];
43 unsigned NumBytesAtAlign4;
44 bool AtAlign8;
46 public:
47 TypeLocBuilder()
48 : Buffer(InlineBuffer), Capacity(InlineCapacity), Index(InlineCapacity),
49 NumBytesAtAlign4(0), AtAlign8(false) {}
51 ~TypeLocBuilder() {
52 if (Buffer != InlineBuffer)
53 delete[] Buffer;
56 /// Ensures that this buffer has at least as much capacity as described.
57 void reserve(size_t Requested) {
58 if (Requested > Capacity)
59 // For now, match the request exactly.
60 grow(Requested);
63 /// Pushes a copy of the given TypeLoc onto this builder. The builder
64 /// must be empty for this to work.
65 void pushFullCopy(TypeLoc L);
67 /// Pushes space for a typespec TypeLoc. Invalidates any TypeLocs
68 /// previously retrieved from this builder.
69 TypeSpecTypeLoc pushTypeSpec(QualType T) {
70 size_t LocalSize = TypeSpecTypeLoc::LocalDataSize;
71 unsigned LocalAlign = TypeSpecTypeLoc::LocalDataAlignment;
72 return pushImpl(T, LocalSize, LocalAlign).castAs<TypeSpecTypeLoc>();
75 /// Resets this builder to the newly-initialized state.
76 void clear() {
77 #ifndef NDEBUG
78 LastTy = QualType();
79 #endif
80 Index = Capacity;
81 NumBytesAtAlign4 = 0;
82 AtAlign8 = false;
85 /// Tell the TypeLocBuilder that the type it is storing has been
86 /// modified in some safe way that doesn't affect type-location information.
87 void TypeWasModifiedSafely(QualType T) {
88 #ifndef NDEBUG
89 LastTy = T;
90 #endif
93 /// Pushes space for a new TypeLoc of the given type. Invalidates
94 /// any TypeLocs previously retrieved from this builder.
95 template <class TyLocType> TyLocType push(QualType T) {
96 TyLocType Loc = TypeLoc(T, nullptr).castAs<TyLocType>();
97 size_t LocalSize = Loc.getLocalDataSize();
98 unsigned LocalAlign = Loc.getLocalDataAlignment();
99 return pushImpl(T, LocalSize, LocalAlign).castAs<TyLocType>();
102 /// Creates a TypeSourceInfo for the given type.
103 TypeSourceInfo *getTypeSourceInfo(ASTContext& Context, QualType T) {
104 #ifndef NDEBUG
105 assert(T == LastTy && "type doesn't match last type pushed!");
106 #endif
108 size_t FullDataSize = Capacity - Index;
109 TypeSourceInfo *DI = Context.CreateTypeSourceInfo(T, FullDataSize);
110 memcpy(DI->getTypeLoc().getOpaqueData(), &Buffer[Index], FullDataSize);
111 return DI;
114 /// Copies the type-location information to the given AST context and
115 /// returns a \c TypeLoc referring into the AST context.
116 TypeLoc getTypeLocInContext(ASTContext &Context, QualType T) {
117 #ifndef NDEBUG
118 assert(T == LastTy && "type doesn't match last type pushed!");
119 #endif
121 size_t FullDataSize = Capacity - Index;
122 void *Mem = Context.Allocate(FullDataSize);
123 memcpy(Mem, &Buffer[Index], FullDataSize);
124 return TypeLoc(T, Mem);
127 private:
129 TypeLoc pushImpl(QualType T, size_t LocalSize, unsigned LocalAlignment);
131 /// Grow to the given capacity.
132 void grow(size_t NewCapacity);
134 /// Retrieve a temporary TypeLoc that refers into this \c TypeLocBuilder
135 /// object.
137 /// The resulting \c TypeLoc should only be used so long as the
138 /// \c TypeLocBuilder is active and has not had more type information
139 /// pushed into it.
140 TypeLoc getTemporaryTypeLoc(QualType T) {
141 #ifndef NDEBUG
142 assert(LastTy == T && "type doesn't match last type pushed!");
143 #endif
144 return TypeLoc(T, &Buffer[Index]);
150 #endif