Home | History | Annotate | Download | only in Sema
      1 //===--- ExternalSemaSource.h - External Sema 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 ExternalSemaSource interface.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 #ifndef LLVM_CLANG_SEMA_EXTERNALSEMASOURCE_H
     14 #define LLVM_CLANG_SEMA_EXTERNALSEMASOURCE_H
     15 
     16 #include "clang/AST/ExternalASTSource.h"
     17 #include "clang/AST/Type.h"
     18 #include "clang/Sema/TypoCorrection.h"
     19 #include "clang/Sema/Weak.h"
     20 #include "llvm/ADT/MapVector.h"
     21 #include <utility>
     22 
     23 namespace llvm {
     24 template <class T, unsigned n> class SmallSetVector;
     25 }
     26 
     27 namespace clang {
     28 
     29 class CXXConstructorDecl;
     30 class CXXDeleteExpr;
     31 class CXXRecordDecl;
     32 class DeclaratorDecl;
     33 class LookupResult;
     34 struct ObjCMethodList;
     35 class Scope;
     36 class Sema;
     37 class TypedefNameDecl;
     38 class ValueDecl;
     39 class VarDecl;
     40 struct LateParsedTemplate;
     41 
     42 /// \brief A simple structure that captures a vtable use for the purposes of
     43 /// the \c ExternalSemaSource.
     44 struct ExternalVTableUse {
     45   CXXRecordDecl *Record;
     46   SourceLocation Location;
     47   bool DefinitionRequired;
     48 };
     49 
     50 /// \brief An abstract interface that should be implemented by
     51 /// external AST sources that also provide information for semantic
     52 /// analysis.
     53 class ExternalSemaSource : public ExternalASTSource {
     54 public:
     55   ExternalSemaSource() {
     56     ExternalASTSource::SemaSource = true;
     57   }
     58 
     59   ~ExternalSemaSource() override;
     60 
     61   /// \brief Initialize the semantic source with the Sema instance
     62   /// being used to perform semantic analysis on the abstract syntax
     63   /// tree.
     64   virtual void InitializeSema(Sema &S) {}
     65 
     66   /// \brief Inform the semantic consumer that Sema is no longer available.
     67   virtual void ForgetSema() {}
     68 
     69   /// \brief Load the contents of the global method pool for a given
     70   /// selector.
     71   virtual void ReadMethodPool(Selector Sel);
     72 
     73   /// Load the contents of the global method pool for a given
     74   /// selector if necessary.
     75   virtual void updateOutOfDateSelector(Selector Sel);
     76 
     77   /// \brief Load the set of namespaces that are known to the external source,
     78   /// which will be used during typo correction.
     79   virtual void ReadKnownNamespaces(
     80                            SmallVectorImpl<NamespaceDecl *> &Namespaces);
     81 
     82   /// \brief Load the set of used but not defined functions or variables with
     83   /// internal linkage, or used but not defined internal functions.
     84   virtual void
     85   ReadUndefinedButUsed(llvm::MapVector<NamedDecl *, SourceLocation> &Undefined);
     86 
     87   virtual void ReadMismatchingDeleteExpressions(llvm::MapVector<
     88       FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &);
     89 
     90   /// \brief Do last resort, unqualified lookup on a LookupResult that
     91   /// Sema cannot find.
     92   ///
     93   /// \param R a LookupResult that is being recovered.
     94   ///
     95   /// \param S the Scope of the identifier occurrence.
     96   ///
     97   /// \return true to tell Sema to recover using the LookupResult.
     98   virtual bool LookupUnqualified(LookupResult &R, Scope *S) { return false; }
     99 
    100   /// \brief Read the set of tentative definitions known to the external Sema
    101   /// source.
    102   ///
    103   /// The external source should append its own tentative definitions to the
    104   /// given vector of tentative definitions. Note that this routine may be
    105   /// invoked multiple times; the external source should take care not to
    106   /// introduce the same declarations repeatedly.
    107   virtual void ReadTentativeDefinitions(
    108                                   SmallVectorImpl<VarDecl *> &TentativeDefs) {}
    109 
    110   /// \brief Read the set of unused file-scope declarations known to the
    111   /// external Sema source.
    112   ///
    113   /// The external source should append its own unused, filed-scope to the
    114   /// given vector of declarations. Note that this routine may be
    115   /// invoked multiple times; the external source should take care not to
    116   /// introduce the same declarations repeatedly.
    117   virtual void ReadUnusedFileScopedDecls(
    118                  SmallVectorImpl<const DeclaratorDecl *> &Decls) {}
    119 
    120   /// \brief Read the set of delegating constructors known to the
    121   /// external Sema source.
    122   ///
    123   /// The external source should append its own delegating constructors to the
    124   /// given vector of declarations. Note that this routine may be
    125   /// invoked multiple times; the external source should take care not to
    126   /// introduce the same declarations repeatedly.
    127   virtual void ReadDelegatingConstructors(
    128                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {}
    129 
    130   /// \brief Read the set of ext_vector type declarations known to the
    131   /// external Sema source.
    132   ///
    133   /// The external source should append its own ext_vector type declarations to
    134   /// the given vector of declarations. Note that this routine may be
    135   /// invoked multiple times; the external source should take care not to
    136   /// introduce the same declarations repeatedly.
    137   virtual void ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {}
    138 
    139   /// \brief Read the set of potentially unused typedefs known to the source.
    140   ///
    141   /// The external source should append its own potentially unused local
    142   /// typedefs to the given vector of declarations. Note that this routine may
    143   /// be invoked multiple times; the external source should take care not to
    144   /// introduce the same declarations repeatedly.
    145   virtual void ReadUnusedLocalTypedefNameCandidates(
    146       llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {}
    147 
    148   /// \brief Read the set of referenced selectors known to the
    149   /// external Sema source.
    150   ///
    151   /// The external source should append its own referenced selectors to the
    152   /// given vector of selectors. Note that this routine
    153   /// may be invoked multiple times; the external source should take care not
    154   /// to introduce the same selectors repeatedly.
    155   virtual void ReadReferencedSelectors(
    156                  SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {}
    157 
    158   /// \brief Read the set of weak, undeclared identifiers known to the
    159   /// external Sema source.
    160   ///
    161   /// The external source should append its own weak, undeclared identifiers to
    162   /// the given vector. Note that this routine may be invoked multiple times;
    163   /// the external source should take care not to introduce the same identifiers
    164   /// repeatedly.
    165   virtual void ReadWeakUndeclaredIdentifiers(
    166                  SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WI) {}
    167 
    168   /// \brief Read the set of used vtables known to the external Sema source.
    169   ///
    170   /// The external source should append its own used vtables to the given
    171   /// vector. Note that this routine may be invoked multiple times; the external
    172   /// source should take care not to introduce the same vtables repeatedly.
    173   virtual void ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {}
    174 
    175   /// \brief Read the set of pending instantiations known to the external
    176   /// Sema source.
    177   ///
    178   /// The external source should append its own pending instantiations to the
    179   /// given vector. Note that this routine may be invoked multiple times; the
    180   /// external source should take care not to introduce the same instantiations
    181   /// repeatedly.
    182   virtual void ReadPendingInstantiations(
    183                  SmallVectorImpl<std::pair<ValueDecl *,
    184                                            SourceLocation> > &Pending) {}
    185 
    186   /// \brief Read the set of late parsed template functions for this source.
    187   ///
    188   /// The external source should insert its own late parsed template functions
    189   /// into the map. Note that this routine may be invoked multiple times; the
    190   /// external source should take care not to introduce the same map entries
    191   /// repeatedly.
    192   virtual void ReadLateParsedTemplates(
    193       llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
    194           &LPTMap) {}
    195 
    196   /// \copydoc Sema::CorrectTypo
    197   /// \note LookupKind must correspond to a valid Sema::LookupNameKind
    198   ///
    199   /// ExternalSemaSource::CorrectTypo is always given the first chance to
    200   /// correct a typo (really, to offer suggestions to repair a failed lookup).
    201   /// It will even be called when SpellChecking is turned off or after a
    202   /// fatal error has already been detected.
    203   virtual TypoCorrection CorrectTypo(const DeclarationNameInfo &Typo,
    204                                      int LookupKind, Scope *S, CXXScopeSpec *SS,
    205                                      CorrectionCandidateCallback &CCC,
    206                                      DeclContext *MemberContext,
    207                                      bool EnteringContext,
    208                                      const ObjCObjectPointerType *OPT) {
    209     return TypoCorrection();
    210   }
    211 
    212   /// \brief Produces a diagnostic note if the external source contains a
    213   /// complete definition for \p T.
    214   ///
    215   /// \param Loc the location at which a complete type was required but not
    216   /// provided
    217   ///
    218   /// \param T the \c QualType that should have been complete at \p Loc
    219   ///
    220   /// \return true if a diagnostic was produced, false otherwise.
    221   virtual bool MaybeDiagnoseMissingCompleteType(SourceLocation Loc,
    222                                                 QualType T) {
    223     return false;
    224   }
    225 
    226   // isa/cast/dyn_cast support
    227   static bool classof(const ExternalASTSource *Source) {
    228     return Source->SemaSource;
    229   }
    230 };
    231 
    232 } // end namespace clang
    233 
    234 #endif
    235