[cfe-commits] r139818 - in /cfe/trunk: lib/Sema/SemaExpr.cpp test/SemaObjC/class-type-conversion.m

jahanian fjahanian at apple.com
Thu Sep 15 12:12:17 PDT 2011


On Sep 15, 2011, at 11:58 AM, Douglas Gregor wrote:

> 
> On Sep 15, 2011, at 11:30 AM, Fariborz Jahanian wrote:
> 
>> Author: fjahanian
>> Date: Thu Sep 15 13:30:22 2011
>> New Revision: 139818
>> 
>> URL: http://llvm.org/viewvc/llvm-project?rev=139818&view=rev
>> Log:
>> Objective-c: Conversion from type Class to any root class type is allowed 
>> in class methods with no warning. //rdar://10109725
>> 
>> Added:
>>   cfe/trunk/test/SemaObjC/class-type-conversion.m
>> Modified:
>>   cfe/trunk/lib/Sema/SemaExpr.cpp
>> 
>> Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
>> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=139818&r1=139817&r2=139818&view=diff
>> ==============================================================================
>> --- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
>> +++ cfe/trunk/lib/Sema/SemaExpr.cpp Thu Sep 15 13:30:22 2011
>> @@ -5227,6 +5227,35 @@
>>  return ConvTy;
>> }
>> 
>> +static Sema::AssignConvertType
>> +checkObjCPointerTypesForAssignment(Sema &S, QualType LHSType,
>> +                                   QualType RHSType);
>> +/// checkClassTypes - Routine checks for conversion of "Class" type.
>> +// Conversion from type Class to any root class type in a class method
>> +// is allowed.
>> +static Sema::AssignConvertType
>> +checkClassTypes(Sema &S, QualType LHSType) {
>> +  // Conversion from type Class to any root class type is allowed.
>> +  DeclContext *DC = S.CurContext;
>> +  while (isa<BlockDecl>(DC))
>> +    DC = DC->getParent();
>> +  ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(DC);
>> +  if (MD && MD->isClassMethod()) {
>> +    ObjCInterfaceDecl *Root = 0;
>> +      if (ObjCInterfaceDecl * IDecl = MD->getClassInterface())
>> +        do
>> +          Root = IDecl;
>> +        while ((IDecl = IDecl->getSuperClass()));
>> +      if (Root){
>> +        QualType RHSType = 
>> +          S.Context.getObjCObjectPointerType(
>> +                                 S.Context.getObjCInterfaceType(Root));
>> +        return checkObjCPointerTypesForAssignment(S, LHSType, RHSType);
>> +      }
>> +  }
>> +  return Sema::IncompatiblePointer;
>> +}
> 
> This is perhaps the strangest conversion I've seen:
> 
>  - It's not like the conversions from 'id' to any type, because it's only a conversion to a class that doesn't have a superclass

No. It is a conversion to a class which has a super class or when it has no super class.

>  - It's context-dependent (only works in class methods), which is pretty rare with conversions

This is my understanding. 'self' in an instance method has a clear cut type. Radar has more detail.
> 
> I get the impression that the intent here is to make 'self' behave better within an class method, but I think this is the wrong way to go about it. We just don't have a way to express the type of 'self' in a class method in the type system, so if we're going to make 'self' behave better, we need to look at this as a special conversion from the 'self' expression to a particular type, not as a semi-general conversion on the Class type.

This is a special conversion from 'Class' to a particular type, in this case is the "root" class for current class.
Please read Greg's explanation in radar and see if you read it differently that I did.

- fariborz

> 
> 	- Doug
> 




More information about the cfe-commits mailing list