Skip to content

Commit aaba376

Browse files
committed
[clang][NFC] Refactor ObjCMethodDecl::ImplementationControl
This patch moves `ObjCMethodDecl::ImplementationControl` to a DeclBase.h so that it's complete at the point where corresponsing bit-field is declared. This patch also converts it to a scoped enum `clang::ObjCImplementationControl`.
1 parent f471f6f commit aaba376

File tree

17 files changed

+109
-107
lines changed

17 files changed

+109
-107
lines changed

clang/include/clang/AST/DeclBase.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,6 +1428,8 @@ enum class OMPDeclareReductionInitKind {
14281428
Copy // omp_priv = <expr>
14291429
};
14301430

1431+
enum class ObjCImplementationControl { None, Required, Optional };
1432+
14311433
/// DeclContext - This is used only as base class of specific decl types that
14321434
/// can act as declaration contexts. These decls are (only the top classes
14331435
/// that directly derive from DeclContext are mentioned, not their subclasses):

clang/include/clang/AST/DeclObjC.h

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,6 @@ class ObjCMethodDecl : public NamedDecl, public DeclContext {
139139
// This class stores some data in DeclContext::ObjCMethodDeclBits
140140
// to save some space. Use the provided accessors to access it.
141141

142-
public:
143-
enum ImplementationControl { None, Required, Optional };
144-
145-
private:
146142
/// Return type of this method.
147143
QualType MethodDeclType;
148144

@@ -168,14 +164,14 @@ class ObjCMethodDecl : public NamedDecl, public DeclContext {
168164
/// constructed by createImplicitParams.
169165
ImplicitParamDecl *CmdDecl = nullptr;
170166

171-
ObjCMethodDecl(SourceLocation beginLoc, SourceLocation endLoc,
172-
Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
173-
DeclContext *contextDecl, bool isInstance = true,
174-
bool isVariadic = false, bool isPropertyAccessor = false,
175-
bool isSynthesizedAccessorStub = false,
176-
bool isImplicitlyDeclared = false, bool isDefined = false,
177-
ImplementationControl impControl = None,
178-
bool HasRelatedResultType = false);
167+
ObjCMethodDecl(
168+
SourceLocation beginLoc, SourceLocation endLoc, Selector SelInfo,
169+
QualType T, TypeSourceInfo *ReturnTInfo, DeclContext *contextDecl,
170+
bool isInstance = true, bool isVariadic = false,
171+
bool isPropertyAccessor = false, bool isSynthesizedAccessorStub = false,
172+
bool isImplicitlyDeclared = false, bool isDefined = false,
173+
ObjCImplementationControl impControl = ObjCImplementationControl::None,
174+
bool HasRelatedResultType = false);
179175

180176
SelectorLocationsKind getSelLocsKind() const {
181177
return static_cast<SelectorLocationsKind>(ObjCMethodDeclBits.SelLocsKind);
@@ -235,7 +231,7 @@ class ObjCMethodDecl : public NamedDecl, public DeclContext {
235231
bool isVariadic = false, bool isPropertyAccessor = false,
236232
bool isSynthesizedAccessorStub = false,
237233
bool isImplicitlyDeclared = false, bool isDefined = false,
238-
ImplementationControl impControl = None,
234+
ObjCImplementationControl impControl = ObjCImplementationControl::None,
239235
bool HasRelatedResultType = false);
240236

241237
static ObjCMethodDecl *CreateDeserialized(ASTContext &C, unsigned ID);
@@ -495,16 +491,17 @@ class ObjCMethodDecl : public NamedDecl, public DeclContext {
495491
const ObjCPropertyDecl *findPropertyDecl(bool CheckOverrides = true) const;
496492

497493
// Related to protocols declared in \@protocol
498-
void setDeclImplementation(ImplementationControl ic) {
499-
ObjCMethodDeclBits.DeclImplementation = ic;
494+
void setDeclImplementation(ObjCImplementationControl ic) {
495+
ObjCMethodDeclBits.DeclImplementation = llvm::to_underlying(ic);
500496
}
501497

502-
ImplementationControl getImplementationControl() const {
503-
return ImplementationControl(ObjCMethodDeclBits.DeclImplementation);
498+
ObjCImplementationControl getImplementationControl() const {
499+
return static_cast<ObjCImplementationControl>(
500+
ObjCMethodDeclBits.DeclImplementation);
504501
}
505502

506503
bool isOptional() const {
507-
return getImplementationControl() == Optional;
504+
return getImplementationControl() == ObjCImplementationControl::Optional;
508505
}
509506

510507
/// Returns true if this specific method declaration is marked with the

clang/lib/ARCMigrate/ObjCMT.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ ClassImplementsAllMethodsAndProperties(ASTContext &Ctx,
636636
for (const auto *MD : PDecl->methods()) {
637637
if (MD->isImplicit())
638638
continue;
639-
if (MD->getImplementationControl() == ObjCMethodDecl::Optional)
639+
if (MD->getImplementationControl() == ObjCImplementationControl::Optional)
640640
continue;
641641
DeclContext::lookup_result R = ImpDecl->lookup(MD->getDeclName());
642642
if (R.empty())

clang/lib/AST/DeclObjC.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ ObjCMethodDecl::ObjCMethodDecl(
825825
QualType T, TypeSourceInfo *ReturnTInfo, DeclContext *contextDecl,
826826
bool isInstance, bool isVariadic, bool isPropertyAccessor,
827827
bool isSynthesizedAccessorStub, bool isImplicitlyDeclared, bool isDefined,
828-
ImplementationControl impControl, bool HasRelatedResultType)
828+
ObjCImplementationControl impControl, bool HasRelatedResultType)
829829
: NamedDecl(ObjCMethod, contextDecl, beginLoc, SelInfo),
830830
DeclContext(ObjCMethod), MethodDeclType(T), ReturnTInfo(ReturnTInfo),
831831
DeclEndLoc(endLoc) {
@@ -855,8 +855,8 @@ ObjCMethodDecl *ObjCMethodDecl::Create(
855855
Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
856856
DeclContext *contextDecl, bool isInstance, bool isVariadic,
857857
bool isPropertyAccessor, bool isSynthesizedAccessorStub,
858-
bool isImplicitlyDeclared, bool isDefined, ImplementationControl impControl,
859-
bool HasRelatedResultType) {
858+
bool isImplicitlyDeclared, bool isDefined,
859+
ObjCImplementationControl impControl, bool HasRelatedResultType) {
860860
return new (C, contextDecl) ObjCMethodDecl(
861861
beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
862862
isVariadic, isPropertyAccessor, isSynthesizedAccessorStub,

clang/lib/AST/ODRDiagsEmitter.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,10 @@ bool ODRDiagsEmitter::diagnoseSubMismatchObjCMethod(
461461
}
462462
if (FirstMethod->getImplementationControl() !=
463463
SecondMethod->getImplementationControl()) {
464-
DiagError(ControlLevel) << FirstMethod->getImplementationControl();
465-
DiagNote(ControlLevel) << SecondMethod->getImplementationControl();
464+
DiagError(ControlLevel)
465+
<< llvm::to_underlying(FirstMethod->getImplementationControl());
466+
DiagNote(ControlLevel) << llvm::to_underlying(
467+
SecondMethod->getImplementationControl());
466468
return true;
467469
}
468470
if (FirstMethod->isThisDeclarationADesignatedInitializer() !=

clang/lib/AST/ODRHash.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
380380
Hash.AddBoolean(Method->isThisDeclarationADesignatedInitializer());
381381
Hash.AddBoolean(Method->hasSkippedBody());
382382

383-
ID.AddInteger(Method->getImplementationControl());
383+
ID.AddInteger(llvm::to_underlying(Method->getImplementationControl()));
384384
ID.AddInteger(Method->getMethodFamily());
385385
ImplicitParamDecl *Cmd = Method->getCmdDecl();
386386
Hash.AddBoolean(Cmd);

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6485,7 +6485,7 @@ void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) {
64856485
/*isInstance=*/true, /*isVariadic=*/false,
64866486
/*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false,
64876487
/*isImplicitlyDeclared=*/true,
6488-
/*isDefined=*/false, ObjCMethodDecl::Required);
6488+
/*isDefined=*/false, ObjCImplementationControl::Required);
64896489
D->addInstanceMethod(DTORMethod);
64906490
CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false);
64916491
D->setHasDestructors(true);
@@ -6506,7 +6506,7 @@ void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) {
65066506
/*isVariadic=*/false,
65076507
/*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false,
65086508
/*isImplicitlyDeclared=*/true,
6509-
/*isDefined=*/false, ObjCMethodDecl::Required);
6509+
/*isDefined=*/false, ObjCImplementationControl::Required);
65106510
D->addInstanceMethod(CTORMethod);
65116511
CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true);
65126512
D->setHasNonZeroConstructors(true);

clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6855,15 +6855,15 @@ void RewriteModernObjC::RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl,
68556855
std::vector<ObjCMethodDecl *> InstanceMethods, ClassMethods;
68566856
std::vector<ObjCMethodDecl *> OptInstanceMethods, OptClassMethods;
68576857
for (auto *MD : PDecl->instance_methods()) {
6858-
if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
6858+
if (MD->getImplementationControl() == ObjCImplementationControl::Optional) {
68596859
OptInstanceMethods.push_back(MD);
68606860
} else {
68616861
InstanceMethods.push_back(MD);
68626862
}
68636863
}
68646864

68656865
for (auto *MD : PDecl->class_methods()) {
6866-
if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
6866+
if (MD->getImplementationControl() == ObjCImplementationControl::Optional) {
68676867
OptClassMethods.push_back(MD);
68686868
} else {
68696869
ClassMethods.push_back(MD);

clang/lib/Sema/SemaDeclObjC.cpp

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2632,7 +2632,8 @@ void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
26322632
// don't issue warning when protocol method is optional because primary
26332633
// class is not required to implement it and it is safe for protocol
26342634
// to implement it.
2635-
if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
2635+
if (MethodDecl->getImplementationControl() ==
2636+
ObjCImplementationControl::Optional)
26362637
return;
26372638
// don't issue warning when primary class's method is
26382639
// deprecated/unavailable.
@@ -2765,45 +2766,43 @@ static void CheckProtocolMethodDefs(
27652766
// check unimplemented instance methods.
27662767
if (!NSIDecl)
27672768
for (auto *method : PDecl->instance_methods()) {
2768-
if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
2769+
if (method->getImplementationControl() !=
2770+
ObjCImplementationControl::Optional &&
27692771
!method->isPropertyAccessor() &&
27702772
!InsMap.count(method->getSelector()) &&
2771-
(!Super || !Super->lookupMethod(method->getSelector(),
2772-
true /* instance */,
2773-
false /* shallowCategory */,
2774-
true /* followsSuper */,
2775-
nullptr /* category */))) {
2776-
// If a method is not implemented in the category implementation but
2777-
// has been declared in its primary class, superclass,
2778-
// or in one of their protocols, no need to issue the warning.
2779-
// This is because method will be implemented in the primary class
2780-
// or one of its super class implementation.
2781-
2782-
// Ugly, but necessary. Method declared in protocol might have
2783-
// have been synthesized due to a property declared in the class which
2784-
// uses the protocol.
2785-
if (ObjCMethodDecl *MethodInClass =
2786-
IDecl->lookupMethod(method->getSelector(),
2787-
true /* instance */,
2788-
true /* shallowCategoryLookup */,
2789-
false /* followSuper */))
2790-
if (C || MethodInClass->isPropertyAccessor())
2791-
continue;
2792-
unsigned DIAG = diag::warn_unimplemented_protocol_method;
2793-
if (!S.Diags.isIgnored(DIAG, Impl->getLocation())) {
2794-
WarnUndefinedMethod(S, Impl, method, IncompleteImpl, DIAG, PDecl);
2795-
}
2796-
}
2773+
(!Super || !Super->lookupMethod(
2774+
method->getSelector(), true /* instance */,
2775+
false /* shallowCategory */, true /* followsSuper */,
2776+
nullptr /* category */))) {
2777+
// If a method is not implemented in the category implementation but
2778+
// has been declared in its primary class, superclass,
2779+
// or in one of their protocols, no need to issue the warning.
2780+
// This is because method will be implemented in the primary class
2781+
// or one of its super class implementation.
2782+
2783+
// Ugly, but necessary. Method declared in protocol might have
2784+
// have been synthesized due to a property declared in the class which
2785+
// uses the protocol.
2786+
if (ObjCMethodDecl *MethodInClass = IDecl->lookupMethod(
2787+
method->getSelector(), true /* instance */,
2788+
true /* shallowCategoryLookup */, false /* followSuper */))
2789+
if (C || MethodInClass->isPropertyAccessor())
2790+
continue;
2791+
unsigned DIAG = diag::warn_unimplemented_protocol_method;
2792+
if (!S.Diags.isIgnored(DIAG, Impl->getLocation())) {
2793+
WarnUndefinedMethod(S, Impl, method, IncompleteImpl, DIAG, PDecl);
2794+
}
2795+
}
27972796
}
27982797
// check unimplemented class methods
27992798
for (auto *method : PDecl->class_methods()) {
2800-
if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
2799+
if (method->getImplementationControl() !=
2800+
ObjCImplementationControl::Optional &&
28012801
!ClsMap.count(method->getSelector()) &&
2802-
(!Super || !Super->lookupMethod(method->getSelector(),
2803-
false /* class method */,
2804-
false /* shallowCategoryLookup */,
2805-
true /* followSuper */,
2806-
nullptr /* category */))) {
2802+
(!Super || !Super->lookupMethod(
2803+
method->getSelector(), false /* class method */,
2804+
false /* shallowCategoryLookup */,
2805+
true /* followSuper */, nullptr /* category */))) {
28072806
// See above comment for instance method lookups.
28082807
if (C && IDecl->lookupMethod(method->getSelector(),
28092808
false /* class */,
@@ -4759,8 +4758,9 @@ Decl *Sema::ActOnMethodDeclaration(
47594758
MethodType == tok::minus, isVariadic,
47604759
/*isPropertyAccessor=*/false, /*isSynthesizedAccessorStub=*/false,
47614760
/*isImplicitlyDeclared=*/false, /*isDefined=*/false,
4762-
MethodDeclKind == tok::objc_optional ? ObjCMethodDecl::Optional
4763-
: ObjCMethodDecl::Required,
4761+
MethodDeclKind == tok::objc_optional
4762+
? ObjCImplementationControl::Optional
4763+
: ObjCImplementationControl::Required,
47644764
HasRelatedResultType);
47654765

47664766
SmallVector<ParmVarDecl*, 16> Params;

clang/lib/Sema/SemaExprObjC.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,15 @@ static ObjCMethodDecl *getNSNumberFactoryMethod(Sema &S, SourceLocation Loc,
285285
if (!Method && S.getLangOpts().DebuggerObjCLiteral) {
286286
// create a stub definition this NSNumber factory method.
287287
TypeSourceInfo *ReturnTInfo = nullptr;
288-
Method =
289-
ObjCMethodDecl::Create(CX, SourceLocation(), SourceLocation(), Sel,
290-
S.NSNumberPointer, ReturnTInfo, S.NSNumberDecl,
291-
/*isInstance=*/false, /*isVariadic=*/false,
292-
/*isPropertyAccessor=*/false,
293-
/*isSynthesizedAccessorStub=*/false,
294-
/*isImplicitlyDeclared=*/true,
295-
/*isDefined=*/false, ObjCMethodDecl::Required,
296-
/*HasRelatedResultType=*/false);
288+
Method = ObjCMethodDecl::Create(
289+
CX, SourceLocation(), SourceLocation(), Sel, S.NSNumberPointer,
290+
ReturnTInfo, S.NSNumberDecl,
291+
/*isInstance=*/false, /*isVariadic=*/false,
292+
/*isPropertyAccessor=*/false,
293+
/*isSynthesizedAccessorStub=*/false,
294+
/*isImplicitlyDeclared=*/true,
295+
/*isDefined=*/false, ObjCImplementationControl::Required,
296+
/*HasRelatedResultType=*/false);
297297
ParmVarDecl *value = ParmVarDecl::Create(S.Context, Method,
298298
SourceLocation(), SourceLocation(),
299299
&CX.Idents.get("value"),
@@ -568,7 +568,7 @@ ExprResult Sema::BuildObjCBoxedExpr(SourceRange SR, Expr *ValueExpr) {
568568
/*isPropertyAccessor=*/false,
569569
/*isSynthesizedAccessorStub=*/false,
570570
/*isImplicitlyDeclared=*/true,
571-
/*isDefined=*/false, ObjCMethodDecl::Required,
571+
/*isDefined=*/false, ObjCImplementationControl::Required,
572572
/*HasRelatedResultType=*/false);
573573
QualType ConstCharType = Context.CharTy.withConst();
574574
ParmVarDecl *value =
@@ -682,7 +682,7 @@ ExprResult Sema::BuildObjCBoxedExpr(SourceRange SR, Expr *ValueExpr) {
682682
/*isPropertyAccessor=*/false,
683683
/*isSynthesizedAccessorStub=*/false,
684684
/*isImplicitlyDeclared=*/true,
685-
/*isDefined=*/false, ObjCMethodDecl::Required,
685+
/*isDefined=*/false, ObjCImplementationControl::Required,
686686
/*HasRelatedResultType=*/false);
687687

688688
SmallVector<ParmVarDecl *, 2> Params;
@@ -816,7 +816,7 @@ ExprResult Sema::BuildObjCArrayLiteral(SourceRange SR, MultiExprArg Elements) {
816816
false /*isVariadic*/,
817817
/*isPropertyAccessor=*/false, /*isSynthesizedAccessorStub=*/false,
818818
/*isImplicitlyDeclared=*/true, /*isDefined=*/false,
819-
ObjCMethodDecl::Required, false);
819+
ObjCImplementationControl::Required, false);
820820
SmallVector<ParmVarDecl *, 2> Params;
821821
ParmVarDecl *objects = ParmVarDecl::Create(Context, Method,
822822
SourceLocation(),
@@ -978,7 +978,7 @@ ExprResult Sema::BuildObjCDictionaryLiteral(SourceRange SR,
978978
/*isPropertyAccessor=*/false,
979979
/*isSynthesizedAccessorStub=*/false,
980980
/*isImplicitlyDeclared=*/true, /*isDefined=*/false,
981-
ObjCMethodDecl::Required, false);
981+
ObjCImplementationControl::Required, false);
982982
SmallVector<ParmVarDecl *, 3> Params;
983983
ParmVarDecl *objects = ParmVarDecl::Create(Context, Method,
984984
SourceLocation(),
@@ -1347,7 +1347,8 @@ ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
13471347
}
13481348

13491349
if (Method &&
1350-
Method->getImplementationControl() != ObjCMethodDecl::Optional &&
1350+
Method->getImplementationControl() !=
1351+
ObjCImplementationControl::Optional &&
13511352
!getSourceManager().isInSystemHeader(Method->getLocation()))
13521353
ReferencedSelectors.insert(std::make_pair(Sel, AtLoc));
13531354

clang/lib/Sema/SemaObjCProperty.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2488,8 +2488,8 @@ void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property) {
24882488
/*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false,
24892489
/*isImplicitlyDeclared=*/true, /*isDefined=*/false,
24902490
(property->getPropertyImplementation() == ObjCPropertyDecl::Optional)
2491-
? ObjCMethodDecl::Optional
2492-
: ObjCMethodDecl::Required);
2491+
? ObjCImplementationControl::Optional
2492+
: ObjCImplementationControl::Required);
24932493
CD->addDecl(GetterMethod);
24942494

24952495
AddPropertyAttrs(*this, GetterMethod, property);
@@ -2530,19 +2530,17 @@ void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property) {
25302530
// for this class.
25312531
SourceLocation Loc = property->getLocation();
25322532

2533-
SetterMethod =
2534-
ObjCMethodDecl::Create(Context, Loc, Loc,
2535-
property->getSetterName(), Context.VoidTy,
2536-
nullptr, CD, !IsClassProperty,
2537-
/*isVariadic=*/false,
2538-
/*isPropertyAccessor=*/true,
2539-
/*isSynthesizedAccessorStub=*/false,
2540-
/*isImplicitlyDeclared=*/true,
2541-
/*isDefined=*/false,
2542-
(property->getPropertyImplementation() ==
2543-
ObjCPropertyDecl::Optional) ?
2544-
ObjCMethodDecl::Optional :
2545-
ObjCMethodDecl::Required);
2533+
SetterMethod = ObjCMethodDecl::Create(
2534+
Context, Loc, Loc, property->getSetterName(), Context.VoidTy, nullptr,
2535+
CD, !IsClassProperty,
2536+
/*isVariadic=*/false,
2537+
/*isPropertyAccessor=*/true,
2538+
/*isSynthesizedAccessorStub=*/false,
2539+
/*isImplicitlyDeclared=*/true,
2540+
/*isDefined=*/false,
2541+
(property->getPropertyImplementation() == ObjCPropertyDecl::Optional)
2542+
? ObjCImplementationControl::Optional
2543+
: ObjCImplementationControl::Required);
25462544

25472545
// Remove all qualifiers from the setter's parameter type.
25482546
QualType paramTy =

0 commit comments

Comments
 (0)