Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/include/clang/AST/AbstractBasicReader.h
Line
Count
Source (jump to first uncovered line)
1
//==--- AbstractBasiceReader.h - Abstract basic value deserialization -----===//
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
#ifndef LLVM_CLANG_AST_ABSTRACTBASICREADER_H
10
#define LLVM_CLANG_AST_ABSTRACTBASICREADER_H
11
12
#include "clang/AST/DeclTemplate.h"
13
#include <optional>
14
15
namespace clang {
16
namespace serialization {
17
18
template <class T>
19
0
inline T makeNullableFromOptional(const std::optional<T> &value) {
20
0
  return (value ? *value : T());
21
0
}
Unexecuted instantiation: clang::QualType clang::serialization::makeNullableFromOptional<clang::QualType>(std::__1::optional<clang::QualType> const&)
Unexecuted instantiation: clang::TemplateName clang::serialization::makeNullableFromOptional<clang::TemplateName>(std::__1::optional<clang::TemplateName> const&)
22
23
0
template <class T> inline T *makePointerFromOptional(std::optional<T *> value) {
24
0
  return value.value_or(nullptr);
25
0
}
Unexecuted instantiation: clang::ConceptDecl* clang::serialization::makePointerFromOptional<clang::ConceptDecl>(std::__1::optional<clang::ConceptDecl*>)
Unexecuted instantiation: clang::TagDecl* clang::serialization::makePointerFromOptional<clang::TagDecl>(std::__1::optional<clang::TagDecl*>)
Unexecuted instantiation: clang::TemplateTypeParmDecl* clang::serialization::makePointerFromOptional<clang::TemplateTypeParmDecl>(std::__1::optional<clang::TemplateTypeParmDecl*>)
26
27
// PropertyReader is a class concept that requires the following method:
28
//   BasicReader find(llvm::StringRef propertyName);
29
// where BasicReader is some class conforming to the BasicReader concept.
30
// An abstract AST-node reader is created with a PropertyReader and
31
// performs a sequence of calls like so:
32
//   propertyReader.find(propertyName).read##TypeName()
33
// to read the properties of the node it is deserializing.
34
35
// BasicReader is a class concept that requires methods like:
36
//   ValueType read##TypeName();
37
// where TypeName is the name of a PropertyType node from PropertiesBase.td
38
// and ValueType is the corresponding C++ type name.  The read method may
39
// require one or more buffer arguments.
40
//
41
// In addition to the concrete type names, BasicReader is expected to
42
// implement these methods:
43
//
44
//   template <class EnumType>
45
//   void writeEnum(T value);
46
//
47
//     Reads an enum value from the current property.  EnumType will always
48
//     be an enum type.  Only necessary if the BasicReader doesn't provide
49
//     type-specific readers for all the enum types.
50
//
51
//   template <class ValueType>
52
//   std::optional<ValueType> writeOptional();
53
//
54
//     Reads an optional value from the current property.
55
//
56
//   template <class ValueType>
57
//   ArrayRef<ValueType> readArray(llvm::SmallVectorImpl<ValueType> &buffer);
58
//
59
//     Reads an array of values from the current property.
60
//
61
//   PropertyReader readObject();
62
//
63
//     Reads an object from the current property; the returned property
64
//     reader will be subjected to a sequence of property reads and then
65
//     discarded before any other properties are reader from the "outer"
66
//     property reader (which need not be the same type).  The sub-reader
67
//     will be used as if with the following code:
68
//
69
//       {
70
//         auto &&widget = W.find("widget").readObject();
71
//         auto kind = widget.find("kind").readWidgetKind();
72
//         auto declaration = widget.find("declaration").readDeclRef();
73
//         return Widget(kind, declaration);
74
//       }
75
76
// ReadDispatcher does type-based forwarding to one of the read methods
77
// on the BasicReader passed in:
78
//
79
// template <class ValueType>
80
// struct ReadDispatcher {
81
//   template <class BasicReader, class... BufferTypes>
82
//   static ValueType read(BasicReader &R, BufferTypes &&...);
83
// };
84
85
// BasicReaderBase provides convenience implementations of the read methods
86
// for EnumPropertyType and SubclassPropertyType types that just defer to
87
// the "underlying" implementations (for UInt32 and the base class,
88
// respectively).
89
//
90
// template <class Impl>
91
// class BasicReaderBase {
92
// protected:
93
//   BasicReaderBase(ASTContext &ctx);
94
//   Impl &asImpl();
95
// public:
96
//   ASTContext &getASTContext();
97
//   ...
98
// };
99
100
// The actual classes are auto-generated; see ClangASTPropertiesEmitter.cpp.
101
#include "clang/AST/AbstractBasicReader.inc"
102
103
/// DataStreamBasicReader provides convenience implementations for many
104
/// BasicReader methods based on the assumption that the
105
/// ultimate reader implementation is based on a variable-length stream
106
/// of unstructured data (like Clang's module files).  It is designed
107
/// to pair with DataStreamBasicWriter.
108
///
109
/// This class can also act as a PropertyReader, implementing find("...")
110
/// by simply forwarding to itself.
111
///
112
/// Unimplemented methods:
113
///   readBool
114
///   readUInt32
115
///   readUInt64
116
///   readIdentifier
117
///   readSelector
118
///   readSourceLocation
119
///   readQualType
120
///   readStmtRef
121
///   readDeclRef
122
template <class Impl>
123
class DataStreamBasicReader : public BasicReaderBase<Impl> {
124
protected:
125
  using BasicReaderBase<Impl>::asImpl;
126
0
  DataStreamBasicReader(ASTContext &ctx) : BasicReaderBase<Impl>(ctx) {}
127
128
public:
129
  using BasicReaderBase<Impl>::getASTContext;
130
131
  /// Implement property-find by ignoring it.  We rely on properties being
132
  /// serialized and deserialized in a reliable order instead.
133
0
  Impl &find(const char *propertyName) {
134
0
    return asImpl();
135
0
  }
136
137
  template <class T>
138
0
  T readEnum() {
139
0
    return T(asImpl().readUInt32());
140
0
  }
Unexecuted instantiation: clang::TemplateArgument::ArgKind clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::TemplateArgument::ArgKind>()
Unexecuted instantiation: clang::TemplateName::NameKind clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::TemplateName::NameKind>()
Unexecuted instantiation: clang::OverloadedOperatorKind clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::OverloadedOperatorKind>()
Unexecuted instantiation: clang::ArraySizeModifier clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::ArraySizeModifier>()
Unexecuted instantiation: clang::attr::Kind clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::attr::Kind>()
Unexecuted instantiation: clang::BuiltinType::Kind clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::BuiltinType::Kind>()
Unexecuted instantiation: clang::AutoTypeKeyword clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::AutoTypeKeyword>()
Unexecuted instantiation: clang::ElaboratedTypeKeyword clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::ElaboratedTypeKeyword>()
Unexecuted instantiation: clang::VectorKind clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::VectorKind>()
Unexecuted instantiation: clang::CallingConv clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::CallingConv>()
Unexecuted instantiation: clang::RefQualifierKind clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::RefQualifierKind>()
Unexecuted instantiation: clang::TypeOfKind clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::TypeOfKind>()
Unexecuted instantiation: clang::UnaryTransformType::UTTKind clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::UnaryTransformType::UTTKind>()
Unexecuted instantiation: clang::DeclarationName::NameKind clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::DeclarationName::NameKind>()
Unexecuted instantiation: clang::NestedNameSpecifier::SpecifierKind clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::NestedNameSpecifier::SpecifierKind>()
Unexecuted instantiation: clang::OpenMPReductionClauseModifier clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::OpenMPReductionClauseModifier>()
Unexecuted instantiation: clang::OpenMPDependClauseKind clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::OpenMPDependClauseKind>()
Unexecuted instantiation: llvm::omp::Clause clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<llvm::omp::Clause>()
Unexecuted instantiation: clang::OpenMPLastprivateModifier clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::OpenMPLastprivateModifier>()
Unexecuted instantiation: clang::OpenMPDeviceClauseModifier clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::OpenMPDeviceClauseModifier>()
Unexecuted instantiation: clang::OpenMPGrainsizeClauseModifier clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::OpenMPGrainsizeClauseModifier>()
Unexecuted instantiation: clang::OpenMPNumTasksClauseModifier clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::OpenMPNumTasksClauseModifier>()
Unexecuted instantiation: clang::OpenMPOrderClauseKind clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::OpenMPOrderClauseKind>()
Unexecuted instantiation: clang::OpenMPOrderClauseModifier clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::OpenMPOrderClauseModifier>()
Unexecuted instantiation: clang::OpenMPBindClauseKind clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::OpenMPBindClauseKind>()
Unexecuted instantiation: llvm::omp::TraitSet clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<llvm::omp::TraitSet>()
Unexecuted instantiation: llvm::omp::TraitSelector clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<llvm::omp::TraitSelector>()
Unexecuted instantiation: llvm::omp::TraitProperty clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<llvm::omp::TraitProperty>()
Unexecuted instantiation: clang::APValue::ValueKind clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::APValue::ValueKind>()
Unexecuted instantiation: clang::OMPDeclareTargetDeclAttr::MapTypeTy clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::OMPDeclareTargetDeclAttr::MapTypeTy>()
Unexecuted instantiation: clang::OMPDeclareTargetDeclAttr::DevTypeTy clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<clang::OMPDeclareTargetDeclAttr::DevTypeTy>()
Unexecuted instantiation: llvm::omp::Directive clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readEnum<llvm::omp::Directive>()
141
142
  // Implement object reading by forwarding to this, collapsing the
143
  // structure into a single data stream.
144
0
  Impl &readObject() { return asImpl(); }
145
146
  template <class T>
147
0
  llvm::ArrayRef<T> readArray(llvm::SmallVectorImpl<T> &buffer) {
148
0
    assert(buffer.empty());
149
150
0
    uint32_t size = asImpl().readUInt32();
151
0
    buffer.reserve(size);
152
153
0
    for (uint32_t i = 0; i != size; ++i) {
154
0
      buffer.push_back(ReadDispatcher<T>::read(asImpl()));
155
0
    }
156
0
    return buffer;
157
0
  }
Unexecuted instantiation: llvm::ArrayRef<clang::NamedDecl*> clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readArray<clang::NamedDecl*>(llvm::SmallVectorImpl<clang::NamedDecl*>&)
Unexecuted instantiation: llvm::ArrayRef<clang::TemplateArgument> clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readArray<clang::TemplateArgument>(llvm::SmallVectorImpl<clang::TemplateArgument>&)
Unexecuted instantiation: llvm::ArrayRef<clang::QualType> clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readArray<clang::QualType>(llvm::SmallVectorImpl<clang::QualType>&)
Unexecuted instantiation: llvm::ArrayRef<clang::FunctionType::ExtParameterInfo> clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readArray<clang::FunctionType::ExtParameterInfo>(llvm::SmallVectorImpl<clang::FunctionType::ExtParameterInfo>&)
Unexecuted instantiation: llvm::ArrayRef<clang::ObjCProtocolDecl*> clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readArray<clang::ObjCProtocolDecl*>(llvm::SmallVectorImpl<clang::ObjCProtocolDecl*>&)
Unexecuted instantiation: llvm::ArrayRef<clang::APValue> clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readArray<clang::APValue>(llvm::SmallVectorImpl<clang::APValue>&)
Unexecuted instantiation: llvm::ArrayRef<clang::CXXRecordDecl*> clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readArray<clang::CXXRecordDecl*>(llvm::SmallVectorImpl<clang::CXXRecordDecl*>&)
158
159
  template <class T, class... Args>
160
0
  std::optional<T> readOptional(Args &&...args) {
161
0
    return UnpackOptionalValue<T>::unpack(
162
0
             ReadDispatcher<T>::read(asImpl(), std::forward<Args>(args)...));
163
0
  }
Unexecuted instantiation: std::__1::optional<clang::IdentifierInfo*> clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readOptional<clang::IdentifierInfo*>()
Unexecuted instantiation: std::__1::optional<unsigned int> clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readOptional<unsigned int>()
Unexecuted instantiation: std::__1::optional<clang::QualType> clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readOptional<clang::QualType>()
Unexecuted instantiation: std::__1::optional<clang::ConceptDecl*> clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readOptional<clang::ConceptDecl*>()
Unexecuted instantiation: std::__1::optional<clang::TemplateName> clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readOptional<clang::TemplateName>()
Unexecuted instantiation: std::__1::optional<clang::TagDecl*> clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readOptional<clang::TagDecl*>()
Unexecuted instantiation: std::__1::optional<clang::TemplateTypeParmDecl*> clang::serialization::DataStreamBasicReader<clang::ASTRecordReader>::readOptional<clang::TemplateTypeParmDecl*>()
164
165
0
  llvm::APSInt readAPSInt() {
166
0
    bool isUnsigned = asImpl().readBool();
167
0
    llvm::APInt value = asImpl().readAPInt();
168
0
    return llvm::APSInt(std::move(value), isUnsigned);
169
0
  }
170
171
0
  llvm::APInt readAPInt() {
172
0
    unsigned bitWidth = asImpl().readUInt32();
173
0
    unsigned numWords = llvm::APInt::getNumWords(bitWidth);
174
0
    llvm::SmallVector<uint64_t, 4> data;
175
0
    for (uint32_t i = 0; i != numWords; ++i)
176
0
      data.push_back(asImpl().readUInt64());
177
0
    return llvm::APInt(bitWidth, numWords, &data[0]);
178
0
  }
179
180
0
  llvm::FixedPointSemantics readFixedPointSemantics() {
181
0
    unsigned width = asImpl().readUInt32();
182
0
    unsigned scale = asImpl().readUInt32();
183
0
    unsigned tmp = asImpl().readUInt32();
184
0
    bool isSigned = tmp & 0x1;
185
0
    bool isSaturated = tmp & 0x2;
186
0
    bool hasUnsignedPadding = tmp & 0x4;
187
0
    return llvm::FixedPointSemantics(width, scale, isSigned, isSaturated,
188
0
                                     hasUnsignedPadding);
189
0
  }
190
191
  APValue::LValuePathSerializationHelper readLValuePathSerializationHelper(
192
0
      SmallVectorImpl<APValue::LValuePathEntry> &path) {
193
0
    auto origTy = asImpl().readQualType();
194
0
    auto elemTy = origTy;
195
0
    unsigned pathLength = asImpl().readUInt32();
196
0
    for (unsigned i = 0; i < pathLength; ++i) {
197
0
      if (elemTy->template getAs<RecordType>()) {
198
0
        unsigned int_ = asImpl().readUInt32();
199
0
        Decl *decl = asImpl().template readDeclAs<Decl>();
200
0
        if (auto *recordDecl = dyn_cast<CXXRecordDecl>(decl))
201
0
          elemTy = getASTContext().getRecordType(recordDecl);
202
0
        else
203
0
          elemTy = cast<ValueDecl>(decl)->getType();
204
0
        path.push_back(
205
0
            APValue::LValuePathEntry(APValue::BaseOrMemberType(decl, int_)));
206
0
      } else {
207
0
        elemTy = getASTContext().getAsArrayType(elemTy)->getElementType();
208
0
        path.push_back(
209
0
            APValue::LValuePathEntry::ArrayIndex(asImpl().readUInt32()));
210
0
      }
211
0
    }
212
0
    return APValue::LValuePathSerializationHelper(path, origTy);
213
0
  }
214
215
  Qualifiers readQualifiers() {
216
    static_assert(sizeof(Qualifiers().getAsOpaqueValue()) <= sizeof(uint32_t),
217
                  "update this if the value size changes");
218
    uint32_t value = asImpl().readUInt32();
219
    return Qualifiers::fromOpaqueValue(value);
220
  }
221
222
  FunctionProtoType::ExceptionSpecInfo
223
0
  readExceptionSpecInfo(llvm::SmallVectorImpl<QualType> &buffer) {
224
0
    FunctionProtoType::ExceptionSpecInfo esi;
225
0
    esi.Type = ExceptionSpecificationType(asImpl().readUInt32());
226
0
    if (esi.Type == EST_Dynamic) {
227
0
      esi.Exceptions = asImpl().template readArray<QualType>(buffer);
228
0
    } else if (isComputedNoexcept(esi.Type)) {
229
0
      esi.NoexceptExpr = asImpl().readExprRef();
230
0
    } else if (esi.Type == EST_Uninstantiated) {
231
0
      esi.SourceDecl = asImpl().readFunctionDeclRef();
232
0
      esi.SourceTemplate = asImpl().readFunctionDeclRef();
233
0
    } else if (esi.Type == EST_Unevaluated) {
234
0
      esi.SourceDecl = asImpl().readFunctionDeclRef();
235
0
    }
236
0
    return esi;
237
0
  }
238
239
0
  FunctionProtoType::ExtParameterInfo readExtParameterInfo() {
240
0
    static_assert(sizeof(FunctionProtoType::ExtParameterInfo().getOpaqueValue())
241
0
                    <= sizeof(uint32_t),
242
0
                  "opaque value doesn't fit into uint32_t");
243
0
    uint32_t value = asImpl().readUInt32();
244
0
    return FunctionProtoType::ExtParameterInfo::getFromOpaqueValue(value);
245
0
  }
246
247
0
  NestedNameSpecifier *readNestedNameSpecifier() {
248
0
    auto &ctx = getASTContext();
249
250
    // We build this up iteratively.
251
0
    NestedNameSpecifier *cur = nullptr;
252
253
0
    uint32_t depth = asImpl().readUInt32();
254
0
    for (uint32_t i = 0; i != depth; ++i) {
255
0
      auto kind = asImpl().readNestedNameSpecifierKind();
256
0
      switch (kind) {
257
0
      case NestedNameSpecifier::Identifier:
258
0
        cur = NestedNameSpecifier::Create(ctx, cur,
259
0
                                          asImpl().readIdentifier());
260
0
        continue;
261
262
0
      case NestedNameSpecifier::Namespace:
263
0
        cur = NestedNameSpecifier::Create(ctx, cur,
264
0
                                          asImpl().readNamespaceDeclRef());
265
0
        continue;
266
267
0
      case NestedNameSpecifier::NamespaceAlias:
268
0
        cur = NestedNameSpecifier::Create(ctx, cur,
269
0
                                     asImpl().readNamespaceAliasDeclRef());
270
0
        continue;
271
272
0
      case NestedNameSpecifier::TypeSpec:
273
0
      case NestedNameSpecifier::TypeSpecWithTemplate:
274
0
        cur = NestedNameSpecifier::Create(ctx, cur,
275
0
                          kind == NestedNameSpecifier::TypeSpecWithTemplate,
276
0
                          asImpl().readQualType().getTypePtr());
277
0
        continue;
278
279
0
      case NestedNameSpecifier::Global:
280
0
        cur = NestedNameSpecifier::GlobalSpecifier(ctx);
281
0
        continue;
282
283
0
      case NestedNameSpecifier::Super:
284
0
        cur = NestedNameSpecifier::SuperSpecifier(ctx,
285
0
                                            asImpl().readCXXRecordDeclRef());
286
0
        continue;
287
0
      }
288
0
      llvm_unreachable("bad nested name specifier kind");
289
0
    }
290
291
0
    return cur;
292
0
  }
293
};
294
295
} // end namespace serialization
296
} // end namespace clang
297
298
#endif