mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
This changes `getBaseName()` on `DeclName` to return a `DeclBaseName` instead of an `Identifier`. All places that will continue to be expecting an `Identifier` are changed to call `getBaseIdentifier` which will later assert that the `DeclName` is actually backed by an identifier and not a special name. For transitional purposes, a conversion operator from `DeclBaseName` to `Identifier` has been added that will be removed again once migration to DeclBaseName has been completed in other parts of the compiler. Unify approach to printing declaration names Printing a declaration's name using `<<` and `getBaseName()` is be independent of the return type of `getBaseName()` which will change in the future from `Identifier` to `DeclBaseName`
112 lines
3.1 KiB
C++
112 lines
3.1 KiB
C++
//===--- IAMInference.h - Import as member inference system -----*- C++ -*-===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file implements support for inferring when globals can be imported as
|
|
// members
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SWIFT_IMPORTER_IMPORT_AS_MEMBER_INFERENCE_H
|
|
#define SWIFT_IMPORTER_IMPORT_AS_MEMBER_INFERENCE_H
|
|
|
|
#include "SwiftLookupTable.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "swift/AST/Identifier.h"
|
|
|
|
namespace clang {
|
|
class Sema;
|
|
class NamedDecl;
|
|
class TypeDecl;
|
|
class FunctionDecl;
|
|
}
|
|
|
|
namespace swift {
|
|
|
|
// Import As Member -- attempt to import C global functions and variables as
|
|
// members on types or instances.
|
|
|
|
struct IAMOptions {
|
|
// TODO: fine-grained control over how we infer
|
|
static IAMOptions getDefault();
|
|
};
|
|
|
|
enum class IAMAccessorKind : uint8_t { None, Getter, Setter };
|
|
|
|
/// The result of out inference system
|
|
struct IAMResult {
|
|
// The name to import as
|
|
DeclName name = {};
|
|
|
|
// If this is a getter/setter, the other accessor
|
|
const clang::FunctionDecl *pairedAccessor = nullptr;
|
|
|
|
IAMAccessorKind accessorKind = IAMAccessorKind::None;
|
|
|
|
Optional<unsigned> selfIndex = None;
|
|
|
|
// The canonical type decl we will import as a member of
|
|
EffectiveClangContext effectiveDC = {};
|
|
|
|
static IAMResult infer(ASTContext &, clang::Sema &, const clang::NamedDecl *,
|
|
IAMOptions = IAMOptions::getDefault());
|
|
|
|
IAMResult() = default;
|
|
|
|
// Static members
|
|
IAMResult(DeclName declName, EffectiveClangContext dc)
|
|
: name(declName), effectiveDC(dc) {}
|
|
|
|
IAMResult(DeclName declName, IAMAccessorKind kind, EffectiveClangContext dc)
|
|
: name(declName), accessorKind(kind), effectiveDC(dc) {}
|
|
|
|
// Instance members
|
|
IAMResult(DeclName declName, unsigned selfIdx, EffectiveClangContext dc)
|
|
: name(declName), selfIndex(selfIdx), effectiveDC(dc) {}
|
|
|
|
IAMResult(DeclName declName, IAMAccessorKind kind, unsigned selfIdx,
|
|
EffectiveClangContext dc)
|
|
: name(declName), accessorKind(kind), selfIndex(selfIdx),
|
|
effectiveDC(dc) {}
|
|
|
|
bool isImportAsMember() const { return bool(effectiveDC); }
|
|
|
|
bool isInstanceMember() const {
|
|
return isImportAsMember() && selfIndex.hasValue();
|
|
}
|
|
|
|
bool isStaticMember() const {
|
|
return isImportAsMember() && !selfIndex.hasValue();
|
|
}
|
|
|
|
bool isPropertyAccessor() const {
|
|
return accessorKind != IAMAccessorKind::None;
|
|
}
|
|
|
|
bool isGetter() const {
|
|
return accessorKind == IAMAccessorKind::Getter;
|
|
}
|
|
|
|
bool isSetter() const {
|
|
return accessorKind == IAMAccessorKind::Setter;
|
|
}
|
|
|
|
bool isInit() const {
|
|
return isStaticMember() && name.getBaseName() == "init";
|
|
}
|
|
};
|
|
}
|
|
|
|
#endif // SWIFT_IMPORTER_IMPORT_AS_MEMBER_INFERENCE_H
|