]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/AST/ExternalASTSource.h
Merge clang trunk r321017 to contrib/llvm/tools/clang.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / AST / ExternalASTSource.h
1 //===- ExternalASTSource.h - Abstract External AST Interface ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the ExternalASTSource interface, which enables
11 //  construction of AST nodes from some external source.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CLANG_AST_EXTERNALASTSOURCE_H
16 #define LLVM_CLANG_AST_EXTERNALASTSOURCE_H
17
18 #include "clang/AST/CharUnits.h"
19 #include "clang/AST/DeclBase.h"
20 #include "clang/Basic/LLVM.h"
21 #include "clang/Basic/Module.h"
22 #include "clang/Basic/SourceLocation.h"
23 #include "llvm/ADT/ArrayRef.h"
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/IntrusiveRefCntPtr.h"
26 #include "llvm/ADT/Optional.h"
27 #include "llvm/ADT/PointerUnion.h"
28 #include "llvm/ADT/STLExtras.h"
29 #include "llvm/ADT/SmallVector.h"
30 #include "llvm/ADT/StringRef.h"
31 #include "llvm/ADT/iterator.h"
32 #include "llvm/Support/PointerLikeTypeTraits.h"
33 #include <cassert>
34 #include <cstddef>
35 #include <cstdint>
36 #include <iterator>
37 #include <string>
38 #include <utility>
39
40 namespace clang {
41
42 class ASTConsumer;
43 class ASTContext;
44 class CXXBaseSpecifier;
45 class CXXCtorInitializer;
46 class CXXRecordDecl;
47 class DeclarationName;
48 class FieldDecl;
49 class IdentifierInfo;
50 class NamedDecl;
51 class ObjCInterfaceDecl;
52 class RecordDecl;
53 class Selector;
54 class Stmt;
55 class TagDecl;
56
57 /// \brief Abstract interface for external sources of AST nodes.
58 ///
59 /// External AST sources provide AST nodes constructed from some
60 /// external source, such as a precompiled header. External AST
61 /// sources can resolve types and declarations from abstract IDs into
62 /// actual type and declaration nodes, and read parts of declaration
63 /// contexts.
64 class ExternalASTSource : public RefCountedBase<ExternalASTSource> {
65   friend class ExternalSemaSource;
66
67   /// Generation number for this external AST source. Must be increased
68   /// whenever we might have added new redeclarations for existing decls.
69   uint32_t CurrentGeneration = 0;
70
71   /// \brief Whether this AST source also provides information for
72   /// semantic analysis.
73   bool SemaSource = false;
74
75 public:
76   ExternalASTSource() = default;
77   virtual ~ExternalASTSource();
78
79   /// \brief RAII class for safely pairing a StartedDeserializing call
80   /// with FinishedDeserializing.
81   class Deserializing {
82     ExternalASTSource *Source;
83
84   public:
85     explicit Deserializing(ExternalASTSource *source) : Source(source) {
86       assert(Source);
87       Source->StartedDeserializing();
88     }
89
90     ~Deserializing() {
91       Source->FinishedDeserializing();
92     }
93   };
94
95   /// \brief Get the current generation of this AST source. This number
96   /// is incremented each time the AST source lazily extends an existing
97   /// entity.
98   uint32_t getGeneration() const { return CurrentGeneration; }
99
100   /// \brief Resolve a declaration ID into a declaration, potentially
101   /// building a new declaration.
102   ///
103   /// This method only needs to be implemented if the AST source ever
104   /// passes back decl sets as VisibleDeclaration objects.
105   ///
106   /// The default implementation of this method is a no-op.
107   virtual Decl *GetExternalDecl(uint32_t ID);
108
109   /// \brief Resolve a selector ID into a selector.
110   ///
111   /// This operation only needs to be implemented if the AST source
112   /// returns non-zero for GetNumKnownSelectors().
113   ///
114   /// The default implementation of this method is a no-op.
115   virtual Selector GetExternalSelector(uint32_t ID);
116
117   /// \brief Returns the number of selectors known to the external AST
118   /// source.
119   ///
120   /// The default implementation of this method is a no-op.
121   virtual uint32_t GetNumExternalSelectors();
122
123   /// \brief Resolve the offset of a statement in the decl stream into
124   /// a statement.
125   ///
126   /// This operation is meant to be used via a LazyOffsetPtr.  It only
127   /// needs to be implemented if the AST source uses methods like
128   /// FunctionDecl::setLazyBody when building decls.
129   ///
130   /// The default implementation of this method is a no-op.
131   virtual Stmt *GetExternalDeclStmt(uint64_t Offset);
132
133   /// \brief Resolve the offset of a set of C++ constructor initializers in
134   /// the decl stream into an array of initializers.
135   ///
136   /// The default implementation of this method is a no-op.
137   virtual CXXCtorInitializer **GetExternalCXXCtorInitializers(uint64_t Offset);
138
139   /// \brief Resolve the offset of a set of C++ base specifiers in the decl
140   /// stream into an array of specifiers.
141   ///
142   /// The default implementation of this method is a no-op.
143   virtual CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset);
144
145   /// \brief Update an out-of-date identifier.
146   virtual void updateOutOfDateIdentifier(IdentifierInfo &II) {}
147
148   /// \brief Find all declarations with the given name in the given context,
149   /// and add them to the context by calling SetExternalVisibleDeclsForName
150   /// or SetNoExternalVisibleDeclsForName.
151   /// \return \c true if any declarations might have been found, \c false if
152   /// we definitely have no declarations with tbis name.
153   ///
154   /// The default implementation of this method is a no-op returning \c false.
155   virtual bool
156   FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name);
157
158   /// \brief Ensures that the table of all visible declarations inside this
159   /// context is up to date.
160   ///
161   /// The default implementation of this function is a no-op.
162   virtual void completeVisibleDeclsMap(const DeclContext *DC);
163
164   /// \brief Retrieve the module that corresponds to the given module ID.
165   virtual Module *getModule(unsigned ID) { return nullptr; }
166
167   /// Abstracts clang modules and precompiled header files and holds
168   /// everything needed to generate debug info for an imported module
169   /// or PCH.
170   class ASTSourceDescriptor {
171     StringRef PCHModuleName;
172     StringRef Path;
173     StringRef ASTFile;
174     ASTFileSignature Signature;
175     const Module *ClangModule = nullptr;
176
177   public:
178     ASTSourceDescriptor() = default;
179     ASTSourceDescriptor(StringRef Name, StringRef Path, StringRef ASTFile,
180                         ASTFileSignature Signature)
181         : PCHModuleName(std::move(Name)), Path(std::move(Path)),
182           ASTFile(std::move(ASTFile)), Signature(Signature) {}
183     ASTSourceDescriptor(const Module &M);
184
185     std::string getModuleName() const;
186     StringRef getPath() const { return Path; }
187     StringRef getASTFile() const { return ASTFile; }
188     ASTFileSignature getSignature() const { return Signature; }
189     const Module *getModuleOrNull() const { return ClangModule; }
190   };
191
192   /// Return a descriptor for the corresponding module, if one exists.
193   virtual llvm::Optional<ASTSourceDescriptor> getSourceDescriptor(unsigned ID);
194
195   enum ExtKind { EK_Always, EK_Never, EK_ReplyHazy };
196
197   virtual ExtKind hasExternalDefinitions(const Decl *D);
198
199   /// \brief Finds all declarations lexically contained within the given
200   /// DeclContext, after applying an optional filter predicate.
201   ///
202   /// \param IsKindWeWant a predicate function that returns true if the passed
203   /// declaration kind is one we are looking for.
204   ///
205   /// The default implementation of this method is a no-op.
206   virtual void
207   FindExternalLexicalDecls(const DeclContext *DC,
208                            llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
209                            SmallVectorImpl<Decl *> &Result);
210
211   /// \brief Finds all declarations lexically contained within the given
212   /// DeclContext.
213   void FindExternalLexicalDecls(const DeclContext *DC,
214                                 SmallVectorImpl<Decl *> &Result) {
215     FindExternalLexicalDecls(DC, [](Decl::Kind) { return true; }, Result);
216   }
217
218   /// \brief Get the decls that are contained in a file in the Offset/Length
219   /// range. \p Length can be 0 to indicate a point at \p Offset instead of
220   /// a range.
221   virtual void FindFileRegionDecls(FileID File, unsigned Offset,
222                                    unsigned Length,
223                                    SmallVectorImpl<Decl *> &Decls);
224
225   /// \brief Gives the external AST source an opportunity to complete
226   /// the redeclaration chain for a declaration. Called each time we
227   /// need the most recent declaration of a declaration after the
228   /// generation count is incremented.
229   virtual void CompleteRedeclChain(const Decl *D);
230
231   /// \brief Gives the external AST source an opportunity to complete
232   /// an incomplete type.
233   virtual void CompleteType(TagDecl *Tag);
234
235   /// \brief Gives the external AST source an opportunity to complete an
236   /// incomplete Objective-C class.
237   ///
238   /// This routine will only be invoked if the "externally completed" bit is
239   /// set on the ObjCInterfaceDecl via the function
240   /// \c ObjCInterfaceDecl::setExternallyCompleted().
241   virtual void CompleteType(ObjCInterfaceDecl *Class);
242
243   /// \brief Loads comment ranges.
244   virtual void ReadComments();
245
246   /// \brief Notify ExternalASTSource that we started deserialization of
247   /// a decl or type so until FinishedDeserializing is called there may be
248   /// decls that are initializing. Must be paired with FinishedDeserializing.
249   ///
250   /// The default implementation of this method is a no-op.
251   virtual void StartedDeserializing();
252
253   /// \brief Notify ExternalASTSource that we finished the deserialization of
254   /// a decl or type. Must be paired with StartedDeserializing.
255   ///
256   /// The default implementation of this method is a no-op.
257   virtual void FinishedDeserializing();
258
259   /// \brief Function that will be invoked when we begin parsing a new
260   /// translation unit involving this external AST source.
261   ///
262   /// The default implementation of this method is a no-op.
263   virtual void StartTranslationUnit(ASTConsumer *Consumer);
264
265   /// \brief Print any statistics that have been gathered regarding
266   /// the external AST source.
267   ///
268   /// The default implementation of this method is a no-op.
269   virtual void PrintStats();
270   
271   /// \brief Perform layout on the given record.
272   ///
273   /// This routine allows the external AST source to provide an specific 
274   /// layout for a record, overriding the layout that would normally be
275   /// constructed. It is intended for clients who receive specific layout
276   /// details rather than source code (such as LLDB). The client is expected
277   /// to fill in the field offsets, base offsets, virtual base offsets, and
278   /// complete object size.
279   ///
280   /// \param Record The record whose layout is being requested.
281   ///
282   /// \param Size The final size of the record, in bits.
283   ///
284   /// \param Alignment The final alignment of the record, in bits.
285   ///
286   /// \param FieldOffsets The offset of each of the fields within the record,
287   /// expressed in bits. All of the fields must be provided with offsets.
288   ///
289   /// \param BaseOffsets The offset of each of the direct, non-virtual base
290   /// classes. If any bases are not given offsets, the bases will be laid 
291   /// out according to the ABI.
292   ///
293   /// \param VirtualBaseOffsets The offset of each of the virtual base classes
294   /// (either direct or not). If any bases are not given offsets, the bases will be laid 
295   /// out according to the ABI.
296   /// 
297   /// \returns true if the record layout was provided, false otherwise.
298   virtual bool layoutRecordType(
299       const RecordDecl *Record, uint64_t &Size, uint64_t &Alignment,
300       llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
301       llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
302       llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets);
303
304   //===--------------------------------------------------------------------===//
305   // Queries for performance analysis.
306   //===--------------------------------------------------------------------===//
307   
308   struct MemoryBufferSizes {
309     size_t malloc_bytes;
310     size_t mmap_bytes;
311     
312     MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
313         : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
314   };
315   
316   /// Return the amount of memory used by memory buffers, breaking down
317   /// by heap-backed versus mmap'ed memory.
318   MemoryBufferSizes getMemoryBufferSizes() const {
319     MemoryBufferSizes sizes(0, 0);
320     getMemoryBufferSizes(sizes);
321     return sizes;
322   }
323
324   virtual void getMemoryBufferSizes(MemoryBufferSizes &sizes) const;
325
326 protected:
327   static DeclContextLookupResult
328   SetExternalVisibleDeclsForName(const DeclContext *DC,
329                                  DeclarationName Name,
330                                  ArrayRef<NamedDecl*> Decls);
331
332   static DeclContextLookupResult
333   SetNoExternalVisibleDeclsForName(const DeclContext *DC,
334                                    DeclarationName Name);
335
336   /// \brief Increment the current generation.
337   uint32_t incrementGeneration(ASTContext &C);
338 };
339
340 /// \brief A lazy pointer to an AST node (of base type T) that resides
341 /// within an external AST source.
342 ///
343 /// The AST node is identified within the external AST source by a
344 /// 63-bit offset, and can be retrieved via an operation on the
345 /// external AST source itself.
346 template<typename T, typename OffsT, T* (ExternalASTSource::*Get)(OffsT Offset)>
347 struct LazyOffsetPtr {
348   /// \brief Either a pointer to an AST node or the offset within the
349   /// external AST source where the AST node can be found.
350   ///
351   /// If the low bit is clear, a pointer to the AST node. If the low
352   /// bit is set, the upper 63 bits are the offset.
353   mutable uint64_t Ptr = 0;
354
355 public:
356   LazyOffsetPtr() = default;
357   explicit LazyOffsetPtr(T *Ptr) : Ptr(reinterpret_cast<uint64_t>(Ptr)) {}
358
359   explicit LazyOffsetPtr(uint64_t Offset) : Ptr((Offset << 1) | 0x01) {
360     assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
361     if (Offset == 0)
362       Ptr = 0;
363   }
364
365   LazyOffsetPtr &operator=(T *Ptr) {
366     this->Ptr = reinterpret_cast<uint64_t>(Ptr);
367     return *this;
368   }
369
370   LazyOffsetPtr &operator=(uint64_t Offset) {
371     assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
372     if (Offset == 0)
373       Ptr = 0;
374     else
375       Ptr = (Offset << 1) | 0x01;
376
377     return *this;
378   }
379
380   /// \brief Whether this pointer is non-NULL.
381   ///
382   /// This operation does not require the AST node to be deserialized.
383   explicit operator bool() const { return Ptr != 0; }
384
385   /// \brief Whether this pointer is non-NULL.
386   ///
387   /// This operation does not require the AST node to be deserialized.
388   bool isValid() const { return Ptr != 0; }
389
390   /// \brief Whether this pointer is currently stored as an offset.
391   bool isOffset() const { return Ptr & 0x01; }
392
393   /// \brief Retrieve the pointer to the AST node that this lazy pointer
394   ///
395   /// \param Source the external AST source.
396   ///
397   /// \returns a pointer to the AST node.
398   T* get(ExternalASTSource *Source) const {
399     if (isOffset()) {
400       assert(Source &&
401              "Cannot deserialize a lazy pointer without an AST source");
402       Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
403     }
404     return reinterpret_cast<T*>(Ptr);
405   }
406 };
407
408 /// \brief A lazy value (of type T) that is within an AST node of type Owner,
409 /// where the value might change in later generations of the external AST
410 /// source.
411 template<typename Owner, typename T, void (ExternalASTSource::*Update)(Owner)>
412 struct LazyGenerationalUpdatePtr {
413   /// A cache of the value of this pointer, in the most recent generation in
414   /// which we queried it.
415   struct LazyData {
416     ExternalASTSource *ExternalSource;
417     uint32_t LastGeneration = 0;
418     T LastValue;
419
420     LazyData(ExternalASTSource *Source, T Value)
421         : ExternalSource(Source), LastValue(Value) {}
422   };
423
424   // Our value is represented as simply T if there is no external AST source.
425   using ValueType = llvm::PointerUnion<T, LazyData*>;
426   ValueType Value;
427
428   LazyGenerationalUpdatePtr(ValueType V) : Value(V) {}
429
430   // Defined in ASTContext.h
431   static ValueType makeValue(const ASTContext &Ctx, T Value);
432
433 public:
434   explicit LazyGenerationalUpdatePtr(const ASTContext &Ctx, T Value = T())
435       : Value(makeValue(Ctx, Value)) {}
436
437   /// Create a pointer that is not potentially updated by later generations of
438   /// the external AST source.
439   enum NotUpdatedTag { NotUpdated };
440   LazyGenerationalUpdatePtr(NotUpdatedTag, T Value = T())
441       : Value(Value) {}
442
443   /// Forcibly set this pointer (which must be lazy) as needing updates.
444   void markIncomplete() {
445     Value.template get<LazyData *>()->LastGeneration = 0;
446   }
447
448   /// Set the value of this pointer, in the current generation.
449   void set(T NewValue) {
450     if (LazyData *LazyVal = Value.template dyn_cast<LazyData*>()) {
451       LazyVal->LastValue = NewValue;
452       return;
453     }
454     Value = NewValue;
455   }
456
457   /// Set the value of this pointer, for this and all future generations.
458   void setNotUpdated(T NewValue) { Value = NewValue; }
459
460   /// Get the value of this pointer, updating its owner if necessary.
461   T get(Owner O) {
462     if (LazyData *LazyVal = Value.template dyn_cast<LazyData*>()) {
463       if (LazyVal->LastGeneration != LazyVal->ExternalSource->getGeneration()) {
464         LazyVal->LastGeneration = LazyVal->ExternalSource->getGeneration();
465         (LazyVal->ExternalSource->*Update)(O);
466       }
467       return LazyVal->LastValue;
468     }
469     return Value.template get<T>();
470   }
471
472   /// Get the most recently computed value of this pointer without updating it.
473   T getNotUpdated() const {
474     if (LazyData *LazyVal = Value.template dyn_cast<LazyData*>())
475       return LazyVal->LastValue;
476     return Value.template get<T>();
477   }
478
479   void *getOpaqueValue() { return Value.getOpaqueValue(); }
480   static LazyGenerationalUpdatePtr getFromOpaqueValue(void *Ptr) {
481     return LazyGenerationalUpdatePtr(ValueType::getFromOpaqueValue(Ptr));
482   }
483 };
484
485 } // namespace clang
486
487 /// Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be
488 /// placed into a PointerUnion.
489 namespace llvm {
490
491 template<typename Owner, typename T,
492          void (clang::ExternalASTSource::*Update)(Owner)>
493 struct PointerLikeTypeTraits<
494     clang::LazyGenerationalUpdatePtr<Owner, T, Update>> {
495   using Ptr = clang::LazyGenerationalUpdatePtr<Owner, T, Update>;
496
497   static void *getAsVoidPointer(Ptr P) { return P.getOpaqueValue(); }
498   static Ptr getFromVoidPointer(void *P) { return Ptr::getFromOpaqueValue(P); }
499
500   enum {
501     NumLowBitsAvailable = PointerLikeTypeTraits<T>::NumLowBitsAvailable - 1
502   };
503 };
504
505 } // namespace llvm
506
507 namespace clang {
508
509 /// \brief Represents a lazily-loaded vector of data.
510 ///
511 /// The lazily-loaded vector of data contains data that is partially loaded
512 /// from an external source and partially added by local translation. The 
513 /// items loaded from the external source are loaded lazily, when needed for
514 /// iteration over the complete vector.
515 template<typename T, typename Source, 
516          void (Source::*Loader)(SmallVectorImpl<T>&),
517          unsigned LoadedStorage = 2, unsigned LocalStorage = 4>
518 class LazyVector {
519   SmallVector<T, LoadedStorage> Loaded;
520   SmallVector<T, LocalStorage> Local;
521
522 public:
523   /// Iteration over the elements in the vector.
524   ///
525   /// In a complete iteration, the iterator walks the range [-M, N),
526   /// where negative values are used to indicate elements
527   /// loaded from the external source while non-negative values are used to
528   /// indicate elements added via \c push_back().
529   /// However, to provide iteration in source order (for, e.g., chained
530   /// precompiled headers), dereferencing the iterator flips the negative
531   /// values (corresponding to loaded entities), so that position -M
532   /// corresponds to element 0 in the loaded entities vector, position -M+1
533   /// corresponds to element 1 in the loaded entities vector, etc. This
534   /// gives us a reasonably efficient, source-order walk.
535   ///
536   /// We define this as a wrapping iterator around an int. The
537   /// iterator_adaptor_base class forwards the iterator methods to basic integer
538   /// arithmetic.
539   class iterator
540       : public llvm::iterator_adaptor_base<
541             iterator, int, std::random_access_iterator_tag, T, int, T *, T &> {
542     friend class LazyVector;
543
544     LazyVector *Self;
545
546     iterator(LazyVector *Self, int Position)
547         : iterator::iterator_adaptor_base(Position), Self(Self) {}
548
549     bool isLoaded() const { return this->I < 0; }
550
551   public:
552     iterator() : iterator(nullptr, 0) {}
553
554     typename iterator::reference operator*() const {
555       if (isLoaded())
556         return Self->Loaded.end()[this->I];
557       return Self->Local.begin()[this->I];
558     }
559   };
560
561   iterator begin(Source *source, bool LocalOnly = false) {
562     if (LocalOnly)
563       return iterator(this, 0);
564     
565     if (source)
566       (source->*Loader)(Loaded);
567     return iterator(this, -(int)Loaded.size());
568   }
569   
570   iterator end() {
571     return iterator(this, Local.size());
572   }
573   
574   void push_back(const T& LocalValue) {
575     Local.push_back(LocalValue);
576   }
577   
578   void erase(iterator From, iterator To) {
579     if (From.isLoaded() && To.isLoaded()) {
580       Loaded.erase(&*From, &*To);
581       return;
582     }
583
584     if (From.isLoaded()) {
585       Loaded.erase(&*From, Loaded.end());
586       From = begin(nullptr, true);
587     }
588
589     Local.erase(&*From, &*To);
590   }
591 };
592
593 /// \brief A lazy pointer to a statement.
594 using LazyDeclStmtPtr =
595     LazyOffsetPtr<Stmt, uint64_t, &ExternalASTSource::GetExternalDeclStmt>;
596
597 /// \brief A lazy pointer to a declaration.
598 using LazyDeclPtr =
599     LazyOffsetPtr<Decl, uint32_t, &ExternalASTSource::GetExternalDecl>;
600
601 /// \brief A lazy pointer to a set of CXXCtorInitializers.
602 using LazyCXXCtorInitializersPtr =
603     LazyOffsetPtr<CXXCtorInitializer *, uint64_t,
604                   &ExternalASTSource::GetExternalCXXCtorInitializers>;
605
606 /// \brief A lazy pointer to a set of CXXBaseSpecifiers.
607 using LazyCXXBaseSpecifiersPtr =
608     LazyOffsetPtr<CXXBaseSpecifier, uint64_t,
609                   &ExternalASTSource::GetExternalCXXBaseSpecifiers>;
610
611 } // namespace clang
612
613 #endif // LLVM_CLANG_AST_EXTERNALASTSOURCE_H