diff options
author | Steve Naroff <snaroff@apple.com> | 2009-03-09 21:12:44 +0000 |
---|---|---|
committer | Steve Naroff <snaroff@apple.com> | 2009-03-09 21:12:44 +0000 |
commit | 61f72cbd037e58f12cfe90cd442373f44092f030 (patch) | |
tree | 77292f6d4c23460429428e75f3309d662856586f /lib/Sema | |
parent | 35bd763b9438b53f7920521ed19c1ef74c7a6795 (diff) |
Implement property '.' notation on Factory/Class objects. Parser changes aren't very pretty:-(
This fixes <rdar://problem/6496506> Implement class setter/getter for properties.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@66465 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema')
-rw-r--r-- | lib/Sema/Sema.h | 6 | ||||
-rw-r--r-- | lib/Sema/SemaExpr.cpp | 19 | ||||
-rw-r--r-- | lib/Sema/SemaExprObjC.cpp | 74 |
3 files changed, 83 insertions, 16 deletions
diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h index 10a3a9defd..4ac8ffc0fb 100644 --- a/lib/Sema/Sema.h +++ b/lib/Sema/Sema.h @@ -1791,6 +1791,12 @@ public: ObjCMethodDecl *LookupPrivateInstanceMethod(Selector Sel, ObjCInterfaceDecl *ClassDecl); + virtual OwningExprResult ActOnClassPropertyRefExpr( + IdentifierInfo &receiverName, + IdentifierInfo &propertyName, + SourceLocation &receiverNameLoc, + SourceLocation &propertyNameLoc); + // ActOnClassMessage - used for both unary and keyword messages. // ArgExprs is optional - if it is present, the number of expressions // is obtained from NumArgs. diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index d96b5e56d4..f37c0d5bad 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -1657,20 +1657,6 @@ CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc, } -/// constructSetterName - Return the setter name for the given -/// identifier, i.e. "set" + Name where the initial character of Name -/// has been capitalized. -// FIXME: Merge with same routine in Parser. But where should this -// live? -static IdentifierInfo *constructSetterName(IdentifierTable &Idents, - const IdentifierInfo *Name) { - llvm::SmallString<100> SelectorName; - SelectorName = "set"; - SelectorName.append(Name->getName(), Name->getName()+Name->getLength()); - SelectorName[3] = toupper(SelectorName[3]); - return &Idents.get(&SelectorName[0], &SelectorName[SelectorName.size()]); -} - Action::OwningExprResult Sema::ActOnMemberReferenceExpr(Scope *S, ExprArg Base, SourceLocation OpLoc, tok::TokenKind OpKind, SourceLocation MemberLoc, @@ -1900,8 +1886,9 @@ Sema::ActOnMemberReferenceExpr(Scope *S, ExprArg Base, SourceLocation OpLoc, // If we found a getter then this may be a valid dot-reference, we // will look for the matching setter, in case it is needed. - IdentifierInfo *SetterName = constructSetterName(PP.getIdentifierTable(), - &Member); + IdentifierInfo *SetterName = + SelectorTable::constructSetterName(PP.getIdentifierTable(), &Member); + Selector SetterSel = PP.getSelectorTable().getUnarySelector(SetterName); ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); if (!Setter) { diff --git a/lib/Sema/SemaExprObjC.cpp b/lib/Sema/SemaExprObjC.cpp index 6b40c89a8c..512a72f9b4 100644 --- a/lib/Sema/SemaExprObjC.cpp +++ b/lib/Sema/SemaExprObjC.cpp @@ -16,6 +16,8 @@ #include "clang/AST/DeclObjC.h" #include "clang/AST/ExprObjC.h" #include "llvm/ADT/SmallString.h" +#include "clang/Lex/Preprocessor.h" + using namespace clang; Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs, @@ -267,6 +269,78 @@ ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel, return Method; } +Action::OwningExprResult Sema::ActOnClassPropertyRefExpr( + IdentifierInfo &receiverName, + IdentifierInfo &propertyName, + SourceLocation &receiverNameLoc, + SourceLocation &propertyNameLoc) { + + ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(&receiverName); + + // Search for a declared property first. + + Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName); + ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel); + + // If this reference is in an @implementation, check for 'private' methods. + if (!Getter) + if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) + if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) + if (ObjCImplementationDecl *ImpDecl = + ObjCImplementations[ClassDecl->getIdentifier()]) + Getter = ImpDecl->getClassMethod(Sel); + + if (Getter) { + // FIXME: refactor/share with ActOnMemberReference(). + // Check if we can reference this property. + if (DiagnoseUseOfDecl(Getter, propertyNameLoc)) + return ExprError(); + } + + // Look for the matching setter, in case it is needed. + IdentifierInfo *SetterName = + SelectorTable::constructSetterName(PP.getIdentifierTable(), &propertyName); + + Selector SetterSel = PP.getSelectorTable().getUnarySelector(SetterName); + ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); + if (!Setter) { + // If this reference is in an @implementation, also check for 'private' + // methods. + if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) + if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) + if (ObjCImplementationDecl *ImpDecl = + ObjCImplementations[ClassDecl->getIdentifier()]) + Setter = ImpDecl->getClassMethod(SetterSel); + } + // Look through local category implementations associated with the class. + if (!Setter) { + for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Setter; i++) { + if (ObjCCategoryImpls[i]->getClassInterface() == IFace) + Setter = ObjCCategoryImpls[i]->getClassMethod(SetterSel); + } + } + + if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc)) + return ExprError(); + + if (Getter || Setter) { + QualType PType; + + if (Getter) + PType = Getter->getResultType(); + else { + for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(), + E = Setter->param_end(); PI != E; ++PI) + PType = (*PI)->getType(); + } + return Owned(new (Context) ObjCKVCRefExpr(Getter, PType, Setter, + propertyNameLoc, IFace, receiverNameLoc)); + } + return ExprError(Diag(propertyNameLoc, diag::err_property_not_found) + << &propertyName << Context.getObjCInterfaceType(IFace)); +} + + // ActOnClassMessage - used for both unary and keyword messages. // ArgExprs is optional - if it is present, the number of expressions // is obtained from Sel.getNumArgs(). |