blob: d59d1bab0620f13b00089478d999994ee37f88f7 [file] [log] [blame]
//===--- IndexSymbol.cpp --------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/Index/IndexSymbol.h"
#include "swift/AST/AST.h"
using namespace swift;
using namespace swift::index;
static void setFuncSymbolInfo(const FuncDecl *FD, SymbolInfo &sym) {
sym.Kind = SymbolKind::Function;
if (FD->getAttrs().hasAttribute<IBActionAttr>())
sym.Properties |= SymbolProperty::IBAnnotated;
if (FD->getDeclContext()->isTypeContext()) {
if (FD->isStatic()) {
if (FD->getCorrectStaticSpelling() == StaticSpellingKind::KeywordClass)
sym.Kind = SymbolKind::ClassMethod;
else
sym.Kind = SymbolKind::StaticMethod;
} else {
sym.Kind = SymbolKind::InstanceMethod;
}
}
if (FD->isAccessor()) {
sym.SubKind = getSubKindForAccessor(FD->getAccessorKind());
return;
}
if (auto *op = FD->getOperatorDecl()) {
switch (op->getKind()) {
case DeclKind::PrefixOperator:
sym.SubKind = SymbolSubKind::SwiftPrefixOperator;
return;
case DeclKind::PostfixOperator:
sym.SubKind = SymbolSubKind::SwiftPostfixOperator;
return;
case DeclKind::InfixOperator:
sym.SubKind = SymbolSubKind::SwiftInfixOperator;
return;
default:
llvm_unreachable("unexpected operator kind");
}
}
}
static SymbolKind getVarSymbolKind(const VarDecl *VD) {
auto *DC = VD->getDeclContext();
if (DC->isTypeContext()) {
if (VD->isStatic()) {
if (VD->getCorrectStaticSpelling() == StaticSpellingKind::KeywordClass)
return SymbolKind::ClassProperty;
return SymbolKind::StaticProperty;
}
return SymbolKind::InstanceProperty;
}
assert(!DC->isLocalContext() && "local variable seen while indexing");
return SymbolKind::Variable;
}
SymbolInfo index::getSymbolInfoForDecl(const Decl *D) {
SymbolInfo info{ SymbolKind::Unknown, SymbolSubKind::None,
SymbolPropertySet(), SymbolLanguage::Swift };
switch (D->getKind()) {
case DeclKind::Enum: info.Kind = SymbolKind::Enum; break;
case DeclKind::Struct: info.Kind = SymbolKind::Struct; break;
case DeclKind::Class: info.Kind = SymbolKind::Class; break;
case DeclKind::Protocol: info.Kind = SymbolKind::Protocol; break;
case DeclKind::Extension: {
info.Kind = SymbolKind::Extension;
auto *ED = cast<ExtensionDecl>(D);
if (!ED->getExtendedType())
break;
NominalTypeDecl *NTD = ED->getExtendedType()->getAnyNominal();
if (!NTD)
break;
if (isa<StructDecl>(NTD))
info.SubKind = SymbolSubKind::SwiftExtensionOfStruct;
else if (isa<ClassDecl>(NTD))
info.SubKind = SymbolSubKind::SwiftExtensionOfClass;
else if (isa<EnumDecl>(NTD))
info.SubKind = SymbolSubKind::SwiftExtensionOfEnum;
else if (isa<ProtocolDecl>(NTD))
info.SubKind = SymbolSubKind::SwiftExtensionOfProtocol;
assert(info.SubKind != SymbolSubKind::None);
break;
}
case DeclKind::TypeAlias: info.Kind = SymbolKind::TypeAlias; break;
case DeclKind::AssociatedType:
info.Kind = SymbolKind::TypeAlias;
info.SubKind = SymbolSubKind::SwiftAssociatedType;
break;
case DeclKind::GenericTypeParam:
info.Kind = SymbolKind::TypeAlias;
info.SubKind = SymbolSubKind::SwiftGenericTypeParam;
break;
case DeclKind::EnumElement: info.Kind = SymbolKind::EnumConstant; break;
case DeclKind::Subscript:
info.Kind = SymbolKind::InstanceProperty;
info.SubKind = SymbolSubKind::SwiftSubscript;
break;
case DeclKind::Constructor: info.Kind = SymbolKind::Constructor; break;
case DeclKind::Destructor: info.Kind = SymbolKind::Destructor; break;
case DeclKind::Param:
info.Kind = SymbolKind::Parameter;
break;
case DeclKind::Func:
setFuncSymbolInfo(cast<FuncDecl>(D), info);
break;
case DeclKind::Var:
info.Kind = getVarSymbolKind(cast<VarDecl>(D));
if (D->getAttrs().hasAttribute<IBOutletAttr>())
info.Properties |= SymbolProperty::IBAnnotated;
if (D->getAttrs().hasAttribute<GKInspectableAttr>())
info.Properties |= SymbolProperty::GKInspectable;
break;
default:
break;
}
return info;
}
SymbolSubKind index::getSubKindForAccessor(AccessorKind AK) {
switch (AK) {
case AccessorKind::NotAccessor: return SymbolSubKind::None;
case AccessorKind::IsGetter: return SymbolSubKind::AccessorGetter;
case AccessorKind::IsSetter: return SymbolSubKind::AccessorSetter;
case AccessorKind::IsWillSet: return SymbolSubKind::SwiftAccessorWillSet;
case AccessorKind::IsDidSet: return SymbolSubKind::SwiftAccessorDidSet;
case AccessorKind::IsAddressor: return SymbolSubKind::SwiftAccessorAddressor;
case AccessorKind::IsMutableAddressor:
return SymbolSubKind::SwiftAccessorMutableAddressor;
case AccessorKind::IsMaterializeForSet:
llvm_unreachable("unexpected MaterializeForSet");
}
llvm_unreachable("Unhandled AccessorKind in switch.");
}