blob: f0b1e9f3efb999ef40ff66483b88767fc5415fa8 [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) {
837 if (Tok.isNot(tok::identifier)) {
838 Diag(Tok, diag::err_expected_ident);
839 SkipUntil(tok::greater);
840 return true;
841 }
842 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
843 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000844 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +0000845 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Chris Lattnere13b9592008-07-26 04:03:38 +0000847 if (Tok.isNot(tok::comma))
848 break;
849 ConsumeToken();
850 }
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Chris Lattnere13b9592008-07-26 04:03:38 +0000852 // Consume the '>'.
853 if (Tok.isNot(tok::greater)) {
854 Diag(Tok, diag::err_expected_greater);
855 return true;
856 }
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Chris Lattnere13b9592008-07-26 04:03:38 +0000858 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Chris Lattnere13b9592008-07-26 04:03:38 +0000860 // Convert the list of protocols identifiers into a list of protocol decls.
861 Actions.FindProtocolDeclaration(WarnOnDeclarations,
862 &ProtocolIdents[0], ProtocolIdents.size(),
863 Protocols);
864 return false;
865}
866
Steve Naroffdac269b2007-08-20 21:31:48 +0000867/// objc-class-instance-variables:
868/// '{' objc-instance-variable-decl-list[opt] '}'
869///
870/// objc-instance-variable-decl-list:
871/// objc-visibility-spec
872/// objc-instance-variable-decl ';'
873/// ';'
874/// objc-instance-variable-decl-list objc-visibility-spec
875/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
876/// objc-instance-variable-decl-list ';'
877///
878/// objc-visibility-spec:
879/// @private
880/// @protected
881/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000882/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000883///
884/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +0000885/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +0000886///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000887void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff60fccee2007-10-29 21:38:07 +0000888 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000889 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattnerb28317a2009-03-28 19:18:32 +0000890 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000891
Douglas Gregor1a0d31a2009-01-12 18:45:55 +0000892 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +0000893
Steve Naroffddbff782007-08-21 21:17:12 +0000894 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000896 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000897 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000898 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000899 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Steve Naroffddbff782007-08-21 21:17:12 +0000901 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000902 if (Tok.is(tok::semi)) {
Chris Lattnerc2253f52009-11-06 06:40:12 +0000903 Diag(Tok, diag::ext_extra_struct_semi)
904 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Steve Naroffddbff782007-08-21 21:17:12 +0000905 ConsumeToken();
906 continue;
907 }
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Steve Naroffddbff782007-08-21 21:17:12 +0000909 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000910 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000911 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000912 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000913 case tok::objc_private:
914 case tok::objc_public:
915 case tok::objc_protected:
916 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000917 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000918 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000919 continue;
Steve Naroffddbff782007-08-21 21:17:12 +0000920 default:
921 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000922 continue;
923 }
924 }
Mike Stump1eb44332009-09-09 15:08:12 +0000925
John McCallbdd563e2009-11-03 02:38:08 +0000926 struct ObjCIvarCallback : FieldCallback {
927 Parser &P;
928 DeclPtrTy IDecl;
929 tok::ObjCKeywordKind visibility;
930 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
931
932 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
933 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
934 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
935 }
936
937 DeclPtrTy invoke(FieldDeclarator &FD) {
938 // Install the declarator into the interface decl.
939 DeclPtrTy Field
940 = P.Actions.ActOnIvar(P.CurScope,
941 FD.D.getDeclSpec().getSourceRange().getBegin(),
942 IDecl, FD.D, FD.BitfieldSize, visibility);
943 AllIvarDecls.push_back(Field);
944 return Field;
945 }
946 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
947
Chris Lattnere1359422008-04-10 06:46:29 +0000948 // Parse all the comma separated declarators.
949 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +0000950 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Chris Lattnerdf195262007-10-09 17:51:17 +0000952 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000953 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +0000954 } else {
955 Diag(Tok, diag::err_expected_semi_decl_list);
956 // Skip to end of block or statement
957 SkipUntil(tok::r_brace, true, true);
958 }
959 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000960 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000961 // Call ActOnFields() even if we don't have any decls. This is useful
962 // for code rewriting tools that need to be aware of the empty list.
963 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000964 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +0000965 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +0000966 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000967}
Steve Naroffdac269b2007-08-20 21:31:48 +0000968
969/// objc-protocol-declaration:
970/// objc-protocol-definition
971/// objc-protocol-forward-reference
972///
973/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +0000974/// @protocol identifier
975/// objc-protocol-refs[opt]
976/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000977/// @end
978///
979/// objc-protocol-forward-reference:
980/// @protocol identifier-list ';'
981///
982/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000983/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000984/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000985Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
986 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000987 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000988 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
989 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Chris Lattnerdf195262007-10-09 17:51:17 +0000991 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000992 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000993 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000994 }
995 // Save the protocol name, then consume it.
996 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
997 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Chris Lattnerdf195262007-10-09 17:51:17 +0000999 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001000 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001001 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001002 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001003 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001004 }
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Chris Lattnerdf195262007-10-09 17:51:17 +00001006 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001007 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1008 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1009
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001010 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001011 while (1) {
1012 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001013 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001014 Diag(Tok, diag::err_expected_ident);
1015 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001016 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001017 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001018 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1019 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001020 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Chris Lattnerdf195262007-10-09 17:51:17 +00001022 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001023 break;
1024 }
1025 // Consume the ';'.
1026 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001027 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Steve Naroffe440eb82007-10-10 17:32:04 +00001029 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001030 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001031 ProtocolRefs.size(),
1032 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +00001033 }
Mike Stump1eb44332009-09-09 15:08:12 +00001034
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001035 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001036 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001037
Chris Lattnerb28317a2009-03-28 19:18:32 +00001038 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001039 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001040 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001041 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1042 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001043 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Chris Lattnerb28317a2009-03-28 19:18:32 +00001045 DeclPtrTy ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001046 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001047 ProtocolRefs.data(),
1048 ProtocolRefs.size(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +00001049 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001050 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001051 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001052}
Steve Naroffdac269b2007-08-20 21:31:48 +00001053
1054/// objc-implementation:
1055/// objc-class-implementation-prologue
1056/// objc-category-implementation-prologue
1057///
1058/// objc-class-implementation-prologue:
1059/// @implementation identifier objc-superclass[opt]
1060/// objc-class-instance-variables[opt]
1061///
1062/// objc-category-implementation-prologue:
1063/// @implementation identifier ( identifier )
Chris Lattnerb28317a2009-03-28 19:18:32 +00001064Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001065 SourceLocation atLoc) {
1066 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1067 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1068 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001069
Chris Lattnerdf195262007-10-09 17:51:17 +00001070 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001071 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001072 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001073 }
1074 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001075 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001076 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001077
1078 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001079 // we have a category implementation.
1080 SourceLocation lparenLoc = ConsumeParen();
1081 SourceLocation categoryLoc, rparenLoc;
1082 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001083
Chris Lattnerdf195262007-10-09 17:51:17 +00001084 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001085 categoryId = Tok.getIdentifierInfo();
1086 categoryLoc = ConsumeToken();
1087 } else {
1088 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001089 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001090 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001091 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001092 Diag(Tok, diag::err_expected_rparen);
1093 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001094 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001095 }
1096 rparenLoc = ConsumeParen();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001097 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001098 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001099 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001100 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001101 PendingObjCImpDecl.push_back(ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001102 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001103 }
1104 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001105 SourceLocation superClassLoc;
1106 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001107 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001108 // We have a super class
1109 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001110 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001111 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001112 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001113 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001114 superClassId = Tok.getIdentifierInfo();
1115 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001116 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001117 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001118 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001119 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Steve Naroff60fccee2007-10-29 21:38:07 +00001121 if (Tok.is(tok::l_brace)) // we have ivars
1122 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001123 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001124 PendingObjCImpDecl.push_back(ObjCImpDecl);
1125
Chris Lattnerb28317a2009-03-28 19:18:32 +00001126 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001127}
Steve Naroff60fccee2007-10-29 21:38:07 +00001128
Chris Lattnerb28317a2009-03-28 19:18:32 +00001129Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001130 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1131 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001132 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001133 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001134 if (ObjCImpDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001135 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001136 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001137 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001138 }
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001139 else
1140 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001141 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001142}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001143
Fariborz Jahanian1ac71042009-11-17 17:15:16 +00001144Parser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001145 if (PendingObjCImpDecl.empty())
1146 return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
1147 DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
1148 Actions.ActOnAtEnd(SourceLocation(), ImpDecl);
1149 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1150}
1151
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001152/// compatibility-alias-decl:
1153/// @compatibility_alias alias-name class-name ';'
1154///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001155Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001156 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1157 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1158 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001159 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001160 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001161 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001162 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001163 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1164 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
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 *classId = Tok.getIdentifierInfo();
1170 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1171 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001172 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001173 return DeclPtrTy();
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001174 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001175 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1176 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001177}
1178
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001179/// property-synthesis:
1180/// @synthesize property-ivar-list ';'
1181///
1182/// property-ivar-list:
1183/// property-ivar
1184/// property-ivar-list ',' property-ivar
1185///
1186/// property-ivar:
1187/// identifier
1188/// identifier '=' identifier
1189///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001190Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001191 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1192 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001193 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnerdf195262007-10-09 17:51:17 +00001194 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001195 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001196 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001197 }
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Chris Lattnerdf195262007-10-09 17:51:17 +00001199 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001200 IdentifierInfo *propertyIvar = 0;
1201 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1202 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001203 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001204 // property '=' ivar-name
1205 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +00001206 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001207 Diag(Tok, diag::err_expected_ident);
1208 break;
1209 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001210 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001211 ConsumeToken(); // consume ivar-name
1212 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001213 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1214 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001215 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001216 break;
1217 ConsumeToken(); // consume ','
1218 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001219 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001220 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Fariborz Jahaniand3fdcb52009-11-06 21:48:47 +00001221 else
1222 ConsumeToken(); // consume ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001223 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001224}
1225
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001226/// property-dynamic:
1227/// @dynamic property-list
1228///
1229/// property-list:
1230/// identifier
1231/// property-list ',' identifier
1232///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001233Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001234 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1235 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1236 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001237 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001238 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001239 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001240 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001241 while (Tok.is(tok::identifier)) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001242 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1243 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1244 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1245 propertyId, 0);
1246
Chris Lattnerdf195262007-10-09 17:51:17 +00001247 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001248 break;
1249 ConsumeToken(); // consume ','
1250 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001251 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001252 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001253 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001254}
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001256/// objc-throw-statement:
1257/// throw expression[opt];
1258///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001259Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001260 OwningExprResult Res(Actions);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001261 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001262 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001263 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001264 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001265 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001266 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001267 }
1268 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001269 ConsumeToken(); // consume ';'
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001270 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001271}
1272
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001273/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001274/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001275///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001276Parser::OwningStmtResult
1277Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001278 ConsumeToken(); // consume synchronized
1279 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001280 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001281 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001282 }
1283 ConsumeParen(); // '('
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001284 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001285 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001286 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001287 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001288 }
1289 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001290 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001291 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001292 }
1293 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001294 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001295 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001296 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001297 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001298 // Enter a scope to hold everything within the compound stmt. Compound
1299 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001300 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001301
Sebastian Redl61364dd2008-12-11 19:30:53 +00001302 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001303
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001304 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001305 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001306 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001307 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001308}
1309
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001310/// objc-try-catch-statement:
1311/// @try compound-statement objc-catch-list[opt]
1312/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1313///
1314/// objc-catch-list:
1315/// @catch ( parameter-declaration ) compound-statement
1316/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1317/// catch-parameter-declaration:
1318/// parameter-declaration
1319/// '...' [OBJC2]
1320///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001321Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001322 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001323
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001324 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001325 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001326 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001327 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001328 }
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001329 OwningStmtResult CatchStmts(Actions);
1330 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001331 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001332 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001333 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001334 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001335 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001336
Chris Lattnerdf195262007-10-09 17:51:17 +00001337 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001338 // At this point, we need to lookahead to determine if this @ is the start
1339 // of an @catch or @finally. We don't want to consume the @ token if this
1340 // is an @try or @encode or something else.
1341 Token AfterAt = GetLookAheadToken(1);
1342 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1343 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1344 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001345
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001346 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001347 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001348 DeclPtrTy FirstPart;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001349 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001350 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001351 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001352 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001353 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001354 DeclSpec DS;
1355 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001356 // For some odd reason, the name of the exception variable is
Mike Stump1eb44332009-09-09 15:08:12 +00001357 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff7ba138a2009-03-03 19:52:17 +00001358 // we must accept either 'declarator' or 'abstract-declarator' here.
1359 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1360 ParseDeclarator(ParmDecl);
1361
1362 // Inform the actions module about the parameter declarator, so it
1363 // gets added to the current scope.
1364 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001365 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001366 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001367
Steve Naroff93a25952009-04-07 22:56:58 +00001368 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001369
Steve Naroff93a25952009-04-07 22:56:58 +00001370 if (Tok.is(tok::r_paren))
1371 RParenLoc = ConsumeParen();
1372 else // Skip over garbage, until we get to ')'. Eat the ')'.
1373 SkipUntil(tok::r_paren, true, false);
1374
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001375 OwningStmtResult CatchBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001376 if (Tok.is(tok::l_brace))
1377 CatchBody = ParseCompoundStatementBody();
1378 else
1379 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001380 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001381 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001382 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff7ba138a2009-03-03 19:52:17 +00001383 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001384 move(CatchStmts));
Steve Naroff64515f32008-02-05 21:27:35 +00001385 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001386 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1387 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001388 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001389 }
1390 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001391 } else {
1392 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001393 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001394 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001395
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001396 OwningStmtResult FinallyBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001397 if (Tok.is(tok::l_brace))
1398 FinallyBody = ParseCompoundStatementBody();
1399 else
1400 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001401 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001402 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001403 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001404 move(FinallyBody));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001405 catch_or_finally_seen = true;
1406 break;
1407 }
1408 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001409 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001410 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001411 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001412 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001413 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1414 move(FinallyStmt));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001415}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001416
Steve Naroff3536b442007-09-06 21:24:23 +00001417/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001418///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001419Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1420 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Chris Lattner49f28ca2009-03-05 08:00:35 +00001422 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1423 PP.getSourceManager(),
1424 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001425
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001426 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001427 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001428 if (ObjCImpDecl) {
1429 Diag(Tok, diag::warn_semicolon_before_method_body)
1430 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
1431 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001432 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001433 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001434
Steve Naroff409be832007-11-11 19:54:21 +00001435 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001436 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001437 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001438
Steve Naroff409be832007-11-11 19:54:21 +00001439 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1440 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001441
Steve Naroff409be832007-11-11 19:54:21 +00001442 // If we didn't find the '{', bail out.
1443 if (Tok.isNot(tok::l_brace))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001444 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001445 }
Steve Naroff409be832007-11-11 19:54:21 +00001446 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001447
Steve Naroff409be832007-11-11 19:54:21 +00001448 // Enter a scope for the method body.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001449 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001450
Steve Naroff409be832007-11-11 19:54:21 +00001451 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001452 // specified Declarator for the method.
Steve Naroffebf64432009-02-28 16:59:13 +00001453 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001454
1455 OwningStmtResult FnBody(ParseCompoundStatementBody());
1456
Steve Naroff409be832007-11-11 19:54:21 +00001457 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001458 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001459 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1460 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001461
Steve Naroff32ce8372009-03-02 22:00:56 +00001462 // TODO: Pass argument information.
1463 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Steve Naroff409be832007-11-11 19:54:21 +00001465 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001466 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001467
Steve Naroff71c0a952007-11-13 23:01:27 +00001468 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001469}
Anders Carlsson55085182007-08-21 17:43:55 +00001470
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001471Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroff64515f32008-02-05 21:27:35 +00001472 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001473 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001474 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1475 return ParseObjCThrowStmt(AtLoc);
1476 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1477 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001478 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001479 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001480 // If the expression is invalid, skip ahead to the next semicolon. Not
1481 // doing this opens us up to the possibility of infinite loops if
1482 // ParseExpression does not consume any tokens.
1483 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001484 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001485 }
1486 // Otherwise, eat the semicolon.
1487 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson6b1d2832009-05-17 21:11:30 +00001488 return Actions.ActOnExprStmt(Actions.FullExpr(Res));
Steve Naroff64515f32008-02-05 21:27:35 +00001489}
1490
Sebastian Redl1d922962008-12-13 15:32:12 +00001491Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001492 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001493 case tok::string_literal: // primary-expression: string-literal
1494 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001495 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001496 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001497 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001498 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001499
Chris Lattner4fef81d2008-08-05 06:19:09 +00001500 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1501 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001502 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001503 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001504 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001505 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001506 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001507 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001508 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001509 }
Anders Carlsson55085182007-08-21 17:43:55 +00001510 }
Anders Carlsson55085182007-08-21 17:43:55 +00001511}
1512
Mike Stump1eb44332009-09-09 15:08:12 +00001513/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001514/// '[' objc-receiver objc-message-args ']'
1515///
1516/// objc-receiver:
1517/// expression
1518/// class-name
1519/// type-name
Sebastian Redl1d922962008-12-13 15:32:12 +00001520Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001521 assert(Tok.is(tok::l_square) && "'[' expected");
1522 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1523
1524 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001525 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001526 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahaniand2869922009-04-08 19:50:10 +00001527 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1528 SourceLocation NameLoc = ConsumeToken();
1529 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1530 ExprArg(Actions));
1531 }
Chris Lattner699b6612008-01-25 18:59:06 +00001532 }
1533
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001534 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001535 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001536 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001537 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001538 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001539
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001540 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001541 0, move(Res));
Chris Lattner699b6612008-01-25 18:59:06 +00001542}
Sebastian Redl1d922962008-12-13 15:32:12 +00001543
Chris Lattner699b6612008-01-25 18:59:06 +00001544/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1545/// the rest of a message expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001546///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001547/// objc-message-args:
1548/// objc-selector
1549/// objc-keywordarg-list
1550///
1551/// objc-keywordarg-list:
1552/// objc-keywordarg
1553/// objc-keywordarg-list objc-keywordarg
1554///
Mike Stump1eb44332009-09-09 15:08:12 +00001555/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001556/// selector-name[opt] ':' objc-keywordexpr
1557///
1558/// objc-keywordexpr:
1559/// nonempty-expr-list
1560///
1561/// nonempty-expr-list:
1562/// assignment-expression
1563/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001564///
1565Parser::OwningExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001566Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +00001567 SourceLocation NameLoc,
Chris Lattner699b6612008-01-25 18:59:06 +00001568 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +00001569 ExprArg ReceiverExpr) {
Steve Naroffc4df6d22009-11-07 02:08:14 +00001570 if (Tok.is(tok::code_completion)) {
1571 if (ReceiverName)
Douglas Gregor60b01cc2009-11-17 23:31:36 +00001572 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc);
Steve Naroffc4df6d22009-11-07 02:08:14 +00001573 else
Douglas Gregor60b01cc2009-11-17 23:31:36 +00001574 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get());
Steve Naroffc4df6d22009-11-07 02:08:14 +00001575 ConsumeToken();
1576 }
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001577 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001578 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001579 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001580
Anders Carlssonff975cf2009-02-14 18:21:46 +00001581 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00001582
Steve Naroff68d331a2007-09-27 14:38:14 +00001583 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001584 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00001585
Chris Lattnerdf195262007-10-09 17:51:17 +00001586 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001587 while (1) {
1588 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001589 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001590
Chris Lattnerdf195262007-10-09 17:51:17 +00001591 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001592 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001593 // We must manually skip to a ']', otherwise the expression skipper will
1594 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1595 // the enclosing expression.
1596 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001597 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001598 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001599
Steve Naroff68d331a2007-09-27 14:38:14 +00001600 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001601 /// Parse the expression after ':'
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001602 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001603 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001604 // We must manually skip to a ']', otherwise the expression skipper will
1605 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1606 // the enclosing expression.
1607 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001608 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00001609 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001610
Steve Naroff37387c92007-09-17 20:25:27 +00001611 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001612 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00001613
Steve Naroff37387c92007-09-17 20:25:27 +00001614 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00001615 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001616 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001617 break;
1618 // We have a selector or a colon, continue parsing.
1619 }
1620 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001621 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001622 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00001623 /// Parse the expression after ','
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001624 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001625 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001626 // We must manually skip to a ']', otherwise the expression skipper will
1627 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1628 // the enclosing expression.
1629 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001630 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00001631 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001632
Steve Naroff49f109c2007-11-15 13:05:42 +00001633 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001634 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001635 }
1636 } else if (!selIdent) {
1637 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00001638
Chris Lattner4fef81d2008-08-05 06:19:09 +00001639 // We must manually skip to a ']', otherwise the expression skipper will
1640 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1641 // the enclosing expression.
1642 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001643 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001644 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001645
Chris Lattnerdf195262007-10-09 17:51:17 +00001646 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001647 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001648 // We must manually skip to a ']', otherwise the expression skipper will
1649 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1650 // the enclosing expression.
1651 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001652 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001653 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001654
Chris Lattner699b6612008-01-25 18:59:06 +00001655 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00001656
Steve Naroff29238a02007-10-05 18:42:47 +00001657 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001658 if (nKeys == 0)
1659 KeyIdents.push_back(selIdent);
1660 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001661
Chris Lattnerff384912007-10-07 02:00:24 +00001662 // We've just parsed a keyword message.
Sebastian Redl1d922962008-12-13 15:32:12 +00001663 if (ReceiverName)
1664 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump1eb44332009-09-09 15:08:12 +00001665 LBracLoc, NameLoc, SelectorLoc,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001666 RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001667 KeyExprs.take(), KeyExprs.size()));
1668 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001669 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001670 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001671}
1672
Sebastian Redl1d922962008-12-13 15:32:12 +00001673Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl20df9b72008-12-11 22:51:44 +00001674 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00001675 if (Res.isInvalid()) return move(Res);
1676
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001677 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1678 // expressions. At this point, we know that the only valid thing that starts
1679 // with '@' is an @"".
1680 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001681 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001682 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00001683 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001684
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001685 while (Tok.is(tok::at)) {
1686 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001687
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001688 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001689 if (!isTokenStringLiteral())
1690 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001691
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001692 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001693 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00001694 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001695
Sebastian Redleffa8d12008-12-10 00:02:53 +00001696 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001697 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001698
1699 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1700 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00001701}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001702
1703/// objc-encode-expression:
1704/// @encode ( type-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001705Parser::OwningExprResult
1706Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001707 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00001708
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001709 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001710
Chris Lattner4fef81d2008-08-05 06:19:09 +00001711 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001712 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1713
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001714 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001715
Douglas Gregor809070a2009-02-18 17:45:20 +00001716 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00001717
Anders Carlsson4988ae32007-08-23 15:31:37 +00001718 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001719
Douglas Gregor809070a2009-02-18 17:45:20 +00001720 if (Ty.isInvalid())
1721 return ExprError();
1722
Mike Stump1eb44332009-09-09 15:08:12 +00001723 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00001724 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001725}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001726
1727/// objc-protocol-expression
1728/// @protocol ( protocol-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001729Parser::OwningExprResult
1730Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001731 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001732
Chris Lattner4fef81d2008-08-05 06:19:09 +00001733 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001734 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1735
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001736 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001737
Chris Lattner4fef81d2008-08-05 06:19:09 +00001738 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00001739 return ExprError(Diag(Tok, diag::err_expected_ident));
1740
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001741 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001742 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001743
Anders Carlsson4988ae32007-08-23 15:31:37 +00001744 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001745
Sebastian Redl1d922962008-12-13 15:32:12 +00001746 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1747 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001748}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001749
1750/// objc-selector-expression
1751/// @selector '(' objc-keyword-selector ')'
Sebastian Redl1d922962008-12-13 15:32:12 +00001752Parser::OwningExprResult
1753Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001754 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001755
Chris Lattner4fef81d2008-08-05 06:19:09 +00001756 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001757 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1758
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001759 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001760 SourceLocation LParenLoc = ConsumeParen();
1761 SourceLocation sLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001762 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001763 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1764 return ExprError(Diag(Tok, diag::err_expected_ident));
1765
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001766 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001767 unsigned nColons = 0;
1768 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001769 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001770 if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00001771 return ExprError(Diag(Tok, diag::err_expected_colon));
1772
Chris Lattnercb53b362007-12-27 19:57:00 +00001773 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001774 ConsumeToken(); // Eat the ':'.
1775 if (Tok.is(tok::r_paren))
1776 break;
1777 // Check for another keyword selector.
1778 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001779 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001780 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001781 if (!SelIdent && Tok.isNot(tok::colon))
1782 break;
1783 }
Steve Naroff887407e2007-12-05 22:21:29 +00001784 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001785 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001786 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001787 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1788 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00001789 }