blob: dc173eaf4de7a17507fda06677a32a0ae7bab015 [file] [log] [blame]
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Steve Naroff4985ace2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +000016#include "clang/Parse/Scope.h"
Chris Lattner500d3292009-01-29 05:15:15 +000017#include "clang/Parse/ParseDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "llvm/ADT/SmallVector.h"
19using namespace clang;
20
21
Chris Lattner891dca62008-12-08 21:53:24 +000022/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Reid Spencer5f016e22007-07-11 17:01:13 +000023/// external-declaration: [C99 6.9]
24/// [OBJC] objc-class-definition
Steve Naroff91fa0b72007-10-29 21:39:29 +000025/// [OBJC] objc-class-declaration
26/// [OBJC] objc-alias-declaration
27/// [OBJC] objc-protocol-definition
28/// [OBJC] objc-method-definition
29/// [OBJC] '@' 'end'
Chris Lattnerb28317a2009-03-28 19:18:32 +000030Parser::DeclPtrTy Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000031 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump1eb44332009-09-09 15:08:12 +000032
Steve Naroff861cf3e2007-08-23 18:16:40 +000033 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000034 case tok::objc_class:
35 return ParseObjCAtClassDeclaration(AtLoc);
36 case tok::objc_interface:
37 return ParseObjCAtInterfaceDeclaration(AtLoc);
38 case tok::objc_protocol:
39 return ParseObjCAtProtocolDeclaration(AtLoc);
40 case tok::objc_implementation:
41 return ParseObjCAtImplementationDeclaration(AtLoc);
42 case tok::objc_end:
43 return ParseObjCAtEndDeclaration(AtLoc);
44 case tok::objc_compatibility_alias:
45 return ParseObjCAtAliasDeclaration(AtLoc);
46 case tok::objc_synthesize:
47 return ParseObjCPropertySynthesize(AtLoc);
48 case tok::objc_dynamic:
49 return ParseObjCPropertyDynamic(AtLoc);
50 default:
51 Diag(AtLoc, diag::err_unexpected_at);
52 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +000053 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +000054 }
55}
56
57///
Mike Stump1eb44332009-09-09 15:08:12 +000058/// objc-class-declaration:
Reid Spencer5f016e22007-07-11 17:01:13 +000059/// '@' 'class' identifier-list ';'
Mike Stump1eb44332009-09-09 15:08:12 +000060///
Chris Lattnerb28317a2009-03-28 19:18:32 +000061Parser::DeclPtrTy Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000062 ConsumeToken(); // the identifier "class"
63 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
Ted Kremenekc09cba62009-11-17 23:12:20 +000064 llvm::SmallVector<SourceLocation, 8> ClassLocs;
65
Mike Stump1eb44332009-09-09 15:08:12 +000066
Reid Spencer5f016e22007-07-11 17:01:13 +000067 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000068 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000069 Diag(Tok, diag::err_expected_ident);
70 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +000071 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +000072 }
Reid Spencer5f016e22007-07-11 17:01:13 +000073 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremenekc09cba62009-11-17 23:12:20 +000074 ClassLocs.push_back(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +000075 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +000076
Chris Lattnerdf195262007-10-09 17:51:17 +000077 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000078 break;
Mike Stump1eb44332009-09-09 15:08:12 +000079
Reid Spencer5f016e22007-07-11 17:01:13 +000080 ConsumeToken();
81 }
Mike Stump1eb44332009-09-09 15:08:12 +000082
Reid Spencer5f016e22007-07-11 17:01:13 +000083 // Consume the ';'.
84 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Chris Lattnerb28317a2009-03-28 19:18:32 +000085 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +000086
Ted Kremenekc09cba62009-11-17 23:12:20 +000087 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
88 ClassLocs.data(),
89 ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000090}
91
Steve Naroffdac269b2007-08-20 21:31:48 +000092///
93/// objc-interface:
94/// objc-class-interface-attributes[opt] objc-class-interface
95/// objc-category-interface
96///
97/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +000098/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +000099/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000100/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000101/// objc-interface-decl-list
102/// @end
103///
104/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000105/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000106/// objc-protocol-refs[opt]
107/// objc-interface-decl-list
108/// @end
109///
110/// objc-superclass:
111/// ':' identifier
112///
113/// objc-class-interface-attributes:
114/// __attribute__((visibility("default")))
115/// __attribute__((visibility("hidden")))
116/// __attribute__((deprecated))
117/// __attribute__((unavailable))
118/// __attribute__((objc_exception)) - used by NSException on 64-bit
119///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000120Parser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration(
Steve Naroffdac269b2007-08-20 21:31:48 +0000121 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000122 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000123 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
124 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Chris Lattnerdf195262007-10-09 17:51:17 +0000126 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000127 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000128 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000129 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000130
Steve Naroffdac269b2007-08-20 21:31:48 +0000131 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000132 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000133 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Chris Lattnerdf195262007-10-09 17:51:17 +0000135 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000136 SourceLocation lparenLoc = ConsumeParen();
137 SourceLocation categoryLoc, rparenLoc;
138 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Steve Naroff527fe232007-08-23 19:56:30 +0000140 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000141 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000142 categoryId = Tok.getIdentifierInfo();
143 categoryLoc = ConsumeToken();
Steve Naroff527fe232007-08-23 19:56:30 +0000144 } else if (!getLang().ObjC2) {
145 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000146 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000147 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000148 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000149 Diag(Tok, diag::err_expected_rparen);
150 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +0000151 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000152 }
153 rparenLoc = ConsumeParen();
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Steve Naroffdac269b2007-08-20 21:31:48 +0000155 // Next, we need to check for any protocol references.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000156 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000157 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000158 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000159 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000160 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
161 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000162 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Steve Naroffdac269b2007-08-20 21:31:48 +0000164 if (attrList) // categories don't support attributes.
165 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Jay Foadbeaaccd2009-05-21 09:52:38 +0000167 DeclPtrTy CategoryType =
Mike Stump1eb44332009-09-09 15:08:12 +0000168 Actions.ActOnStartCategoryInterface(atLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000169 nameId, nameLoc,
170 categoryId, categoryLoc,
171 ProtocolRefs.data(),
172 ProtocolRefs.size(),
173 EndProtoLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000175 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000176 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000177 }
178 // Parse a class interface.
179 IdentifierInfo *superClassId = 0;
180 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000181
Chris Lattnerdf195262007-10-09 17:51:17 +0000182 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000183 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000184 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000185 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000186 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000187 }
188 superClassId = Tok.getIdentifierInfo();
189 superClassLoc = ConsumeToken();
190 }
191 // Next, we need to check for any protocol references.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000192 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000193 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
194 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000195 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000196 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
197 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000198 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000199
200 DeclPtrTy ClsType =
201 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000202 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000203 ProtocolRefs.data(), ProtocolRefs.size(),
Chris Lattner06036d32008-07-26 04:13:19 +0000204 EndProtoLoc, attrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Chris Lattnerdf195262007-10-09 17:51:17 +0000206 if (Tok.is(tok::l_brace))
Steve Naroff60fccee2007-10-29 21:38:07 +0000207 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000208
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000209 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000210 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000211}
212
213/// objc-interface-decl-list:
214/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000215/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000216/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000217/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000218/// objc-interface-decl-list declaration
219/// objc-interface-decl-list ';'
220///
Steve Naroff294494e2007-08-22 16:35:03 +0000221/// objc-method-requirement: [OBJC2]
222/// @required
223/// @optional
224///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000225void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000226 tok::ObjCKeywordKind contextKey) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000227 llvm::SmallVector<DeclPtrTy, 32> allMethods;
228 llvm::SmallVector<DeclPtrTy, 16> allProperties;
Chris Lattner682bf922009-03-29 16:50:03 +0000229 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000230 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Chris Lattnerbc662af2008-10-20 06:10:06 +0000232 SourceLocation AtEndLoc;
233
Steve Naroff294494e2007-08-22 16:35:03 +0000234 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000235 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000236 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000237 DeclPtrTy methodPrototype =
Chris Lattnerdf195262007-10-09 17:51:17 +0000238 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000239 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000240 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
241 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000242 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
243 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000244 continue;
245 }
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Chris Lattnere82a10f2008-10-20 05:46:22 +0000247 // Ignore excess semicolons.
248 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000249 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000250 continue;
251 }
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Chris Lattnerbc662af2008-10-20 06:10:06 +0000253 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000254 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000255 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Chris Lattnere82a10f2008-10-20 05:46:22 +0000257 // If we don't have an @ directive, parse it as a function definition.
258 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000259 // The code below does not consume '}'s because it is afraid of eating the
260 // end of a namespace. Because of the way this code is structured, an
261 // erroneous r_brace would cause an infinite loop if not handled here.
262 if (Tok.is(tok::r_brace))
263 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Steve Naroff4985ace2007-08-22 18:35:33 +0000265 // FIXME: as the name implies, this rule allows function definitions.
266 // We could pass a flag or check for functions during semantic analysis.
Chris Lattner682bf922009-03-29 16:50:03 +0000267 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000268 continue;
269 }
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Chris Lattnere82a10f2008-10-20 05:46:22 +0000271 // Otherwise, we have an @ directive, eat the @.
272 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnera2449b22008-10-20 05:57:40 +0000273 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Chris Lattnera2449b22008-10-20 05:57:40 +0000275 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattnere82a10f2008-10-20 05:46:22 +0000276 AtEndLoc = AtLoc;
277 break;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000278 }
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Chris Lattnerbc662af2008-10-20 06:10:06 +0000280 // Eat the identifier.
281 ConsumeToken();
282
Chris Lattnera2449b22008-10-20 05:57:40 +0000283 switch (DirectiveKind) {
284 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000285 // FIXME: If someone forgets an @end on a protocol, this loop will
286 // continue to eat up tons of stuff and spew lots of nonsense errors. It
287 // would probably be better to bail out if we saw an @class or @interface
288 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000289 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000290 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000291 SkipUntil(tok::r_brace, tok::at);
292 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Chris Lattnera2449b22008-10-20 05:57:40 +0000294 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000295 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000296 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000297 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000298 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000299 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000300 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000301 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000302 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Chris Lattnera2449b22008-10-20 05:57:40 +0000304 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000305 if (!getLang().ObjC2)
306 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
307
Chris Lattnere82a10f2008-10-20 05:46:22 +0000308 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000309 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000310 if (Tok.is(tok::l_paren))
Chris Lattnere82a10f2008-10-20 05:46:22 +0000311 ParseObjCPropertyAttribute(OCDS);
Mike Stump1eb44332009-09-09 15:08:12 +0000312
John McCallbdd563e2009-11-03 02:38:08 +0000313 struct ObjCPropertyCallback : FieldCallback {
314 Parser &P;
315 DeclPtrTy IDecl;
316 llvm::SmallVectorImpl<DeclPtrTy> &Props;
317 ObjCDeclSpec &OCDS;
318 SourceLocation AtLoc;
319 tok::ObjCKeywordKind MethodImplKind;
320
321 ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
322 llvm::SmallVectorImpl<DeclPtrTy> &Props,
323 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
324 tok::ObjCKeywordKind MethodImplKind) :
325 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
326 MethodImplKind(MethodImplKind) {
327 }
328
329 DeclPtrTy invoke(FieldDeclarator &FD) {
330 if (FD.D.getIdentifier() == 0) {
331 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
332 << FD.D.getSourceRange();
333 return DeclPtrTy();
334 }
335 if (FD.BitfieldSize) {
336 P.Diag(AtLoc, diag::err_objc_property_bitfield)
337 << FD.D.getSourceRange();
338 return DeclPtrTy();
339 }
340
341 // Install the property declarator into interfaceDecl.
342 IdentifierInfo *SelName =
343 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
344
345 Selector GetterSel =
346 P.PP.getSelectorTable().getNullarySelector(SelName);
347 IdentifierInfo *SetterName = OCDS.getSetterName();
348 Selector SetterSel;
349 if (SetterName)
350 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
351 else
352 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
353 P.PP.getSelectorTable(),
354 FD.D.getIdentifier());
355 bool isOverridingProperty = false;
356 DeclPtrTy Property =
357 P.Actions.ActOnProperty(P.CurScope, AtLoc, FD, OCDS,
358 GetterSel, SetterSel, IDecl,
359 &isOverridingProperty,
360 MethodImplKind);
361 if (!isOverridingProperty)
362 Props.push_back(Property);
363
364 return Property;
365 }
366 } Callback(*this, interfaceDecl, allProperties,
367 OCDS, AtLoc, MethodImplKind);
368
Chris Lattnere82a10f2008-10-20 05:46:22 +0000369 // Parse all the comma separated declarators.
370 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +0000371 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000373 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
374 tok::at);
Chris Lattnera2449b22008-10-20 05:57:40 +0000375 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000376 }
Steve Naroff294494e2007-08-22 16:35:03 +0000377 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000378
379 // We break out of the big loop in two cases: when we see @end or when we see
380 // EOF. In the former case, eat the @end. In the later case, emit an error.
381 if (Tok.isObjCAtKeyword(tok::objc_end))
382 ConsumeToken(); // the "end" identifier
383 else
384 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Chris Lattnera2449b22008-10-20 05:57:40 +0000386 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000387 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8a779312008-06-06 16:45:15 +0000388 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000389 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000390 allProperties.data(), allProperties.size(),
391 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000392}
393
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000394/// Parse property attribute declarations.
395///
396/// property-attr-decl: '(' property-attrlist ')'
397/// property-attrlist:
398/// property-attribute
399/// property-attrlist ',' property-attribute
400/// property-attribute:
401/// getter '=' identifier
402/// setter '=' identifier ':'
403/// readonly
404/// readwrite
405/// assign
406/// retain
407/// copy
408/// nonatomic
409///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000410void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000411 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000412 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000414 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000415 if (Tok.is(tok::code_completion)) {
416 Actions.CodeCompleteObjCProperty(CurScope, DS);
417 ConsumeToken();
418 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000419 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000421 // If this is not an identifier at all, bail out early.
422 if (II == 0) {
423 MatchRHSPunctuation(tok::r_paren, LHSLoc);
424 return;
425 }
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Chris Lattner156b0612008-10-20 07:37:22 +0000427 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Chris Lattner92e62b02008-11-20 04:42:34 +0000429 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000430 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000431 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000432 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000433 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000434 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000435 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000436 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000437 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000438 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000439 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000440 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000441 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattnere00da7c2008-10-20 07:39:53 +0000442 // getter/setter require extra treatment.
Chris Lattner156b0612008-10-20 07:37:22 +0000443 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
444 tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000445 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Chris Lattner8ca329c2008-10-20 07:24:39 +0000447 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000448 Diag(Tok, diag::err_expected_ident);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000449 SkipUntil(tok::r_paren);
450 return;
451 }
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Daniel Dunbare013d682009-10-18 20:26:12 +0000453 if (II->getNameStart()[0] == 's') {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000454 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
455 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000456 ConsumeToken(); // consume method name
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Chris Lattner156b0612008-10-20 07:37:22 +0000458 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
459 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000460 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000461 } else {
462 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
463 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000464 ConsumeToken(); // consume method name
Chris Lattner8ca329c2008-10-20 07:24:39 +0000465 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000466 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000467 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000468 SkipUntil(tok::r_paren);
469 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000470 }
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Chris Lattner156b0612008-10-20 07:37:22 +0000472 if (Tok.isNot(tok::comma))
473 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Chris Lattner156b0612008-10-20 07:37:22 +0000475 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000476 }
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Chris Lattner156b0612008-10-20 07:37:22 +0000478 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000479}
480
Steve Naroff3536b442007-09-06 21:24:23 +0000481/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000482/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000483/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000484///
485/// objc-instance-method: '-'
486/// objc-class-method: '+'
487///
Steve Naroff4985ace2007-08-22 18:35:33 +0000488/// objc-method-attributes: [OBJC2]
489/// __attribute__((deprecated))
490///
Mike Stump1eb44332009-09-09 15:08:12 +0000491Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000492 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000493 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000494
Mike Stump1eb44332009-09-09 15:08:12 +0000495 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000496 SourceLocation mLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Chris Lattnerb28317a2009-03-28 19:18:32 +0000498 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000499 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000500 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000501 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000502}
503
504/// objc-selector:
505/// identifier
506/// one of
507/// enum struct union if else while do for switch case default
508/// break continue return goto asm sizeof typeof __alignof
509/// unsigned long const short volatile signed restrict _Complex
510/// in out inout bycopy byref oneway int char float double void _Bool
511///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000512IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000513 switch (Tok.getKind()) {
514 default:
515 return 0;
516 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000517 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000518 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000519 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000520 case tok::kw_break:
521 case tok::kw_case:
522 case tok::kw_catch:
523 case tok::kw_char:
524 case tok::kw_class:
525 case tok::kw_const:
526 case tok::kw_const_cast:
527 case tok::kw_continue:
528 case tok::kw_default:
529 case tok::kw_delete:
530 case tok::kw_do:
531 case tok::kw_double:
532 case tok::kw_dynamic_cast:
533 case tok::kw_else:
534 case tok::kw_enum:
535 case tok::kw_explicit:
536 case tok::kw_export:
537 case tok::kw_extern:
538 case tok::kw_false:
539 case tok::kw_float:
540 case tok::kw_for:
541 case tok::kw_friend:
542 case tok::kw_goto:
543 case tok::kw_if:
544 case tok::kw_inline:
545 case tok::kw_int:
546 case tok::kw_long:
547 case tok::kw_mutable:
548 case tok::kw_namespace:
549 case tok::kw_new:
550 case tok::kw_operator:
551 case tok::kw_private:
552 case tok::kw_protected:
553 case tok::kw_public:
554 case tok::kw_register:
555 case tok::kw_reinterpret_cast:
556 case tok::kw_restrict:
557 case tok::kw_return:
558 case tok::kw_short:
559 case tok::kw_signed:
560 case tok::kw_sizeof:
561 case tok::kw_static:
562 case tok::kw_static_cast:
563 case tok::kw_struct:
564 case tok::kw_switch:
565 case tok::kw_template:
566 case tok::kw_this:
567 case tok::kw_throw:
568 case tok::kw_true:
569 case tok::kw_try:
570 case tok::kw_typedef:
571 case tok::kw_typeid:
572 case tok::kw_typename:
573 case tok::kw_typeof:
574 case tok::kw_union:
575 case tok::kw_unsigned:
576 case tok::kw_using:
577 case tok::kw_virtual:
578 case tok::kw_void:
579 case tok::kw_volatile:
580 case tok::kw_wchar_t:
581 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000582 case tok::kw__Bool:
583 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000584 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000585 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000586 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000587 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000588 }
Steve Naroff294494e2007-08-22 16:35:03 +0000589}
590
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000591/// objc-for-collection-in: 'in'
592///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000593bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000594 // FIXME: May have to do additional look-ahead to only allow for
595 // valid tokens following an 'in'; such as an identifier, unary operators,
596 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000597 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000598 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000599}
600
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000601/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000602/// qualifier list and builds their bitmask representation in the input
603/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000604///
605/// objc-type-qualifiers:
606/// objc-type-qualifier
607/// objc-type-qualifiers objc-type-qualifier
608///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000609void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000610 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000611 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000612 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Chris Lattnere8b724d2007-12-12 06:56:32 +0000614 const IdentifierInfo *II = Tok.getIdentifierInfo();
615 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000616 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000617 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000619 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000620 switch (i) {
621 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000622 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
623 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
624 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
625 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
626 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
627 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000628 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000629 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000630 ConsumeToken();
631 II = 0;
632 break;
633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Chris Lattnere8b724d2007-12-12 06:56:32 +0000635 // If this wasn't a recognized qualifier, bail out.
636 if (II) return;
637 }
638}
639
640/// objc-type-name:
641/// '(' objc-type-qualifiers[opt] type-name ')'
642/// '(' objc-type-qualifiers[opt] ')'
643///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000644Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000645 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Chris Lattner4a76b292008-10-22 03:52:06 +0000647 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000648 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000650 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000651 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000652
Chris Lattner4a76b292008-10-22 03:52:06 +0000653 TypeTy *Ty = 0;
Douglas Gregor809070a2009-02-18 17:45:20 +0000654 if (isTypeSpecifierQualifier()) {
655 TypeResult TypeSpec = ParseTypeName();
656 if (!TypeSpec.isInvalid())
657 Ty = TypeSpec.get();
658 }
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Steve Naroffd7333c22008-10-21 14:15:04 +0000660 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000661 ConsumeParen();
662 else if (Tok.getLocation() == TypeStartLoc) {
663 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000664 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000665 SkipUntil(tok::r_paren);
666 } else {
667 // Otherwise, we found *something*, but didn't get a ')' in the right
668 // place. Emit an error then return what we have as the type.
669 MatchRHSPunctuation(tok::r_paren, LParenLoc);
670 }
Steve Narofff28b2642007-09-05 23:30:30 +0000671 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000672}
673
674/// objc-method-decl:
675/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000676/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000677/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000678/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000679///
680/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000681/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000682/// objc-keyword-selector objc-keyword-decl
683///
684/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000685/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
686/// objc-selector ':' objc-keyword-attributes[opt] identifier
687/// ':' objc-type-name objc-keyword-attributes[opt] identifier
688/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000689///
Steve Naroff4985ace2007-08-22 18:35:33 +0000690/// objc-parmlist:
691/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000692///
Steve Naroff4985ace2007-08-22 18:35:33 +0000693/// objc-parms:
694/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000695///
Steve Naroff4985ace2007-08-22 18:35:33 +0000696/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000697/// , ...
698///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000699/// objc-keyword-attributes: [OBJC2]
700/// __attribute__((unused))
701///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000702Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
703 tok::TokenKind mType,
704 DeclPtrTy IDecl,
705 tok::ObjCKeywordKind MethodImplKind) {
John McCall54abf7d2009-11-04 02:18:39 +0000706 ParsingDeclRAIIObject PD(*this);
707
Chris Lattnere8904e92008-08-23 01:48:03 +0000708 // Parse the return type if present.
Chris Lattnerff384912007-10-07 02:00:24 +0000709 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000710 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000711 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000712 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Steve Naroffbef11852007-10-26 20:53:56 +0000714 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000715 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000716
Steve Naroff84c43102009-02-11 20:43:13 +0000717 // An unnamed colon is valid.
718 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000719 Diag(Tok, diag::err_expected_selector_for_method)
720 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000721 // Skip until we get a ; or {}.
722 SkipUntil(tok::r_brace);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000723 return DeclPtrTy();
Chris Lattnere8904e92008-08-23 01:48:03 +0000724 }
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000726 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattnerdf195262007-10-09 17:51:17 +0000727 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000728 // If attributes exist after the method, parse them.
729 AttributeList *MethodAttrs = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000730 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000731 MethodAttrs = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Chris Lattnerff384912007-10-07 02:00:24 +0000733 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall54abf7d2009-11-04 02:18:39 +0000734 DeclPtrTy Result
735 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000736 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000737 0, CargNames, MethodAttrs,
738 MethodImplKind);
John McCall54abf7d2009-11-04 02:18:39 +0000739 PD.complete(Result);
740 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000741 }
Steve Narofff28b2642007-09-05 23:30:30 +0000742
Steve Naroff68d331a2007-09-27 14:38:14 +0000743 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnere294d3f2009-04-11 18:57:04 +0000744 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Chris Lattnerff384912007-10-07 02:00:24 +0000746 while (1) {
Chris Lattnere294d3f2009-04-11 18:57:04 +0000747 Action::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Chris Lattnerff384912007-10-07 02:00:24 +0000749 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000750 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000751 Diag(Tok, diag::err_expected_colon);
752 break;
753 }
754 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Chris Lattnere294d3f2009-04-11 18:57:04 +0000756 ArgInfo.Type = 0;
757 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
758 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
759
Chris Lattnerff384912007-10-07 02:00:24 +0000760 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000761 ArgInfo.ArgAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000762 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnere294d3f2009-04-11 18:57:04 +0000763 ArgInfo.ArgAttrs = ParseAttributes();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000764
Chris Lattnerdf195262007-10-09 17:51:17 +0000765 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000766 Diag(Tok, diag::err_expected_ident); // missing argument name.
767 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000768 }
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Chris Lattnere294d3f2009-04-11 18:57:04 +0000770 ArgInfo.Name = Tok.getIdentifierInfo();
771 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000772 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Chris Lattnere294d3f2009-04-11 18:57:04 +0000774 ArgInfos.push_back(ArgInfo);
775 KeyIdents.push_back(SelIdent);
776
Chris Lattnerff384912007-10-07 02:00:24 +0000777 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000778 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000779 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000780 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000781 break;
782 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000783 }
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Steve Naroff335eafa2007-11-15 12:35:21 +0000785 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Chris Lattnerff384912007-10-07 02:00:24 +0000787 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000788 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000789 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000790 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000791 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000792 ConsumeToken();
793 break;
794 }
Chris Lattnerff384912007-10-07 02:00:24 +0000795 DeclSpec DS;
796 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000797 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +0000798 Declarator ParmDecl(DS, Declarator::PrototypeContext);
799 ParseDeclarator(ParmDecl);
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000800 CargNames.push_back(ParmDecl);
Chris Lattnerff384912007-10-07 02:00:24 +0000801 }
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Chris Lattnerff384912007-10-07 02:00:24 +0000803 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000804 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000805 AttributeList *MethodAttrs = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000806 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000807 MethodAttrs = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000809 if (KeyIdents.size() == 0)
810 return DeclPtrTy();
Chris Lattnerff384912007-10-07 02:00:24 +0000811 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
812 &KeyIdents[0]);
John McCall54abf7d2009-11-04 02:18:39 +0000813 DeclPtrTy Result
814 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000815 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000816 &ArgInfos[0], CargNames, MethodAttrs,
Steve Naroff335eafa2007-11-15 12:35:21 +0000817 MethodImplKind, isVariadic);
John McCall54abf7d2009-11-04 02:18:39 +0000818 PD.complete(Result);
819 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +0000820}
821
Steve Naroffdac269b2007-08-20 21:31:48 +0000822/// objc-protocol-refs:
823/// '<' identifier-list '>'
824///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000825bool Parser::
Chris Lattnerb28317a2009-03-28 19:18:32 +0000826ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000827 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
828 bool WarnOnDeclarations,
829 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +0000830 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000832 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Chris Lattnere13b9592008-07-26 04:03:38 +0000834 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Chris Lattnere13b9592008-07-26 04:03:38 +0000836 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +0000837 if (Tok.is(tok::code_completion)) {
838 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
839 ProtocolIdents.size());
840 ConsumeToken();
841 }
842
Chris Lattnere13b9592008-07-26 04:03:38 +0000843 if (Tok.isNot(tok::identifier)) {
844 Diag(Tok, diag::err_expected_ident);
845 SkipUntil(tok::greater);
846 return true;
847 }
848 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
849 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000850 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +0000851 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Chris Lattnere13b9592008-07-26 04:03:38 +0000853 if (Tok.isNot(tok::comma))
854 break;
855 ConsumeToken();
856 }
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Chris Lattnere13b9592008-07-26 04:03:38 +0000858 // Consume the '>'.
859 if (Tok.isNot(tok::greater)) {
860 Diag(Tok, diag::err_expected_greater);
861 return true;
862 }
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Chris Lattnere13b9592008-07-26 04:03:38 +0000864 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Chris Lattnere13b9592008-07-26 04:03:38 +0000866 // Convert the list of protocols identifiers into a list of protocol decls.
867 Actions.FindProtocolDeclaration(WarnOnDeclarations,
868 &ProtocolIdents[0], ProtocolIdents.size(),
869 Protocols);
870 return false;
871}
872
Steve Naroffdac269b2007-08-20 21:31:48 +0000873/// objc-class-instance-variables:
874/// '{' objc-instance-variable-decl-list[opt] '}'
875///
876/// objc-instance-variable-decl-list:
877/// objc-visibility-spec
878/// objc-instance-variable-decl ';'
879/// ';'
880/// objc-instance-variable-decl-list objc-visibility-spec
881/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
882/// objc-instance-variable-decl-list ';'
883///
884/// objc-visibility-spec:
885/// @private
886/// @protected
887/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000888/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000889///
890/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +0000891/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +0000892///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000893void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff60fccee2007-10-29 21:38:07 +0000894 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000895 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattnerb28317a2009-03-28 19:18:32 +0000896 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000897
Douglas Gregor1a0d31a2009-01-12 18:45:55 +0000898 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +0000899
Steve Naroffddbff782007-08-21 21:17:12 +0000900 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000902 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000903 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000904 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000905 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Steve Naroffddbff782007-08-21 21:17:12 +0000907 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000908 if (Tok.is(tok::semi)) {
Chris Lattnerc2253f52009-11-06 06:40:12 +0000909 Diag(Tok, diag::ext_extra_struct_semi)
910 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Steve Naroffddbff782007-08-21 21:17:12 +0000911 ConsumeToken();
912 continue;
913 }
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Steve Naroffddbff782007-08-21 21:17:12 +0000915 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000916 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000917 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000918 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000919 case tok::objc_private:
920 case tok::objc_public:
921 case tok::objc_protected:
922 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000923 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000924 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000925 continue;
Steve Naroffddbff782007-08-21 21:17:12 +0000926 default:
927 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000928 continue;
929 }
930 }
Mike Stump1eb44332009-09-09 15:08:12 +0000931
John McCallbdd563e2009-11-03 02:38:08 +0000932 struct ObjCIvarCallback : FieldCallback {
933 Parser &P;
934 DeclPtrTy IDecl;
935 tok::ObjCKeywordKind visibility;
936 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
937
938 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
939 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
940 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
941 }
942
943 DeclPtrTy invoke(FieldDeclarator &FD) {
944 // Install the declarator into the interface decl.
945 DeclPtrTy Field
946 = P.Actions.ActOnIvar(P.CurScope,
947 FD.D.getDeclSpec().getSourceRange().getBegin(),
948 IDecl, FD.D, FD.BitfieldSize, visibility);
949 AllIvarDecls.push_back(Field);
950 return Field;
951 }
952 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
953
Chris Lattnere1359422008-04-10 06:46:29 +0000954 // Parse all the comma separated declarators.
955 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +0000956 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Chris Lattnerdf195262007-10-09 17:51:17 +0000958 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000959 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +0000960 } else {
961 Diag(Tok, diag::err_expected_semi_decl_list);
962 // Skip to end of block or statement
963 SkipUntil(tok::r_brace, true, true);
964 }
965 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000966 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000967 // Call ActOnFields() even if we don't have any decls. This is useful
968 // for code rewriting tools that need to be aware of the empty list.
969 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000970 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +0000971 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +0000972 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000973}
Steve Naroffdac269b2007-08-20 21:31:48 +0000974
975/// objc-protocol-declaration:
976/// objc-protocol-definition
977/// objc-protocol-forward-reference
978///
979/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +0000980/// @protocol identifier
981/// objc-protocol-refs[opt]
982/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000983/// @end
984///
985/// objc-protocol-forward-reference:
986/// @protocol identifier-list ';'
987///
988/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000989/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000990/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000991Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
992 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000993 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000994 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
995 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Chris Lattnerdf195262007-10-09 17:51:17 +0000997 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000998 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000999 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001000 }
1001 // Save the protocol name, then consume it.
1002 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1003 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Chris Lattnerdf195262007-10-09 17:51:17 +00001005 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001006 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001007 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001008 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001009 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001010 }
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Chris Lattnerdf195262007-10-09 17:51:17 +00001012 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001013 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1014 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1015
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001016 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001017 while (1) {
1018 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001019 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001020 Diag(Tok, diag::err_expected_ident);
1021 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001022 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001023 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001024 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1025 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001026 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Chris Lattnerdf195262007-10-09 17:51:17 +00001028 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001029 break;
1030 }
1031 // Consume the ';'.
1032 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001033 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001034
Steve Naroffe440eb82007-10-10 17:32:04 +00001035 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001036 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001037 ProtocolRefs.size(),
1038 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +00001039 }
Mike Stump1eb44332009-09-09 15:08:12 +00001040
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001041 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001042 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001043
Chris Lattnerb28317a2009-03-28 19:18:32 +00001044 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001045 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001046 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001047 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1048 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001049 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Chris Lattnerb28317a2009-03-28 19:18:32 +00001051 DeclPtrTy ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001052 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001053 ProtocolRefs.data(),
1054 ProtocolRefs.size(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +00001055 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001056 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001057 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001058}
Steve Naroffdac269b2007-08-20 21:31:48 +00001059
1060/// objc-implementation:
1061/// objc-class-implementation-prologue
1062/// objc-category-implementation-prologue
1063///
1064/// objc-class-implementation-prologue:
1065/// @implementation identifier objc-superclass[opt]
1066/// objc-class-instance-variables[opt]
1067///
1068/// objc-category-implementation-prologue:
1069/// @implementation identifier ( identifier )
Chris Lattnerb28317a2009-03-28 19:18:32 +00001070Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001071 SourceLocation atLoc) {
1072 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1073 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1074 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Chris Lattnerdf195262007-10-09 17:51:17 +00001076 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001077 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001078 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001079 }
1080 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001081 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001082 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001083
1084 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001085 // we have a category implementation.
1086 SourceLocation lparenLoc = ConsumeParen();
1087 SourceLocation categoryLoc, rparenLoc;
1088 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Chris Lattnerdf195262007-10-09 17:51:17 +00001090 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001091 categoryId = Tok.getIdentifierInfo();
1092 categoryLoc = ConsumeToken();
1093 } else {
1094 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001095 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001096 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001097 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001098 Diag(Tok, diag::err_expected_rparen);
1099 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001100 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001101 }
1102 rparenLoc = ConsumeParen();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001103 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001104 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001105 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001106 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001107 PendingObjCImpDecl.push_back(ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001108 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001109 }
1110 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001111 SourceLocation superClassLoc;
1112 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001113 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001114 // We have a super class
1115 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001116 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001117 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001118 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001119 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001120 superClassId = Tok.getIdentifierInfo();
1121 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001122 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001123 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001124 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001125 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001126
Steve Naroff60fccee2007-10-29 21:38:07 +00001127 if (Tok.is(tok::l_brace)) // we have ivars
1128 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001129 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001130 PendingObjCImpDecl.push_back(ObjCImpDecl);
1131
Chris Lattnerb28317a2009-03-28 19:18:32 +00001132 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001133}
Steve Naroff60fccee2007-10-29 21:38:07 +00001134
Chris Lattnerb28317a2009-03-28 19:18:32 +00001135Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001136 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1137 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001138 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001139 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001140 if (ObjCImpDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001141 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001142 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001143 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001144 }
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001145 else
1146 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001147 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001148}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001149
Fariborz Jahanian1ac71042009-11-17 17:15:16 +00001150Parser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001151 if (PendingObjCImpDecl.empty())
1152 return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
1153 DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
1154 Actions.ActOnAtEnd(SourceLocation(), ImpDecl);
1155 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1156}
1157
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001158/// compatibility-alias-decl:
1159/// @compatibility_alias alias-name class-name ';'
1160///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001161Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001162 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1163 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1164 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001165 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001166 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001167 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001168 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001169 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1170 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001171 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001172 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001173 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001174 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001175 IdentifierInfo *classId = Tok.getIdentifierInfo();
1176 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1177 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001178 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001179 return DeclPtrTy();
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001180 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001181 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1182 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001183}
1184
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001185/// property-synthesis:
1186/// @synthesize property-ivar-list ';'
1187///
1188/// property-ivar-list:
1189/// property-ivar
1190/// property-ivar-list ',' property-ivar
1191///
1192/// property-ivar:
1193/// identifier
1194/// identifier '=' identifier
1195///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001196Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001197 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1198 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001199 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnerdf195262007-10-09 17:51:17 +00001200 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001201 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001202 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001203 }
Mike Stump1eb44332009-09-09 15:08:12 +00001204
Chris Lattnerdf195262007-10-09 17:51:17 +00001205 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001206 IdentifierInfo *propertyIvar = 0;
1207 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1208 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001209 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001210 // property '=' ivar-name
1211 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +00001212 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001213 Diag(Tok, diag::err_expected_ident);
1214 break;
1215 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001216 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001217 ConsumeToken(); // consume ivar-name
1218 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001219 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1220 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001221 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001222 break;
1223 ConsumeToken(); // consume ','
1224 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001225 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001226 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Fariborz Jahaniand3fdcb52009-11-06 21:48:47 +00001227 else
1228 ConsumeToken(); // consume ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001229 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001230}
1231
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001232/// property-dynamic:
1233/// @dynamic property-list
1234///
1235/// property-list:
1236/// identifier
1237/// property-list ',' identifier
1238///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001239Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001240 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1241 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1242 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001243 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001244 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001245 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001246 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001247 while (Tok.is(tok::identifier)) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001248 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1249 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1250 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1251 propertyId, 0);
1252
Chris Lattnerdf195262007-10-09 17:51:17 +00001253 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001254 break;
1255 ConsumeToken(); // consume ','
1256 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001257 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001258 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001259 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001260}
Mike Stump1eb44332009-09-09 15:08:12 +00001261
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001262/// objc-throw-statement:
1263/// throw expression[opt];
1264///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001265Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001266 OwningExprResult Res(Actions);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001267 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001268 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001269 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001270 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001271 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001272 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001273 }
1274 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001275 ConsumeToken(); // consume ';'
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001276 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001277}
1278
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001279/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001280/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001281///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001282Parser::OwningStmtResult
1283Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001284 ConsumeToken(); // consume synchronized
1285 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001286 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001287 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001288 }
1289 ConsumeParen(); // '('
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001290 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001291 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001292 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001293 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001294 }
1295 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001296 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001297 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001298 }
1299 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001300 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001301 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001302 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001303 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001304 // Enter a scope to hold everything within the compound stmt. Compound
1305 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001306 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001307
Sebastian Redl61364dd2008-12-11 19:30:53 +00001308 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001309
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001310 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001311 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001312 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001313 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001314}
1315
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001316/// objc-try-catch-statement:
1317/// @try compound-statement objc-catch-list[opt]
1318/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1319///
1320/// objc-catch-list:
1321/// @catch ( parameter-declaration ) compound-statement
1322/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1323/// catch-parameter-declaration:
1324/// parameter-declaration
1325/// '...' [OBJC2]
1326///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001327Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001328 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001329
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001330 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001331 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001332 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001333 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001334 }
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001335 OwningStmtResult CatchStmts(Actions);
1336 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001337 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001338 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001339 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001340 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001341 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001342
Chris Lattnerdf195262007-10-09 17:51:17 +00001343 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001344 // At this point, we need to lookahead to determine if this @ is the start
1345 // of an @catch or @finally. We don't want to consume the @ token if this
1346 // is an @try or @encode or something else.
1347 Token AfterAt = GetLookAheadToken(1);
1348 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1349 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1350 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001351
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001352 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001353 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001354 DeclPtrTy FirstPart;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001355 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001356 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001357 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001358 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001359 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001360 DeclSpec DS;
1361 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001362 // For some odd reason, the name of the exception variable is
Mike Stump1eb44332009-09-09 15:08:12 +00001363 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff7ba138a2009-03-03 19:52:17 +00001364 // we must accept either 'declarator' or 'abstract-declarator' here.
1365 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1366 ParseDeclarator(ParmDecl);
1367
1368 // Inform the actions module about the parameter declarator, so it
1369 // gets added to the current scope.
1370 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001371 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001372 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Steve Naroff93a25952009-04-07 22:56:58 +00001374 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001375
Steve Naroff93a25952009-04-07 22:56:58 +00001376 if (Tok.is(tok::r_paren))
1377 RParenLoc = ConsumeParen();
1378 else // Skip over garbage, until we get to ')'. Eat the ')'.
1379 SkipUntil(tok::r_paren, true, false);
1380
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001381 OwningStmtResult CatchBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001382 if (Tok.is(tok::l_brace))
1383 CatchBody = ParseCompoundStatementBody();
1384 else
1385 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001386 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001387 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001388 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff7ba138a2009-03-03 19:52:17 +00001389 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001390 move(CatchStmts));
Steve Naroff64515f32008-02-05 21:27:35 +00001391 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001392 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1393 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001394 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001395 }
1396 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001397 } else {
1398 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001399 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001400 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001401
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001402 OwningStmtResult FinallyBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001403 if (Tok.is(tok::l_brace))
1404 FinallyBody = ParseCompoundStatementBody();
1405 else
1406 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001407 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001408 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001409 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001410 move(FinallyBody));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001411 catch_or_finally_seen = true;
1412 break;
1413 }
1414 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001415 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001416 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001417 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001418 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001419 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1420 move(FinallyStmt));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001421}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001422
Steve Naroff3536b442007-09-06 21:24:23 +00001423/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001424///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001425Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1426 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001427
Chris Lattner49f28ca2009-03-05 08:00:35 +00001428 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1429 PP.getSourceManager(),
1430 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001431
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001432 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001433 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001434 if (ObjCImpDecl) {
1435 Diag(Tok, diag::warn_semicolon_before_method_body)
1436 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
1437 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001438 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001439 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001440
Steve Naroff409be832007-11-11 19:54:21 +00001441 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001442 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001443 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001444
Steve Naroff409be832007-11-11 19:54:21 +00001445 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1446 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001447
Steve Naroff409be832007-11-11 19:54:21 +00001448 // If we didn't find the '{', bail out.
1449 if (Tok.isNot(tok::l_brace))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001450 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001451 }
Steve Naroff409be832007-11-11 19:54:21 +00001452 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001453
Steve Naroff409be832007-11-11 19:54:21 +00001454 // Enter a scope for the method body.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001455 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Steve Naroff409be832007-11-11 19:54:21 +00001457 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001458 // specified Declarator for the method.
Steve Naroffebf64432009-02-28 16:59:13 +00001459 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001460
1461 OwningStmtResult FnBody(ParseCompoundStatementBody());
1462
Steve Naroff409be832007-11-11 19:54:21 +00001463 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001464 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001465 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1466 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001467
Steve Naroff32ce8372009-03-02 22:00:56 +00001468 // TODO: Pass argument information.
1469 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump1eb44332009-09-09 15:08:12 +00001470
Steve Naroff409be832007-11-11 19:54:21 +00001471 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001472 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001473
Steve Naroff71c0a952007-11-13 23:01:27 +00001474 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001475}
Anders Carlsson55085182007-08-21 17:43:55 +00001476
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001477Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroff64515f32008-02-05 21:27:35 +00001478 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001479 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001480 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1481 return ParseObjCThrowStmt(AtLoc);
1482 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1483 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001484 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001485 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001486 // If the expression is invalid, skip ahead to the next semicolon. Not
1487 // doing this opens us up to the possibility of infinite loops if
1488 // ParseExpression does not consume any tokens.
1489 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001490 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001491 }
1492 // Otherwise, eat the semicolon.
1493 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson6b1d2832009-05-17 21:11:30 +00001494 return Actions.ActOnExprStmt(Actions.FullExpr(Res));
Steve Naroff64515f32008-02-05 21:27:35 +00001495}
1496
Sebastian Redl1d922962008-12-13 15:32:12 +00001497Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001498 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001499 case tok::string_literal: // primary-expression: string-literal
1500 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001501 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001502 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001503 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001504 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001505
Chris Lattner4fef81d2008-08-05 06:19:09 +00001506 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1507 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001508 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001509 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001510 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001511 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001512 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001513 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001514 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001515 }
Anders Carlsson55085182007-08-21 17:43:55 +00001516 }
Anders Carlsson55085182007-08-21 17:43:55 +00001517}
1518
Mike Stump1eb44332009-09-09 15:08:12 +00001519/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001520/// '[' objc-receiver objc-message-args ']'
1521///
1522/// objc-receiver:
1523/// expression
1524/// class-name
1525/// type-name
Sebastian Redl1d922962008-12-13 15:32:12 +00001526Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001527 assert(Tok.is(tok::l_square) && "'[' expected");
1528 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1529
1530 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001531 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001532 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahaniand2869922009-04-08 19:50:10 +00001533 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1534 SourceLocation NameLoc = ConsumeToken();
1535 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1536 ExprArg(Actions));
1537 }
Chris Lattner699b6612008-01-25 18:59:06 +00001538 }
1539
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001540 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001541 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001542 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001543 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001544 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001545
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001546 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001547 0, move(Res));
Chris Lattner699b6612008-01-25 18:59:06 +00001548}
Sebastian Redl1d922962008-12-13 15:32:12 +00001549
Chris Lattner699b6612008-01-25 18:59:06 +00001550/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1551/// the rest of a message expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001552///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001553/// objc-message-args:
1554/// objc-selector
1555/// objc-keywordarg-list
1556///
1557/// objc-keywordarg-list:
1558/// objc-keywordarg
1559/// objc-keywordarg-list objc-keywordarg
1560///
Mike Stump1eb44332009-09-09 15:08:12 +00001561/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001562/// selector-name[opt] ':' objc-keywordexpr
1563///
1564/// objc-keywordexpr:
1565/// nonempty-expr-list
1566///
1567/// nonempty-expr-list:
1568/// assignment-expression
1569/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001570///
1571Parser::OwningExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001572Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +00001573 SourceLocation NameLoc,
Chris Lattner699b6612008-01-25 18:59:06 +00001574 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +00001575 ExprArg ReceiverExpr) {
Steve Naroffc4df6d22009-11-07 02:08:14 +00001576 if (Tok.is(tok::code_completion)) {
1577 if (ReceiverName)
Douglas Gregor60b01cc2009-11-17 23:31:36 +00001578 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc);
Steve Naroffc4df6d22009-11-07 02:08:14 +00001579 else
Douglas Gregor60b01cc2009-11-17 23:31:36 +00001580 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get());
Steve Naroffc4df6d22009-11-07 02:08:14 +00001581 ConsumeToken();
1582 }
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001583 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001584 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001585 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001586
Anders Carlssonff975cf2009-02-14 18:21:46 +00001587 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00001588
Steve Naroff68d331a2007-09-27 14:38:14 +00001589 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001590 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00001591
Chris Lattnerdf195262007-10-09 17:51:17 +00001592 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001593 while (1) {
1594 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001595 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001596
Chris Lattnerdf195262007-10-09 17:51:17 +00001597 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001598 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001599 // We must manually skip to a ']', otherwise the expression skipper will
1600 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1601 // the enclosing expression.
1602 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001603 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001604 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001605
Steve Naroff68d331a2007-09-27 14:38:14 +00001606 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001607 /// Parse the expression after ':'
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001608 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001609 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001610 // We must manually skip to a ']', otherwise the expression skipper will
1611 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1612 // the enclosing expression.
1613 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001614 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00001615 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001616
Steve Naroff37387c92007-09-17 20:25:27 +00001617 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001618 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00001619
Steve Naroff37387c92007-09-17 20:25:27 +00001620 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00001621 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001622 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001623 break;
1624 // We have a selector or a colon, continue parsing.
1625 }
1626 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001627 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001628 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00001629 /// Parse the expression after ','
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001630 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001631 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001632 // We must manually skip to a ']', otherwise the expression skipper will
1633 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1634 // the enclosing expression.
1635 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001636 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00001637 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001638
Steve Naroff49f109c2007-11-15 13:05:42 +00001639 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001640 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001641 }
1642 } else if (!selIdent) {
1643 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00001644
Chris Lattner4fef81d2008-08-05 06:19:09 +00001645 // We must manually skip to a ']', otherwise the expression skipper will
1646 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1647 // the enclosing expression.
1648 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001649 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001650 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001651
Chris Lattnerdf195262007-10-09 17:51:17 +00001652 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001653 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001654 // We must manually skip to a ']', otherwise the expression skipper will
1655 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1656 // the enclosing expression.
1657 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001658 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001659 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001660
Chris Lattner699b6612008-01-25 18:59:06 +00001661 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00001662
Steve Naroff29238a02007-10-05 18:42:47 +00001663 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001664 if (nKeys == 0)
1665 KeyIdents.push_back(selIdent);
1666 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001667
Chris Lattnerff384912007-10-07 02:00:24 +00001668 // We've just parsed a keyword message.
Sebastian Redl1d922962008-12-13 15:32:12 +00001669 if (ReceiverName)
1670 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump1eb44332009-09-09 15:08:12 +00001671 LBracLoc, NameLoc, SelectorLoc,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001672 RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001673 KeyExprs.take(), KeyExprs.size()));
1674 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001675 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001676 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001677}
1678
Sebastian Redl1d922962008-12-13 15:32:12 +00001679Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl20df9b72008-12-11 22:51:44 +00001680 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00001681 if (Res.isInvalid()) return move(Res);
1682
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001683 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1684 // expressions. At this point, we know that the only valid thing that starts
1685 // with '@' is an @"".
1686 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001687 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001688 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00001689 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001690
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001691 while (Tok.is(tok::at)) {
1692 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001693
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001694 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001695 if (!isTokenStringLiteral())
1696 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001697
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001698 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001699 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00001700 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001701
Sebastian Redleffa8d12008-12-10 00:02:53 +00001702 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001703 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001704
1705 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1706 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00001707}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001708
1709/// objc-encode-expression:
1710/// @encode ( type-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001711Parser::OwningExprResult
1712Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001713 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00001714
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001715 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001716
Chris Lattner4fef81d2008-08-05 06:19:09 +00001717 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001718 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1719
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001720 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001721
Douglas Gregor809070a2009-02-18 17:45:20 +00001722 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00001723
Anders Carlsson4988ae32007-08-23 15:31:37 +00001724 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001725
Douglas Gregor809070a2009-02-18 17:45:20 +00001726 if (Ty.isInvalid())
1727 return ExprError();
1728
Mike Stump1eb44332009-09-09 15:08:12 +00001729 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00001730 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001731}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001732
1733/// objc-protocol-expression
1734/// @protocol ( protocol-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001735Parser::OwningExprResult
1736Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001737 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001738
Chris Lattner4fef81d2008-08-05 06:19:09 +00001739 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001740 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1741
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001742 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001743
Chris Lattner4fef81d2008-08-05 06:19:09 +00001744 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00001745 return ExprError(Diag(Tok, diag::err_expected_ident));
1746
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001747 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001748 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001749
Anders Carlsson4988ae32007-08-23 15:31:37 +00001750 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001751
Sebastian Redl1d922962008-12-13 15:32:12 +00001752 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1753 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001754}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001755
1756/// objc-selector-expression
1757/// @selector '(' objc-keyword-selector ')'
Sebastian Redl1d922962008-12-13 15:32:12 +00001758Parser::OwningExprResult
1759Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001760 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001761
Chris Lattner4fef81d2008-08-05 06:19:09 +00001762 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001763 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1764
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001765 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001766 SourceLocation LParenLoc = ConsumeParen();
1767 SourceLocation sLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001768 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001769 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1770 return ExprError(Diag(Tok, diag::err_expected_ident));
1771
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001772 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001773 unsigned nColons = 0;
1774 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001775 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001776 if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00001777 return ExprError(Diag(Tok, diag::err_expected_colon));
1778
Chris Lattnercb53b362007-12-27 19:57:00 +00001779 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001780 ConsumeToken(); // Eat the ':'.
1781 if (Tok.is(tok::r_paren))
1782 break;
1783 // Check for another keyword selector.
1784 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001785 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001786 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001787 if (!SelIdent && Tok.isNot(tok::colon))
1788 break;
1789 }
Steve Naroff887407e2007-12-05 22:21:29 +00001790 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001791 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001792 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001793 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1794 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00001795 }