Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1 | //===--- SemaExprObjC.cpp - Semantic Analysis for ObjC Expressions --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for Objective-C expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
| 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/DeclObjC.h" |
| 17 | #include "clang/AST/Expr.h" |
| 18 | using namespace clang; |
| 19 | |
| 20 | Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs, |
| 21 | ExprTy **Strings, |
| 22 | unsigned NumStrings) { |
| 23 | SourceLocation AtLoc = AtLocs[0]; |
| 24 | StringLiteral* S = static_cast<StringLiteral *>(Strings[0]); |
| 25 | if (NumStrings > 1) { |
| 26 | // Concatenate objc strings. |
| 27 | StringLiteral* ES = static_cast<StringLiteral *>(Strings[NumStrings-1]); |
| 28 | SourceLocation EndLoc = ES->getSourceRange().getEnd(); |
| 29 | unsigned Length = 0; |
| 30 | for (unsigned i = 0; i < NumStrings; i++) |
| 31 | Length += static_cast<StringLiteral *>(Strings[i])->getByteLength(); |
| 32 | char *strBuf = new char [Length]; |
| 33 | char *p = strBuf; |
| 34 | bool isWide = false; |
| 35 | for (unsigned i = 0; i < NumStrings; i++) { |
| 36 | S = static_cast<StringLiteral *>(Strings[i]); |
| 37 | if (S->isWide()) |
| 38 | isWide = true; |
| 39 | memcpy(p, S->getStrData(), S->getByteLength()); |
| 40 | p += S->getByteLength(); |
| 41 | delete S; |
| 42 | } |
| 43 | S = new StringLiteral(strBuf, Length, |
| 44 | isWide, Context.getPointerType(Context.CharTy), |
| 45 | AtLoc, EndLoc); |
| 46 | } |
| 47 | |
| 48 | if (CheckBuiltinCFStringArgument(S)) |
| 49 | return true; |
| 50 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 51 | if (Context.getObjCConstantStringInterface().isNull()) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 52 | // Initialize the constant string interface lazily. This assumes |
| 53 | // the NSConstantString interface is seen in this translation unit. |
| 54 | IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString"); |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 55 | Decl *IFace = LookupDecl(NSIdent, Decl::IDNS_Ordinary, |
| 56 | SourceLocation(), TUScope); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 57 | ObjCInterfaceDecl *strIFace = dyn_cast_or_null<ObjCInterfaceDecl>(IFace); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 58 | if (!strIFace) |
| 59 | return Diag(S->getLocStart(), diag::err_undef_interface, |
| 60 | NSIdent->getName()); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 61 | Context.setObjCConstantStringInterface(strIFace); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 62 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 63 | QualType t = Context.getObjCConstantStringInterface(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 64 | t = Context.getPointerType(t); |
| 65 | return new ObjCStringLiteral(S, t, AtLoc); |
| 66 | } |
| 67 | |
| 68 | Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc, |
| 69 | SourceLocation EncodeLoc, |
| 70 | SourceLocation LParenLoc, |
| 71 | TypeTy *Ty, |
| 72 | SourceLocation RParenLoc) { |
| 73 | QualType EncodedType = QualType::getFromOpaquePtr(Ty); |
| 74 | |
| 75 | QualType t = Context.getPointerType(Context.CharTy); |
| 76 | return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc); |
| 77 | } |
| 78 | |
| 79 | Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel, |
| 80 | SourceLocation AtLoc, |
| 81 | SourceLocation SelLoc, |
| 82 | SourceLocation LParenLoc, |
| 83 | SourceLocation RParenLoc) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 84 | QualType t = Context.getObjCSelType(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 85 | return new ObjCSelectorExpr(t, Sel, AtLoc, RParenLoc); |
| 86 | } |
| 87 | |
| 88 | Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId, |
| 89 | SourceLocation AtLoc, |
| 90 | SourceLocation ProtoLoc, |
| 91 | SourceLocation LParenLoc, |
| 92 | SourceLocation RParenLoc) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 93 | ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId]; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 94 | if (!PDecl) { |
| 95 | Diag(ProtoLoc, diag::err_undeclared_protocol, ProtocolId->getName()); |
| 96 | return true; |
| 97 | } |
| 98 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 99 | QualType t = Context.getObjCProtoType(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 100 | if (t.isNull()) |
| 101 | return true; |
| 102 | t = Context.getPointerType(t); |
| 103 | return new ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc); |
| 104 | } |
| 105 | |
| 106 | bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 107 | ObjCMethodDecl *Method) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 108 | bool anyIncompatibleArgs = false; |
| 109 | |
| 110 | for (unsigned i = 0; i < NumArgs; i++) { |
| 111 | Expr *argExpr = Args[i]; |
| 112 | assert(argExpr && "CheckMessageArgumentTypes(): missing expression"); |
| 113 | |
| 114 | QualType lhsType = Method->getParamDecl(i)->getType(); |
| 115 | QualType rhsType = argExpr->getType(); |
| 116 | |
| 117 | // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8]. |
| 118 | if (const ArrayType *ary = lhsType->getAsArrayType()) |
| 119 | lhsType = Context.getPointerType(ary->getElementType()); |
| 120 | else if (lhsType->isFunctionType()) |
| 121 | lhsType = Context.getPointerType(lhsType); |
| 122 | |
| 123 | AssignConvertType Result = CheckSingleAssignmentConstraints(lhsType, |
| 124 | argExpr); |
| 125 | if (Args[i] != argExpr) // The expression was converted. |
| 126 | Args[i] = argExpr; // Make sure we store the converted expression. |
| 127 | |
| 128 | anyIncompatibleArgs |= |
| 129 | DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType, |
| 130 | argExpr, "sending"); |
| 131 | } |
| 132 | return anyIncompatibleArgs; |
| 133 | } |
| 134 | |
| 135 | // ActOnClassMessage - used for both unary and keyword messages. |
| 136 | // ArgExprs is optional - if it is present, the number of expressions |
| 137 | // is obtained from Sel.getNumArgs(). |
| 138 | Sema::ExprResult Sema::ActOnClassMessage( |
| 139 | Scope *S, |
| 140 | IdentifierInfo *receiverName, Selector Sel, |
| 141 | SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs) |
| 142 | { |
| 143 | assert(receiverName && "missing receiver class name"); |
| 144 | |
| 145 | Expr **ArgExprs = reinterpret_cast<Expr **>(Args); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 146 | ObjCInterfaceDecl* ClassDecl = 0; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 147 | if (!strcmp(receiverName->getName(), "super") && CurMethodDecl) { |
| 148 | ClassDecl = CurMethodDecl->getClassInterface()->getSuperClass(); |
Steve Naroff | 9511096 | 2008-03-28 21:37:05 +0000 | [diff] [blame] | 149 | if (!ClassDecl) |
| 150 | return Diag(lbrac, diag::error_no_super_class, |
| 151 | CurMethodDecl->getClassInterface()->getName()); |
| 152 | if (CurMethodDecl->isInstance()) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 153 | // Synthesize a cast to the super class. This hack allows us to loosely |
| 154 | // represent super without creating a special expression node. |
| 155 | IdentifierInfo &II = Context.Idents.get("self"); |
| 156 | ExprResult ReceiverExpr = ActOnIdentifierExpr(S, lbrac, II, false); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 157 | QualType superTy = Context.getObjCInterfaceType(ClassDecl); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 158 | superTy = Context.getPointerType(superTy); |
| 159 | ReceiverExpr = ActOnCastExpr(SourceLocation(), superTy.getAsOpaquePtr(), |
| 160 | SourceLocation(), ReceiverExpr.Val); |
| 161 | // We are really in an instance method, redirect. |
| 162 | return ActOnInstanceMessage(ReceiverExpr.Val, Sel, lbrac, rbrac, |
| 163 | Args, NumArgs); |
| 164 | } |
| 165 | // We are sending a message to 'super' within a class method. Do nothing, |
| 166 | // the receiver will pass through as 'super' (how convenient:-). |
| 167 | } else |
| 168 | ClassDecl = getObjCInterfaceDecl(receiverName); |
| 169 | |
| 170 | // FIXME: can ClassDecl ever be null? |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 171 | ObjCMethodDecl *Method = ClassDecl->lookupClassMethod(Sel); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 172 | QualType returnType; |
| 173 | |
| 174 | // Before we give up, check if the selector is an instance method. |
| 175 | if (!Method) |
| 176 | Method = ClassDecl->lookupInstanceMethod(Sel); |
| 177 | if (!Method) { |
| 178 | Diag(lbrac, diag::warn_method_not_found, std::string("+"), Sel.getName(), |
| 179 | SourceRange(lbrac, rbrac)); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 180 | returnType = Context.getObjCIdType(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 181 | } else { |
| 182 | returnType = Method->getResultType(); |
| 183 | if (Sel.getNumArgs()) { |
| 184 | if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method)) |
| 185 | return true; |
| 186 | } |
| 187 | } |
| 188 | return new ObjCMessageExpr(receiverName, Sel, returnType, Method, |
| 189 | lbrac, rbrac, ArgExprs, NumArgs); |
| 190 | } |
| 191 | |
| 192 | // ActOnInstanceMessage - used for both unary and keyword messages. |
| 193 | // ArgExprs is optional - if it is present, the number of expressions |
| 194 | // is obtained from Sel.getNumArgs(). |
| 195 | Sema::ExprResult Sema::ActOnInstanceMessage( |
| 196 | ExprTy *receiver, Selector Sel, |
| 197 | SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs) |
| 198 | { |
| 199 | assert(receiver && "missing receiver expression"); |
| 200 | |
| 201 | Expr **ArgExprs = reinterpret_cast<Expr **>(Args); |
| 202 | Expr *RExpr = static_cast<Expr *>(receiver); |
Chris Lattner | 6e10a08 | 2008-02-01 06:57:39 +0000 | [diff] [blame] | 203 | QualType receiverType = RExpr->getType().getCanonicalType(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 204 | QualType returnType; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 205 | ObjCMethodDecl *Method = 0; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 206 | |
Chris Lattner | 6e10a08 | 2008-02-01 06:57:39 +0000 | [diff] [blame] | 207 | // FIXME: This code is not stripping off type qualifiers! Should it? |
| 208 | if (receiverType == Context.getObjCIdType().getCanonicalType() || |
| 209 | receiverType == Context.getObjCClassType().getCanonicalType()) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 210 | Method = InstanceMethodPool[Sel].Method; |
Chris Lattner | 6e10a08 | 2008-02-01 06:57:39 +0000 | [diff] [blame] | 211 | if (!Method) |
| 212 | Method = FactoryMethodPool[Sel].Method; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 213 | if (!Method) { |
| 214 | Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(), |
| 215 | SourceRange(lbrac, rbrac)); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 216 | returnType = Context.getObjCIdType(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 217 | } else { |
| 218 | returnType = Method->getResultType(); |
| 219 | if (Sel.getNumArgs()) |
| 220 | if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method)) |
| 221 | return true; |
| 222 | } |
| 223 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 224 | bool receiverIsQualId = isa<ObjCQualifiedIdType>(receiverType); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 225 | // FIXME (snaroff): checking in this code from Patrick. Needs to be |
| 226 | // revisited. how do we get the ClassDecl from the receiver expression? |
| 227 | if (!receiverIsQualId) |
Chris Lattner | fb8cc1d | 2008-02-01 06:43:02 +0000 | [diff] [blame] | 228 | while (const PointerType *PTy = receiverType->getAsPointerType()) |
| 229 | receiverType = PTy->getPointeeType(); |
| 230 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 231 | ObjCInterfaceDecl* ClassDecl = 0; |
| 232 | if (ObjCQualifiedInterfaceType *QIT = |
| 233 | dyn_cast<ObjCQualifiedInterfaceType>(receiverType)) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 234 | ClassDecl = QIT->getDecl(); |
| 235 | Method = ClassDecl->lookupInstanceMethod(Sel); |
| 236 | if (!Method) { |
| 237 | // search protocols |
| 238 | for (unsigned i = 0; i < QIT->getNumProtocols(); i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 239 | ObjCProtocolDecl *PDecl = QIT->getProtocols(i); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 240 | if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel))) |
| 241 | break; |
| 242 | } |
| 243 | } |
| 244 | if (!Method) |
| 245 | Diag(lbrac, diag::warn_method_not_found_in_protocol, |
| 246 | std::string("-"), Sel.getName(), |
| 247 | SourceRange(lbrac, rbrac)); |
| 248 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 249 | else if (ObjCQualifiedIdType *QIT = |
| 250 | dyn_cast<ObjCQualifiedIdType>(receiverType)) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 251 | // search protocols |
| 252 | for (unsigned i = 0; i < QIT->getNumProtocols(); i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 253 | ObjCProtocolDecl *PDecl = QIT->getProtocols(i); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 254 | if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel))) |
| 255 | break; |
| 256 | } |
| 257 | if (!Method) |
| 258 | Diag(lbrac, diag::warn_method_not_found_in_protocol, |
| 259 | std::string("-"), Sel.getName(), |
| 260 | SourceRange(lbrac, rbrac)); |
| 261 | } |
| 262 | else { |
Chris Lattner | fb8cc1d | 2008-02-01 06:43:02 +0000 | [diff] [blame] | 263 | ObjCInterfaceType *OCIReceiver =dyn_cast<ObjCInterfaceType>(receiverType); |
| 264 | if (OCIReceiver == 0) { |
Chris Lattner | 6e10a08 | 2008-02-01 06:57:39 +0000 | [diff] [blame] | 265 | Diag(lbrac, diag::error_bad_receiver_type, |
| 266 | RExpr->getType().getAsString()); |
Fariborz Jahanian | 1dad5b2 | 2008-01-25 17:43:39 +0000 | [diff] [blame] | 267 | return true; |
| 268 | } |
Chris Lattner | fb8cc1d | 2008-02-01 06:43:02 +0000 | [diff] [blame] | 269 | ClassDecl = OCIReceiver->getDecl(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 270 | // FIXME: consider using InstanceMethodPool, since it will be faster |
| 271 | // than the following method (which can do *many* linear searches). The |
| 272 | // idea is to add class info to InstanceMethodPool... |
| 273 | Method = ClassDecl->lookupInstanceMethod(Sel); |
| 274 | } |
| 275 | if (!Method) { |
| 276 | // If we have an implementation in scope, check "private" methods. |
| 277 | if (ClassDecl) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 278 | if (ObjCImplementationDecl *ImpDecl = |
| 279 | ObjCImplementations[ClassDecl->getIdentifier()]) |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 280 | Method = ImpDecl->getInstanceMethod(Sel); |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 281 | // If we still haven't found a method, look in the global pool. This |
| 282 | // behavior isn't very desirable, however we need it for GCC |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 283 | // compatibility. |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 284 | if (!Method) |
| 285 | Method = InstanceMethodPool[Sel].Method; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 286 | } |
| 287 | if (!Method) { |
| 288 | Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(), |
| 289 | SourceRange(lbrac, rbrac)); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 290 | returnType = Context.getObjCIdType(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 291 | } else { |
| 292 | returnType = Method->getResultType(); |
| 293 | if (Sel.getNumArgs()) |
| 294 | if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method)) |
| 295 | return true; |
| 296 | } |
| 297 | } |
| 298 | return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac, |
| 299 | ArgExprs, NumArgs); |
| 300 | } |