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