Skip to content

Commit

Permalink
[clang][NFC] Refactor Sema::RedeclarationKind
Browse files Browse the repository at this point in the history
This patch converts the enum into scoped enum, and moves it into its own header for the time being. It's definition is needed in `Sema.h`, and is going to be needed in upcoming `SemaObjC.h`. `Lookup.h` can't hold it, because it includes `Sema.h`.
  • Loading branch information
Endilll committed Apr 17, 2024
1 parent 812963f commit 458328a
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 88 deletions.
43 changes: 23 additions & 20 deletions clang/include/clang/Sema/Lookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,28 +153,30 @@ class LookupResult {

using iterator = UnresolvedSetImpl::iterator;

LookupResult(Sema &SemaRef, const DeclarationNameInfo &NameInfo,
Sema::LookupNameKind LookupKind,
Sema::RedeclarationKind Redecl = Sema::NotForRedeclaration)
LookupResult(
Sema &SemaRef, const DeclarationNameInfo &NameInfo,
Sema::LookupNameKind LookupKind,
RedeclarationKind Redecl = RedeclarationKind::NotForRedeclaration)
: SemaPtr(&SemaRef), NameInfo(NameInfo), LookupKind(LookupKind),
Redecl(Redecl != Sema::NotForRedeclaration),
ExternalRedecl(Redecl == Sema::ForExternalRedeclaration),
DiagnoseAccess(Redecl == Sema::NotForRedeclaration),
DiagnoseAmbiguous(Redecl == Sema::NotForRedeclaration) {
Redecl(Redecl != RedeclarationKind::NotForRedeclaration),
ExternalRedecl(Redecl == RedeclarationKind::ForExternalRedeclaration),
DiagnoseAccess(Redecl == RedeclarationKind::NotForRedeclaration),
DiagnoseAmbiguous(Redecl == RedeclarationKind::NotForRedeclaration) {
configure();
}

// TODO: consider whether this constructor should be restricted to take
// as input a const IdentifierInfo* (instead of Name),
// forcing other cases towards the constructor taking a DNInfo.
LookupResult(Sema &SemaRef, DeclarationName Name, SourceLocation NameLoc,
Sema::LookupNameKind LookupKind,
Sema::RedeclarationKind Redecl = Sema::NotForRedeclaration)
LookupResult(
Sema &SemaRef, DeclarationName Name, SourceLocation NameLoc,
Sema::LookupNameKind LookupKind,
RedeclarationKind Redecl = RedeclarationKind::NotForRedeclaration)
: SemaPtr(&SemaRef), NameInfo(Name, NameLoc), LookupKind(LookupKind),
Redecl(Redecl != Sema::NotForRedeclaration),
ExternalRedecl(Redecl == Sema::ForExternalRedeclaration),
DiagnoseAccess(Redecl == Sema::NotForRedeclaration),
DiagnoseAmbiguous(Redecl == Sema::NotForRedeclaration) {
Redecl(Redecl != RedeclarationKind::NotForRedeclaration),
ExternalRedecl(Redecl == RedeclarationKind::ForExternalRedeclaration),
DiagnoseAccess(Redecl == RedeclarationKind::NotForRedeclaration),
DiagnoseAmbiguous(Redecl == RedeclarationKind::NotForRedeclaration) {
configure();
}

Expand Down Expand Up @@ -285,9 +287,10 @@ class LookupResult {
return ExternalRedecl;
}

Sema::RedeclarationKind redeclarationKind() const {
return ExternalRedecl ? Sema::ForExternalRedeclaration :
Redecl ? Sema::ForVisibleRedeclaration : Sema::NotForRedeclaration;
RedeclarationKind redeclarationKind() const {
return ExternalRedecl ? RedeclarationKind::ForExternalRedeclaration
: Redecl ? RedeclarationKind::ForVisibleRedeclaration
: RedeclarationKind::NotForRedeclaration;
}

/// Specify whether hidden declarations are visible, e.g.,
Expand Down Expand Up @@ -615,9 +618,9 @@ class LookupResult {
}

/// Change this lookup's redeclaration kind.
void setRedeclarationKind(Sema::RedeclarationKind RK) {
Redecl = (RK != Sema::NotForRedeclaration);
ExternalRedecl = (RK == Sema::ForExternalRedeclaration);
void setRedeclarationKind(RedeclarationKind RK) {
Redecl = (RK != RedeclarationKind::NotForRedeclaration);
ExternalRedecl = (RK == RedeclarationKind::ForExternalRedeclaration);
configure();
}

Expand Down
31 changes: 31 additions & 0 deletions clang/include/clang/Sema/Redeclaration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//===- Redeclaration.h - Redeclarations--------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines RedeclarationKind enum.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_SEMA_REDECLARATION_H
#define LLVM_CLANG_SEMA_REDECLARATION_H

/// Specifies whether (or how) name lookup is being performed for a
/// redeclaration (vs. a reference).
enum class RedeclarationKind {
/// The lookup is a reference to this name that is not for the
/// purpose of redeclaring the name.
NotForRedeclaration = 0,
/// The lookup results will be used for redeclaration of a name,
/// if an entity by that name already exists and is visible.
ForVisibleRedeclaration,
/// The lookup results will be used for redeclaration of a name
/// with external linkage; non-visible lookup results with external linkage
/// may also be found.
ForExternalRedeclaration
};

#endif // LLVM_CLANG_SEMA_REDECLARATION_H
40 changes: 9 additions & 31 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include "clang/Sema/IdentifierResolver.h"
#include "clang/Sema/ObjCMethodList.h"
#include "clang/Sema/Ownership.h"
#include "clang/Sema/Redeclaration.h"
#include "clang/Sema/Scope.h"
#include "clang/Sema/SemaBase.h"
#include "clang/Sema/SemaConcept.h"
Expand Down Expand Up @@ -7443,40 +7444,17 @@ class Sema final : public SemaBase {
typedef std::function<ExprResult(Sema &, TypoExpr *, TypoCorrection)>
TypoRecoveryCallback;

/// Specifies whether (or how) name lookup is being performed for a
/// redeclaration (vs. a reference).
enum RedeclarationKind {
/// The lookup is a reference to this name that is not for the
/// purpose of redeclaring the name.
NotForRedeclaration = 0,
/// The lookup results will be used for redeclaration of a name,
/// if an entity by that name already exists and is visible.
ForVisibleRedeclaration,
/// The lookup results will be used for redeclaration of a name
/// with external linkage; non-visible lookup results with external linkage
/// may also be found.
ForExternalRedeclaration
};

RedeclarationKind forRedeclarationInCurContext() const {
// A declaration with an owning module for linkage can never link against
// anything that is not visible. We don't need to check linkage here; if
// the context has internal linkage, redeclaration lookup won't find things
// from other TUs, and we can't safely compute linkage yet in general.
if (cast<Decl>(CurContext)
->getOwningModuleForLinkage(/*IgnoreLinkage*/ true))
return ForVisibleRedeclaration;
return ForExternalRedeclaration;
}
RedeclarationKind forRedeclarationInCurContext() const;

/// Look up a name, looking for a single declaration. Return
/// null if the results were absent, ambiguous, or overloaded.
///
/// It is preferable to use the elaborated form and explicitly handle
/// ambiguity and overloaded.
NamedDecl *LookupSingleName(Scope *S, DeclarationName Name,
SourceLocation Loc, LookupNameKind NameKind,
RedeclarationKind Redecl = NotForRedeclaration);
NamedDecl *LookupSingleName(
Scope *S, DeclarationName Name, SourceLocation Loc,
LookupNameKind NameKind,
RedeclarationKind Redecl = RedeclarationKind::NotForRedeclaration);
bool LookupBuiltin(LookupResult &R);
void LookupNecessaryTypesForBuiltin(Scope *S, unsigned ID);
bool LookupName(LookupResult &R, Scope *S, bool AllowBuiltinCreation = false,
Expand All @@ -7488,9 +7466,9 @@ class Sema final : public SemaBase {
bool LookupParsedName(LookupResult &R, Scope *S, CXXScopeSpec *SS,
bool AllowBuiltinCreation = false,
bool EnteringContext = false);
ObjCProtocolDecl *
LookupProtocol(IdentifierInfo *II, SourceLocation IdLoc,
RedeclarationKind Redecl = NotForRedeclaration);
ObjCProtocolDecl *LookupProtocol(
IdentifierInfo *II, SourceLocation IdLoc,
RedeclarationKind Redecl = RedeclarationKind::NotForRedeclaration);
bool LookupInSuper(LookupResult &R, CXXRecordDecl *Class);

void LookupOverloadedOperatorName(OverloadedOperatorKind Op, Scope *S,
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Interpreter/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,8 @@ std::unique_ptr<RuntimeInterfaceBuilder> Interpreter::FindRuntimeInterface() {

auto LookupInterface = [&](Expr *&Interface, llvm::StringRef Name) {
LookupResult R(S, &Ctx.Idents.get(Name), SourceLocation(),
Sema::LookupOrdinaryName, Sema::ForVisibleRedeclaration);
Sema::LookupOrdinaryName,
RedeclarationKind::ForVisibleRedeclaration);
S.LookupQualifiedName(R, Ctx.getTranslationUnitDecl());
if (R.empty())
return false;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Interpreter/InterpreterUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ NamedDecl *LookupNamed(Sema &S, llvm::StringRef Name,
const DeclContext *Within) {
DeclarationName DName = &S.Context.Idents.get(Name);
LookupResult R(S, DName, SourceLocation(), Sema::LookupOrdinaryName,
Sema::ForVisibleRedeclaration);
RedeclarationKind::ForVisibleRedeclaration);

R.suppressDiagnostics();

Expand Down
24 changes: 14 additions & 10 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5374,7 +5374,7 @@ static bool CheckAnonMemberRedeclaration(Sema &SemaRef, Scope *S,
LookupResult R(SemaRef, Name, NameLoc,
Owner->isRecord() ? Sema::LookupMemberName
: Sema::LookupOrdinaryName,
Sema::ForVisibleRedeclaration);
RedeclarationKind::ForVisibleRedeclaration);
if (!SemaRef.LookupName(R, S)) return false;

// Pick a representative declaration.
Expand Down Expand Up @@ -6470,7 +6470,8 @@ NamedDecl *Sema::HandleDeclarator(Scope *S, Declarator &D,

if (IsLinkageLookup) {
Previous.clear(LookupRedeclarationWithLinkage);
Previous.setRedeclarationKind(ForExternalRedeclaration);
Previous.setRedeclarationKind(
RedeclarationKind::ForExternalRedeclaration);
}

LookupName(Previous, S, CreateBuiltins);
Expand Down Expand Up @@ -8521,7 +8522,8 @@ void Sema::CheckShadow(Scope *S, VarDecl *D) {
return;

LookupResult R(*this, D->getDeclName(), D->getLocation(),
Sema::LookupOrdinaryName, Sema::ForVisibleRedeclaration);
Sema::LookupOrdinaryName,
RedeclarationKind::ForVisibleRedeclaration);
LookupName(R, S);
if (NamedDecl *ShadowedDecl = getShadowedDeclaration(D, R))
CheckShadow(D, ShadowedDecl, R);
Expand Down Expand Up @@ -9161,7 +9163,7 @@ static NamedDecl *DiagnoseInvalidRedeclaration(
LookupResult Prev(SemaRef, Name, NewFD->getLocation(),
IsLocalFriend ? Sema::LookupLocalFriendName
: Sema::LookupOrdinaryName,
Sema::ForVisibleRedeclaration);
RedeclarationKind::ForVisibleRedeclaration);

NewFD->setInvalidDecl();
if (IsLocalFriend)
Expand Down Expand Up @@ -15196,7 +15198,7 @@ Decl *Sema::ActOnParamDeclarator(Scope *S, Declarator &D,
const IdentifierInfo *II = D.getIdentifier();
if (II) {
LookupResult R(*this, II, D.getIdentifierLoc(), LookupOrdinaryName,
ForVisibleRedeclaration);
RedeclarationKind::ForVisibleRedeclaration);
LookupName(R, S);
if (!R.empty()) {
NamedDecl *PrevDecl = *R.begin();
Expand Down Expand Up @@ -17428,7 +17430,7 @@ Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc,

RedeclarationKind Redecl = forRedeclarationInCurContext();
if (TUK == TUK_Friend || TUK == TUK_Reference)
Redecl = NotForRedeclaration;
Redecl = RedeclarationKind::NotForRedeclaration;

/// Create a new tag decl in C/ObjC. Since the ODR-like semantics for ObjC/C
/// implemented asks for structural equivalence checking, the returned decl
Expand Down Expand Up @@ -18589,7 +18591,7 @@ FieldDecl *Sema::HandleField(Scope *S, RecordDecl *Record,
// Check to see if this name was declared as a member previously
NamedDecl *PrevDecl = nullptr;
LookupResult Previous(*this, II, Loc, LookupMemberName,
ForVisibleRedeclaration);
RedeclarationKind::ForVisibleRedeclaration);
LookupName(Previous, S);
switch (Previous.getResultKind()) {
case LookupResult::Found:
Expand Down Expand Up @@ -18993,8 +18995,9 @@ Decl *Sema::ActOnIvar(Scope *S, SourceLocation DeclStart, Declarator &D,
NewID->setInvalidDecl();

if (II) {
NamedDecl *PrevDecl = LookupSingleName(S, II, Loc, LookupMemberName,
ForVisibleRedeclaration);
NamedDecl *PrevDecl =
LookupSingleName(S, II, Loc, LookupMemberName,
RedeclarationKind::ForVisibleRedeclaration);
if (PrevDecl && isDeclInScope(PrevDecl, EnclosingContext, S)
&& !isa<TagDecl>(PrevDecl)) {
Diag(Loc, diag::err_duplicate_member) << II;
Expand Down Expand Up @@ -20039,7 +20042,8 @@ Decl *Sema::ActOnEnumConstant(Scope *S, Decl *theEnumDecl, Decl *lastEnumConst,

// Verify that there isn't already something declared with this name in this
// scope.
LookupResult R(*this, Id, IdLoc, LookupOrdinaryName, ForVisibleRedeclaration);
LookupResult R(*this, Id, IdLoc, LookupOrdinaryName,
RedeclarationKind::ForVisibleRedeclaration);
LookupName(R, S);
NamedDecl *PrevDecl = R.getAsSingle<NamedDecl>();

Expand Down
28 changes: 14 additions & 14 deletions clang/lib/Sema/SemaDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ Sema::ActOnDecompositionDeclarator(Scope *S, Declarator &D,
assert(VarName && "Cannot have an unnamed binding declaration");

LookupResult Previous(*this, NameInfo, LookupOrdinaryName,
ForVisibleRedeclaration);
RedeclarationKind::ForVisibleRedeclaration);
LookupName(Previous, S,
/*CreateBuiltins*/DC->getRedeclContext()->isTranslationUnit());

Expand Down Expand Up @@ -951,7 +951,7 @@ Sema::ActOnDecompositionDeclarator(Scope *S, Declarator &D,
DeclarationNameInfo NameInfo((IdentifierInfo *)nullptr,
Decomp.getLSquareLoc());
LookupResult Previous(*this, NameInfo, LookupOrdinaryName,
ForVisibleRedeclaration);
RedeclarationKind::ForVisibleRedeclaration);

// Build the variable that holds the non-decomposed object.
bool AddToScope = true;
Expand Down Expand Up @@ -11715,7 +11715,7 @@ Decl *Sema::ActOnStartNamespaceDef(Scope *NamespcScope,
// look through using directives, just look for any ordinary names
// as if by qualified name lookup.
LookupResult R(*this, II, IdentLoc, LookupOrdinaryName,
ForExternalRedeclaration);
RedeclarationKind::ForExternalRedeclaration);
LookupQualifiedName(R, CurContext->getRedeclContext());
NamedDecl *PrevDecl =
R.isSingleResult() ? R.getRepresentativeDecl() : nullptr;
Expand Down Expand Up @@ -12916,7 +12916,7 @@ NamedDecl *Sema::BuildUsingDeclaration(

// Do the redeclaration lookup in the current scope.
LookupResult Previous(*this, UsingName, LookupUsingDeclName,
ForVisibleRedeclaration);
RedeclarationKind::ForVisibleRedeclaration);
Previous.setHideTags(false);
if (S) {
LookupName(Previous, S);
Expand Down Expand Up @@ -13159,7 +13159,7 @@ NamedDecl *Sema::BuildUsingEnumDeclaration(Scope *S, AccessSpecifier AS,
/// In class scope, check if this is a duplicate, for better a diagnostic.
DeclarationNameInfo UsingEnumName(ED->getDeclName(), NameLoc);
LookupResult Previous(*this, UsingEnumName, LookupUsingDeclName,
ForVisibleRedeclaration);
RedeclarationKind::ForVisibleRedeclaration);

LookupName(Previous, S);

Expand Down Expand Up @@ -13192,7 +13192,7 @@ NamedDecl *Sema::BuildUsingEnumDeclaration(Scope *S, AccessSpecifier AS,
UsingShadowDecl *PrevDecl = nullptr;
DeclarationNameInfo DNI(EC->getDeclName(), EC->getLocation());
LookupResult Previous(*this, DNI, LookupOrdinaryName,
ForVisibleRedeclaration);
RedeclarationKind::ForVisibleRedeclaration);
LookupName(Previous, S);
FilterUsingLookup(S, Previous);

Expand Down Expand Up @@ -13587,7 +13587,7 @@ Decl *Sema::ActOnAliasDeclaration(Scope *S, AccessSpecifier AS,
LookupResult Previous(*this, NameInfo, LookupOrdinaryName,
TemplateParamLists.size()
? forRedeclarationInCurContext()
: ForVisibleRedeclaration);
: RedeclarationKind::ForVisibleRedeclaration);
LookupName(Previous, S);

// Warn about shadowing the name of a template parameter.
Expand Down Expand Up @@ -13737,7 +13737,7 @@ Decl *Sema::ActOnNamespaceAliasDef(Scope *S, SourceLocation NamespaceLoc,

// Check if we have a previous declaration with the same name.
LookupResult PrevR(*this, Alias, AliasLoc, LookupOrdinaryName,
ForVisibleRedeclaration);
RedeclarationKind::ForVisibleRedeclaration);
LookupName(PrevR, S);

// Check we're not shadowing a template parameter.
Expand Down Expand Up @@ -13983,7 +13983,7 @@ void Sema::CheckImplicitSpecialMemberDeclaration(Scope *S, FunctionDecl *FD) {
// implicit special members with this name.
DeclarationName Name = FD->getDeclName();
LookupResult R(*this, Name, SourceLocation(), LookupOrdinaryName,
ForExternalRedeclaration);
RedeclarationKind::ForExternalRedeclaration);
for (auto *D : FD->getParent()->lookup(Name))
if (auto *Acceptable = R.getAcceptableDecl(D))
R.addDecl(Acceptable);
Expand Down Expand Up @@ -17113,9 +17113,9 @@ Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) {
}

const IdentifierInfo *II = D.getIdentifier();
if (NamedDecl *PrevDecl = LookupSingleName(S, II, D.getIdentifierLoc(),
LookupOrdinaryName,
ForVisibleRedeclaration)) {
if (NamedDecl *PrevDecl =
LookupSingleName(S, II, D.getIdentifierLoc(), LookupOrdinaryName,
RedeclarationKind::ForVisibleRedeclaration)) {
// The scope should be freshly made just for us. There is just no way
// it contains any previous declaration, except for function parameters in
// a function-try-block's catch statement.
Expand Down Expand Up @@ -17906,7 +17906,7 @@ NamedDecl *Sema::ActOnFriendFunctionDecl(Scope *S, Declarator &D,
DeclContext *DC;
Scope *DCScope = S;
LookupResult Previous(*this, NameInfo, LookupOrdinaryName,
ForExternalRedeclaration);
RedeclarationKind::ForExternalRedeclaration);

bool isTemplateId = D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId;

Expand Down Expand Up @@ -19242,7 +19242,7 @@ MSPropertyDecl *Sema::HandleMSProperty(Scope *S, RecordDecl *Record,
// Check to see if this name was declared as a member previously
NamedDecl *PrevDecl = nullptr;
LookupResult Previous(*this, II, Loc, LookupMemberName,
ForVisibleRedeclaration);
RedeclarationKind::ForVisibleRedeclaration);
LookupName(Previous, S);
switch (Previous.getResultKind()) {
case LookupResult::Found:
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9153,7 +9153,7 @@ Sema::CheckMicrosoftIfExistsSymbol(Scope *S,

// Do the redeclaration lookup in the current scope.
LookupResult R(*this, TargetNameInfo, Sema::LookupAnyName,
Sema::NotForRedeclaration);
RedeclarationKind::NotForRedeclaration);
LookupParsedName(R, S, &SS);
R.suppressDiagnostics();

Expand Down

0 comments on commit 458328a

Please sign in to comment.