1 //===----- SemaCodeCompletion.h ------ Code completion support ------------===// 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 /// \file 9 /// This file declares facilities that support code completion. 10 /// 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_CLANG_SEMA_SEMACODECOMPLETION_H 14 #define LLVM_CLANG_SEMA_SEMACODECOMPLETION_H 15 16 #include "clang/AST/ASTFwd.h" 17 #include "clang/AST/Type.h" 18 #include "clang/Basic/AttributeCommonInfo.h" 19 #include "clang/Basic/IdentifierTable.h" 20 #include "clang/Basic/LLVM.h" 21 #include "clang/Basic/SourceLocation.h" 22 #include "clang/Lex/ModuleLoader.h" 23 #include "clang/Sema/CodeCompleteConsumer.h" 24 #include "clang/Sema/DeclSpec.h" 25 #include "clang/Sema/Designator.h" 26 #include "clang/Sema/HeuristicResolver.h" 27 #include "clang/Sema/Ownership.h" 28 #include "clang/Sema/SemaBase.h" 29 #include "llvm/ADT/StringRef.h" 30 #include <optional> 31 32 namespace clang { 33 class DeclGroupRef; 34 class MacroInfo; 35 class Scope; 36 class TemplateName; 37 38 class SemaCodeCompletion : public SemaBase { 39 public: 40 SemaCodeCompletion(Sema &S, CodeCompleteConsumer *CompletionConsumer); 41 42 using TemplateTy = OpaquePtr<TemplateName>; 43 using DeclGroupPtrTy = OpaquePtr<DeclGroupRef>; 44 45 /// Code-completion consumer. 46 CodeCompleteConsumer *CodeCompleter; 47 HeuristicResolver Resolver; 48 49 /// Describes the context in which code completion occurs. 50 enum ParserCompletionContext { 51 /// Code completion occurs at top-level or namespace context. 52 PCC_Namespace, 53 /// Code completion occurs within a class, struct, or union. 54 PCC_Class, 55 /// Code completion occurs within an Objective-C interface, protocol, 56 /// or category. 57 PCC_ObjCInterface, 58 /// Code completion occurs within an Objective-C implementation or 59 /// category implementation 60 PCC_ObjCImplementation, 61 /// Code completion occurs within the list of instance variables 62 /// in an Objective-C interface, protocol, category, or implementation. 63 PCC_ObjCInstanceVariableList, 64 /// Code completion occurs following one or more template 65 /// headers. 66 PCC_Template, 67 /// Code completion occurs following one or more template 68 /// headers within a class. 69 PCC_MemberTemplate, 70 /// Code completion occurs within an expression. 71 PCC_Expression, 72 /// Code completion occurs within a statement, which may 73 /// also be an expression or a declaration. 74 PCC_Statement, 75 /// Code completion occurs at the beginning of the 76 /// initialization statement (or expression) in a for loop. 77 PCC_ForInit, 78 /// Code completion occurs within the condition of an if, 79 /// while, switch, or for statement. 80 PCC_Condition, 81 /// Code completion occurs within the body of a function on a 82 /// recovery path, where we do not have a specific handle on our position 83 /// in the grammar. 84 PCC_RecoveryInFunction, 85 /// Code completion occurs where only a type is permitted. 86 PCC_Type, 87 /// Code completion occurs in a parenthesized expression, which 88 /// might also be a type cast. 89 PCC_ParenthesizedExpression, 90 /// Code completion occurs within a sequence of declaration 91 /// specifiers within a function, method, or block. 92 PCC_LocalDeclarationSpecifiers, 93 /// Code completion occurs at top-level in a REPL session 94 PCC_TopLevelOrExpression, 95 }; 96 97 void CodeCompleteModuleImport(SourceLocation ImportLoc, ModuleIdPath Path); 98 void CodeCompleteOrdinaryName(Scope *S, 99 ParserCompletionContext CompletionContext); 100 void CodeCompleteDeclSpec(Scope *S, DeclSpec &DS, bool AllowNonIdentifiers, 101 bool AllowNestedNameSpecifiers); 102 103 struct CodeCompleteExpressionData; 104 void CodeCompleteExpression(Scope *S, const CodeCompleteExpressionData &Data); 105 void CodeCompleteExpression(Scope *S, QualType PreferredType, 106 bool IsParenthesized = false); 107 void CodeCompleteMemberReferenceExpr(Scope *S, Expr *Base, Expr *OtherOpBase, 108 SourceLocation OpLoc, bool IsArrow, 109 bool IsBaseExprStatement, 110 QualType PreferredType); 111 void CodeCompletePostfixExpression(Scope *S, ExprResult LHS, 112 QualType PreferredType); 113 void CodeCompleteTag(Scope *S, unsigned TagSpec); 114 void CodeCompleteTypeQualifiers(DeclSpec &DS); 115 void CodeCompleteFunctionQualifiers(DeclSpec &DS, Declarator &D, 116 const VirtSpecifiers *VS = nullptr); 117 void CodeCompleteBracketDeclarator(Scope *S); 118 void CodeCompleteCase(Scope *S); 119 enum class AttributeCompletion { 120 Attribute, 121 Scope, 122 None, 123 }; 124 void CodeCompleteAttribute( 125 AttributeCommonInfo::Syntax Syntax, 126 AttributeCompletion Completion = AttributeCompletion::Attribute, 127 const IdentifierInfo *Scope = nullptr); 128 /// Determines the preferred type of the current function argument, by 129 /// examining the signatures of all possible overloads. 130 /// Returns null if unknown or ambiguous, or if code completion is off. 131 /// 132 /// If the code completion point has been reached, also reports the function 133 /// signatures that were considered. 134 /// 135 /// FIXME: rename to GuessCallArgumentType to reduce confusion. 136 QualType ProduceCallSignatureHelp(Expr *Fn, ArrayRef<Expr *> Args, 137 SourceLocation OpenParLoc); 138 QualType ProduceConstructorSignatureHelp(QualType Type, SourceLocation Loc, 139 ArrayRef<Expr *> Args, 140 SourceLocation OpenParLoc, 141 bool Braced); 142 QualType ProduceCtorInitMemberSignatureHelp( 143 Decl *ConstructorDecl, CXXScopeSpec SS, ParsedType TemplateTypeTy, 144 ArrayRef<Expr *> ArgExprs, IdentifierInfo *II, SourceLocation OpenParLoc, 145 bool Braced); 146 QualType ProduceTemplateArgumentSignatureHelp( 147 TemplateTy, ArrayRef<ParsedTemplateArgument>, SourceLocation LAngleLoc); 148 void CodeCompleteInitializer(Scope *S, Decl *D); 149 /// Trigger code completion for a record of \p BaseType. \p InitExprs are 150 /// expressions in the initializer list seen so far and \p D is the current 151 /// Designation being parsed. 152 void CodeCompleteDesignator(const QualType BaseType, 153 llvm::ArrayRef<Expr *> InitExprs, 154 const Designation &D); 155 void CodeCompleteAfterIf(Scope *S, bool IsBracedThen); 156 157 void CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS, bool EnteringContext, 158 bool IsUsingDeclaration, QualType BaseType, 159 QualType PreferredType); 160 void CodeCompleteUsing(Scope *S); 161 void CodeCompleteUsingDirective(Scope *S); 162 void CodeCompleteNamespaceDecl(Scope *S); 163 void CodeCompleteNamespaceAliasDecl(Scope *S); 164 void CodeCompleteOperatorName(Scope *S); 165 void CodeCompleteConstructorInitializer( 166 Decl *Constructor, ArrayRef<CXXCtorInitializer *> Initializers); 167 168 void CodeCompleteLambdaIntroducer(Scope *S, LambdaIntroducer &Intro, 169 bool AfterAmpersand); 170 void CodeCompleteAfterFunctionEquals(Declarator &D); 171 172 void CodeCompleteObjCAtDirective(Scope *S); 173 void CodeCompleteObjCAtVisibility(Scope *S); 174 void CodeCompleteObjCAtStatement(Scope *S); 175 void CodeCompleteObjCAtExpression(Scope *S); 176 void CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS); 177 void CodeCompleteObjCPropertyGetter(Scope *S); 178 void CodeCompleteObjCPropertySetter(Scope *S); 179 void CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS, 180 bool IsParameter); 181 void CodeCompleteObjCMessageReceiver(Scope *S); 182 void CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc, 183 ArrayRef<const IdentifierInfo *> SelIdents, 184 bool AtArgumentExpression); 185 void CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver, 186 ArrayRef<const IdentifierInfo *> SelIdents, 187 bool AtArgumentExpression, 188 bool IsSuper = false); 189 void CodeCompleteObjCInstanceMessage( 190 Scope *S, Expr *Receiver, ArrayRef<const IdentifierInfo *> SelIdents, 191 bool AtArgumentExpression, ObjCInterfaceDecl *Super = nullptr); 192 void CodeCompleteObjCForCollection(Scope *S, DeclGroupPtrTy IterationVar); 193 void CodeCompleteObjCSelector(Scope *S, 194 ArrayRef<const IdentifierInfo *> SelIdents); 195 void 196 CodeCompleteObjCProtocolReferences(ArrayRef<IdentifierLocPair> Protocols); 197 void CodeCompleteObjCProtocolDecl(Scope *S); 198 void CodeCompleteObjCInterfaceDecl(Scope *S); 199 void CodeCompleteObjCClassForwardDecl(Scope *S); 200 void CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName, 201 SourceLocation ClassNameLoc); 202 void CodeCompleteObjCImplementationDecl(Scope *S); 203 void CodeCompleteObjCInterfaceCategory(Scope *S, IdentifierInfo *ClassName, 204 SourceLocation ClassNameLoc); 205 void CodeCompleteObjCImplementationCategory(Scope *S, 206 IdentifierInfo *ClassName, 207 SourceLocation ClassNameLoc); 208 void CodeCompleteObjCPropertyDefinition(Scope *S); 209 void CodeCompleteObjCPropertySynthesizeIvar(Scope *S, 210 IdentifierInfo *PropertyName); 211 void CodeCompleteObjCMethodDecl(Scope *S, 212 std::optional<bool> IsInstanceMethod, 213 ParsedType ReturnType); 214 void CodeCompleteObjCMethodDeclSelector( 215 Scope *S, bool IsInstanceMethod, bool AtParameterName, 216 ParsedType ReturnType, ArrayRef<const IdentifierInfo *> SelIdents); 217 void CodeCompleteObjCClassPropertyRefExpr(Scope *S, 218 const IdentifierInfo &ClassName, 219 SourceLocation ClassNameLoc, 220 bool IsBaseExprStatement); 221 void CodeCompletePreprocessorDirective(bool InConditional); 222 void CodeCompleteInPreprocessorConditionalExclusion(Scope *S); 223 void CodeCompletePreprocessorMacroName(bool IsDefinition); 224 void CodeCompletePreprocessorExpression(); 225 void CodeCompletePreprocessorMacroArgument(Scope *S, IdentifierInfo *Macro, 226 MacroInfo *MacroInfo, 227 unsigned Argument); 228 void CodeCompleteIncludedFile(llvm::StringRef Dir, bool IsAngled); 229 void CodeCompleteNaturalLanguage(); 230 void CodeCompleteAvailabilityPlatformName(); 231 void 232 GatherGlobalCodeCompletions(CodeCompletionAllocator &Allocator, 233 CodeCompletionTUInfo &CCTUInfo, 234 SmallVectorImpl<CodeCompletionResult> &Results); 235 }; 236 237 } // namespace clang 238 239 #endif // LLVM_CLANG_SEMA_SEMACODECOMPLETION_H 240