clang: include/clang/AST/ExternalASTSource.h Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14#ifndef LLVM_CLANG_AST_EXTERNALASTSOURCE_H

15#define LLVM_CLANG_AST_EXTERNALASTSOURCE_H

16

20#include "llvm/ADT/ArrayRef.h"

21#include "llvm/ADT/DenseMap.h"

22#include "llvm/ADT/IntrusiveRefCntPtr.h"

23#include "llvm/ADT/PointerUnion.h"

24#include "llvm/ADT/STLExtras.h"

25#include "llvm/ADT/SmallVector.h"

26#include "llvm/ADT/iterator.h"

27#include "llvm/Support/PointerLikeTypeTraits.h"

28#include

29#include

30#include

31#include

32#include

33#include

34#include

35#include

36

38

39class ASTConsumer;

40class ASTContext;

41class ASTSourceDescriptor;

42class CXXBaseSpecifier;

43class CXXCtorInitializer;

44class CXXRecordDecl;

45class DeclarationName;

46class FieldDecl;

47class IdentifierInfo;

48class NamedDecl;

49class ObjCInterfaceDecl;

50class RecordDecl;

51class Selector;

52class Stmt;

53class TagDecl;

54

55

56

57

58

59

60

61

64

65

66

67 uint32_t CurrentGeneration = 0;

68

69

70 static char ID;

71

72public:

75

76

77

80

81 public:

83 assert(Source);

85 }

86

89 }

90 };

91

92

93

94

95 uint32_t getGeneration() const { return CurrentGeneration; }

96

97

98

99

100

101

102

103

105

106

107

108

109

110

111

113

114

115

116

117

119

120

121

122

123

124

125

126

127

129

130

131

132

133

135

136

137

138

139

141

142

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

162

163

164

165

166

167

169

170

171

172

173

174 virtual bool

177

178

179

180

181

183

184

186

187

188 virtual std::optional getSourceDescriptor(unsigned ID);

189

191

193

194

195

196

197

198

199

200

201 virtual void

203 llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,

205

206

207

211 }

212

213

214

215

217 unsigned Length,

219

220

221

222

223

225

226

227

229

230

231

232

233

234

235

237

238

240

241

242

243

244

245

247

248

249

250

251

253

254

255

256

257

259

260

261

262

263

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

294 const RecordDecl *Record, uint64_t &Size, uint64_t &Alignment,

295 llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,

296 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,

297 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets);

298

299

300

301

302

306

309 };

310

311

312

316 return sizes;

317 }

318

320

321

322

323 virtual bool isA(const void *ClassID) const { return ClassID == &ID; }

325

326

327protected:

332

336

337

339};

340

341

342

343

344

345

346

347template<typename T, typename OffsT, T* (ExternalASTSource::*Get)(OffsT Offset)>

349

350

351

352

353

354 static constexpr size_t DataSize = std::max(sizeof(uint64_t), sizeof(T *));

355 alignas(uint64_t) alignas(T *) mutable unsigned char Data[DataSize] = {};

356

358 return Data[llvm::sys::IsBigEndianHost ? DataSize - 1 : 0];

359 }

360

361 template U &As(bool New) const {

362 unsigned char *Obj =

363 Data + (llvm::sys::IsBigEndianHost ? DataSize - sizeof(U) : 0);

364 if (New)

365 return *new (Obj) U;

366 return *std::launder(reinterpret_cast<U *>(Obj));

367 }

368

369 T *&GetPtr() const { return As<T *>(false); }

370 uint64_t &GetU64() const { return As<uint64_t>(false); }

371 void SetPtr(T *Ptr) const { As<T *>(true) = Ptr; }

372 void SetU64(uint64_t U64) const { As<uint64_t>(true) = U64; }

373

374public:

377

379 assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");

380 if (Offset == 0)

382 else

383 SetU64((Offset << 1) | 0x01);

384 }

385

388 return *this;

389 }

390

392 assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");

393 if (Offset == 0)

395 else

396 SetU64((Offset << 1) | 0x01);

397

398 return *this;

399 }

400

401

402

403

405

406

407

408

410

411

413

414

415

416

417

418

421 assert(Source &&

422 "Cannot deserialize a lazy pointer without an AST source");

423 SetPtr((Source->*Get)(OffsT(GetU64() >> 1)));

424 }

426 }

427

428

429

431

432 (void)get(Source);

434 }

435};

436

437

438

439

440template<typename Owner, typename T, void (ExternalASTSource::*Update)(Owner)>

442

443

448

451 };

452

453

454 using ValueType = llvm::PointerUnion<T, LazyData*>;

456

458

459

461

462public:

465

466

467

471

472

474

475

477 if (auto *LazyVal = Value.template dyn_cast<LazyData *>()) {

478 LazyVal->LastValue = NewValue;

479 return;

480 }

481 Value = NewValue;

482 }

483

484

486

487

489 if (auto *LazyVal = Value.template dyn_cast<LazyData *>()) {

490 if (LazyVal->LastGeneration != LazyVal->ExternalSource->getGeneration()) {

491 LazyVal->LastGeneration = LazyVal->ExternalSource->getGeneration();

492 (LazyVal->ExternalSource->*Update)(O);

493 }

494 return LazyVal->LastValue;

495 }

496 return cast(Value);

497 }

498

499

501 if (auto *LazyVal = Value.template dyn_cast<LazyData *>())

502 return LazyVal->LastValue;

503 return cast(Value);

504 }

505

509 }

510};

511

512}

513

514namespace llvm {

515

516

517

518template<typename Owner, typename T,

523

526

527 static constexpr int NumLowBitsAvailable =

529};

530

531}

532

534

535

536

537

538

539

540

541template<typename T, typename Source,

542 void (Source::*Loader)(SmallVectorImpl&),

543 unsigned LoadedStorage = 2, unsigned LocalStorage = 4>

547

548public:

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

566 : public llvm::iterator_adaptor_base<

567 iterator, int, std::random_access_iterator_tag, T, int, T *, T &> {

569

571

573 : iterator::iterator_adaptor_base(Position), Self(Self) {}

574

575 bool isLoaded() const { return this->I < 0; }

576

577 public:

579

580 typename iterator::reference operator*() const {

581 if (isLoaded())

582 return Self->Loaded.end()[this->I];

583 return Self->Local.begin()[this->I];

584 }

585 };

586

588 if (LocalOnly)

590

591 if (source)

592 (source->*Loader)(Loaded);

593 return iterator(this, -(int)Loaded.size());

594 }

595

597 return iterator(this, Local.size());

598 }

599

601 Local.push_back(LocalValue);

602 }

603

605 if (From.isLoaded() && To.isLoaded()) {

606 Loaded.erase(&*From, &*To);

607 return;

608 }

609

610 if (From.isLoaded()) {

611 Loaded.erase(&*From, Loaded.end());

612 From = begin(nullptr, true);

613 }

614

615 Local.erase(&*From, &*To);

616 }

617};

618

619

622

623

626

627

630 &ExternalASTSource::GetExternalCXXCtorInitializers>;

631

632

635 &ExternalASTSource::GetExternalCXXBaseSpecifiers>;

636

637}

638

639#endif

Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.

llvm::MachO::Record Record

ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs.

Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...

Represents a base class of a C++ class.

Represents a C++ base or member initializer.

The results of name lookup within a DeclContext.

DeclContext - This is used only as base class of specific decl types that can act as declaration cont...

Decl - This represents one declaration (or definition), e.g.

Kind

Lists the kind of concrete classes of Decl.

The name of a declaration.

RAII class for safely pairing a StartedDeserializing call with FinishedDeserializing.

Deserializing(ExternalASTSource *source)

Abstract interface for external sources of AST nodes.

static bool classof(const ExternalASTSource *S)

virtual ExtKind hasExternalDefinitions(const Decl *D)

virtual void StartTranslationUnit(ASTConsumer *Consumer)

Function that will be invoked when we begin parsing a new translation unit involving this external AS...

virtual std::optional< ASTSourceDescriptor > getSourceDescriptor(unsigned ID)

Return a descriptor for the corresponding module, if one exists.

static DeclContextLookupResult SetExternalVisibleDeclsForName(const DeclContext *DC, DeclarationName Name, ArrayRef< NamedDecl * > Decls)

virtual bool isA(const void *ClassID) const

LLVM-style RTTI.

MemoryBufferSizes getMemoryBufferSizes() const

Return the amount of memory used by memory buffers, breaking down by heap-backed versus mmap'ed memor...

uint32_t incrementGeneration(ASTContext &C)

Increment the current generation.

static DeclContextLookupResult SetNoExternalVisibleDeclsForName(const DeclContext *DC, DeclarationName Name)

virtual Module * getModule(unsigned ID)

Retrieve the module that corresponds to the given module ID.

virtual CXXCtorInitializer ** GetExternalCXXCtorInitializers(uint64_t Offset)

Resolve the offset of a set of C++ constructor initializers in the decl stream into an array of initi...

virtual void FinishedDeserializing()

Notify ExternalASTSource that we finished the deserialization of a decl or type.

virtual Selector GetExternalSelector(uint32_t ID)

Resolve a selector ID into a selector.

virtual uint32_t GetNumExternalSelectors()

Returns the number of selectors known to the external AST source.

void FindExternalLexicalDecls(const DeclContext *DC, SmallVectorImpl< Decl * > &Result)

Finds all declarations lexically contained within the given DeclContext.

virtual ~ExternalASTSource()

virtual void ReadComments()

Loads comment ranges.

virtual void CompleteRedeclChain(const Decl *D)

Gives the external AST source an opportunity to complete the redeclaration chain for a declaration.

virtual void FindFileRegionDecls(FileID File, unsigned Offset, unsigned Length, SmallVectorImpl< Decl * > &Decls)

Get the decls that are contained in a file in the Offset/Length range.

virtual bool LoadExternalSpecializations(const Decl *D, bool OnlyPartial)

Load all the external specializations for the Decl.

virtual Decl * GetExternalDecl(GlobalDeclID ID)

Resolve a declaration ID into a declaration, potentially building a new declaration.

virtual void PrintStats()

Print any statistics that have been gathered regarding the external AST source.

virtual Stmt * GetExternalDeclStmt(uint64_t Offset)

Resolve the offset of a statement in the decl stream into a statement.

virtual void StartedDeserializing()

Notify ExternalASTSource that we started deserialization of a decl or type so until FinishedDeseriali...

virtual CXXBaseSpecifier * GetExternalCXXBaseSpecifiers(uint64_t Offset)

Resolve the offset of a set of C++ base specifiers in the decl stream into an array of specifiers.

uint32_t getGeneration() const

Get the current generation of this AST source.

virtual bool layoutRecordType(const RecordDecl *Record, uint64_t &Size, uint64_t &Alignment, llvm::DenseMap< const FieldDecl *, uint64_t > &FieldOffsets, llvm::DenseMap< const CXXRecordDecl *, CharUnits > &BaseOffsets, llvm::DenseMap< const CXXRecordDecl *, CharUnits > &VirtualBaseOffsets)

Perform layout on the given record.

virtual bool FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name, const DeclContext *OriginalDC)

Find all declarations with the given name in the given context, and add them to the context by callin...

virtual void CompleteType(TagDecl *Tag)

Gives the external AST source an opportunity to complete an incomplete type.

virtual void completeVisibleDeclsMap(const DeclContext *DC)

Ensures that the table of all visible declarations inside this context is up to date.

virtual void updateOutOfDateIdentifier(const IdentifierInfo &II)

Update an out-of-date identifier.

ExternalASTSource()=default

virtual void FindExternalLexicalDecls(const DeclContext *DC, llvm::function_ref< bool(Decl::Kind)> IsKindWeWant, SmallVectorImpl< Decl * > &Result)

Finds all declarations lexically contained within the given DeclContext, after applying an optional f...

An abstract interface that should be implemented by external AST sources that also provide informatio...

An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...

One of these records is kept for each identifier that is lexed.

Iteration over the elements in the vector.

iterator::reference operator*() const

Represents a lazily-loaded vector of data.

void push_back(const T &LocalValue)

void erase(iterator From, iterator To)

iterator begin(Source *source, bool LocalOnly=false)

Describes a module or submodule.

Represents an ObjC class declaration.

Represents a struct/union/class.

Smart pointer class that efficiently represents Objective-C method names.

Stmt - This represents one statement.

Represents the declaration of a struct/union/class/enum.

The JSON file list parser is used to communicate input to InstallAPI.

@ Result

The result type of a method or function.

const FunctionProtoType * T

@ Class

The "class" keyword introduces the elaborated-type-specifier.

Diagnostic wrappers for TextAPI types for error reporting.

MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)

A cache of the value of this pointer, in the most recent generation in which we queried it.

LazyData(ExternalASTSource *Source, T Value)

ExternalASTSource * ExternalSource

A lazy value (of type T) that is within an AST node of type Owner, where the value might change in la...

static ValueType makeValue(const ASTContext &Ctx, T Value)

Create the representation of a LazyGenerationalUpdatePtr.

T getNotUpdated() const

Get the most recently computed value of this pointer without updating it.

void set(T NewValue)

Set the value of this pointer, in the current generation.

LazyGenerationalUpdatePtr(ValueType V)

LazyGenerationalUpdatePtr(const ASTContext &Ctx, T Value=T())

NotUpdatedTag

Create a pointer that is not potentially updated by later generations of the external AST source.

void setNotUpdated(T NewValue)

Set the value of this pointer, for this and all future generations.

T get(Owner O)

Get the value of this pointer, updating its owner if necessary.

void markIncomplete()

Forcibly set this pointer (which must be lazy) as needing updates.

static LazyGenerationalUpdatePtr getFromOpaqueValue(void *Ptr)

llvm::PointerUnion< T, LazyData * > ValueType

LazyGenerationalUpdatePtr(NotUpdatedTag, T Value=T())

A lazy pointer to an AST node (of base type T) that resides within an external AST source.

bool isValid() const

Whether this pointer is non-NULL.

LazyOffsetPtr(uint64_t Offset)

bool isOffset() const

Whether this pointer is currently stored as an offset.

LazyOffsetPtr & operator=(T *Ptr)

T ** getAddressOfPointer(ExternalASTSource *Source) const

Retrieve the address of the AST node pointer.

void SetU64(uint64_t U64) const

uint64_t & GetU64() const

LazyOffsetPtr & operator=(uint64_t Offset)

T * get(ExternalASTSource *Source) const

Retrieve the pointer to the AST node that this lazy pointer points to.

unsigned char Data[DataSize]

static constexpr size_t DataSize

Either a pointer to an AST node or the offset within the external AST source where the AST node can b...

void SetPtr(T *Ptr) const

unsigned char GetLSB() const

static Ptr getFromVoidPointer(void *P)

static void * getAsVoidPointer(Ptr P)