blob: 1d29f319c58465f4b516a12c33620d401f75dac1 [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;
Mike Stump1eb44332009-09-09 15:08:12 +000064
Reid Spencer5f016e22007-07-11 17:01:13 +000065 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000066 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000067 Diag(Tok, diag::err_expected_ident);
68 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +000069 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +000070 }
Reid Spencer5f016e22007-07-11 17:01:13 +000071 ClassNames.push_back(Tok.getIdentifierInfo());
72 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +000073
Chris Lattnerdf195262007-10-09 17:51:17 +000074 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000075 break;
Mike Stump1eb44332009-09-09 15:08:12 +000076
Reid Spencer5f016e22007-07-11 17:01:13 +000077 ConsumeToken();
78 }
Mike Stump1eb44332009-09-09 15:08:12 +000079
Reid Spencer5f016e22007-07-11 17:01:13 +000080 // Consume the ';'.
81 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Chris Lattnerb28317a2009-03-28 19:18:32 +000082 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +000083
Steve Naroffe440eb82007-10-10 17:32:04 +000084 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff3536b442007-09-06 21:24:23 +000085 &ClassNames[0], ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000086}
87
Steve Naroffdac269b2007-08-20 21:31:48 +000088///
89/// objc-interface:
90/// objc-class-interface-attributes[opt] objc-class-interface
91/// objc-category-interface
92///
93/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +000094/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +000095/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +000096/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +000097/// objc-interface-decl-list
98/// @end
99///
100/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000101/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000102/// objc-protocol-refs[opt]
103/// objc-interface-decl-list
104/// @end
105///
106/// objc-superclass:
107/// ':' identifier
108///
109/// objc-class-interface-attributes:
110/// __attribute__((visibility("default")))
111/// __attribute__((visibility("hidden")))
112/// __attribute__((deprecated))
113/// __attribute__((unavailable))
114/// __attribute__((objc_exception)) - used by NSException on 64-bit
115///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000116Parser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration(
Steve Naroffdac269b2007-08-20 21:31:48 +0000117 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000118 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000119 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
120 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Chris Lattnerdf195262007-10-09 17:51:17 +0000122 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000123 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000124 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000125 }
126 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000127 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000128 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Chris Lattnerdf195262007-10-09 17:51:17 +0000130 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000131 SourceLocation lparenLoc = ConsumeParen();
132 SourceLocation categoryLoc, rparenLoc;
133 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Steve Naroff527fe232007-08-23 19:56:30 +0000135 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000136 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000137 categoryId = Tok.getIdentifierInfo();
138 categoryLoc = ConsumeToken();
Steve Naroff527fe232007-08-23 19:56:30 +0000139 } else if (!getLang().ObjC2) {
140 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000141 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000142 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000143 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000144 Diag(Tok, diag::err_expected_rparen);
145 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +0000146 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000147 }
148 rparenLoc = ConsumeParen();
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Steve Naroffdac269b2007-08-20 21:31:48 +0000150 // Next, we need to check for any protocol references.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000151 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000152 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000153 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000154 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000155 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
156 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000157 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Steve Naroffdac269b2007-08-20 21:31:48 +0000159 if (attrList) // categories don't support attributes.
160 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Jay Foadbeaaccd2009-05-21 09:52:38 +0000162 DeclPtrTy CategoryType =
Mike Stump1eb44332009-09-09 15:08:12 +0000163 Actions.ActOnStartCategoryInterface(atLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000164 nameId, nameLoc,
165 categoryId, categoryLoc,
166 ProtocolRefs.data(),
167 ProtocolRefs.size(),
168 EndProtoLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000170 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000171 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000172 }
173 // Parse a class interface.
174 IdentifierInfo *superClassId = 0;
175 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000176
Chris Lattnerdf195262007-10-09 17:51:17 +0000177 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000178 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000179 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000180 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000181 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000182 }
183 superClassId = Tok.getIdentifierInfo();
184 superClassLoc = ConsumeToken();
185 }
186 // Next, we need to check for any protocol references.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000187 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000188 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
189 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000190 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000191 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
192 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000193 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000194
195 DeclPtrTy ClsType =
196 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000197 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000198 ProtocolRefs.data(), ProtocolRefs.size(),
Chris Lattner06036d32008-07-26 04:13:19 +0000199 EndProtoLoc, attrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Chris Lattnerdf195262007-10-09 17:51:17 +0000201 if (Tok.is(tok::l_brace))
Steve Naroff60fccee2007-10-29 21:38:07 +0000202 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000203
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000204 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000205 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000206}
207
208/// objc-interface-decl-list:
209/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000210/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000211/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000212/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000213/// objc-interface-decl-list declaration
214/// objc-interface-decl-list ';'
215///
Steve Naroff294494e2007-08-22 16:35:03 +0000216/// objc-method-requirement: [OBJC2]
217/// @required
218/// @optional
219///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000220void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000221 tok::ObjCKeywordKind contextKey) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000222 llvm::SmallVector<DeclPtrTy, 32> allMethods;
223 llvm::SmallVector<DeclPtrTy, 16> allProperties;
Chris Lattner682bf922009-03-29 16:50:03 +0000224 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000225 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Chris Lattnerbc662af2008-10-20 06:10:06 +0000227 SourceLocation AtEndLoc;
228
Steve Naroff294494e2007-08-22 16:35:03 +0000229 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000230 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000231 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000232 DeclPtrTy methodPrototype =
Chris Lattnerdf195262007-10-09 17:51:17 +0000233 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000234 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000235 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
236 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000237 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
238 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000239 continue;
240 }
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Chris Lattnere82a10f2008-10-20 05:46:22 +0000242 // Ignore excess semicolons.
243 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000244 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000245 continue;
246 }
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Chris Lattnerbc662af2008-10-20 06:10:06 +0000248 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000249 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000250 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Chris Lattnere82a10f2008-10-20 05:46:22 +0000252 // If we don't have an @ directive, parse it as a function definition.
253 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000254 // The code below does not consume '}'s because it is afraid of eating the
255 // end of a namespace. Because of the way this code is structured, an
256 // erroneous r_brace would cause an infinite loop if not handled here.
257 if (Tok.is(tok::r_brace))
258 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Steve Naroff4985ace2007-08-22 18:35:33 +0000260 // FIXME: as the name implies, this rule allows function definitions.
261 // We could pass a flag or check for functions during semantic analysis.
Chris Lattner682bf922009-03-29 16:50:03 +0000262 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000263 continue;
264 }
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Chris Lattnere82a10f2008-10-20 05:46:22 +0000266 // Otherwise, we have an @ directive, eat the @.
267 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnera2449b22008-10-20 05:57:40 +0000268 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Chris Lattnera2449b22008-10-20 05:57:40 +0000270 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattnere82a10f2008-10-20 05:46:22 +0000271 AtEndLoc = AtLoc;
272 break;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000273 }
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Chris Lattnerbc662af2008-10-20 06:10:06 +0000275 // Eat the identifier.
276 ConsumeToken();
277
Chris Lattnera2449b22008-10-20 05:57:40 +0000278 switch (DirectiveKind) {
279 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000280 // FIXME: If someone forgets an @end on a protocol, this loop will
281 // continue to eat up tons of stuff and spew lots of nonsense errors. It
282 // would probably be better to bail out if we saw an @class or @interface
283 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000284 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000285 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000286 SkipUntil(tok::r_brace, tok::at);
287 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Chris Lattnera2449b22008-10-20 05:57:40 +0000289 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000290 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000291 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000292 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000293 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000294 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000295 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000296 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000297 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Chris Lattnera2449b22008-10-20 05:57:40 +0000299 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000300 if (!getLang().ObjC2)
301 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
302
Chris Lattnere82a10f2008-10-20 05:46:22 +0000303 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000304 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000305 if (Tok.is(tok::l_paren))
Chris Lattnere82a10f2008-10-20 05:46:22 +0000306 ParseObjCPropertyAttribute(OCDS);
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Chris Lattnere82a10f2008-10-20 05:46:22 +0000308 // Parse all the comma separated declarators.
309 DeclSpec DS;
310 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
311 ParseStructDeclaration(DS, FieldDeclarators);
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000313 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
314 tok::at);
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Chris Lattnere82a10f2008-10-20 05:46:22 +0000316 // Convert them all to property declarations.
317 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
318 FieldDeclarator &FD = FieldDeclarators[i];
Chris Lattnerda3253d2008-10-20 06:33:53 +0000319 if (FD.D.getIdentifier() == 0) {
Chris Lattneref708fd2008-11-18 07:50:21 +0000320 Diag(AtLoc, diag::err_objc_property_requires_field_name)
321 << FD.D.getSourceRange();
Chris Lattnerda3253d2008-10-20 06:33:53 +0000322 continue;
323 }
Fariborz Jahanian573acde2009-01-17 23:21:10 +0000324 if (FD.BitfieldSize) {
325 Diag(AtLoc, diag::err_objc_property_bitfield)
326 << FD.D.getSourceRange();
327 continue;
328 }
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Chris Lattnere82a10f2008-10-20 05:46:22 +0000330 // Install the property declarator into interfaceDecl.
Chris Lattnerda3253d2008-10-20 06:33:53 +0000331 IdentifierInfo *SelName =
332 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +0000333
334 Selector GetterSel =
Chris Lattnerda3253d2008-10-20 06:33:53 +0000335 PP.getSelectorTable().getNullarySelector(SelName);
Chris Lattnere82a10f2008-10-20 05:46:22 +0000336 IdentifierInfo *SetterName = OCDS.getSetterName();
Fariborz Jahanian2e050f12009-03-12 22:34:11 +0000337 Selector SetterSel;
338 if (SetterName)
339 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
340 else
341 SetterSel = SelectorTable::constructSetterName(PP.getIdentifierTable(),
342 PP.getSelectorTable(),
343 FD.D.getIdentifier());
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +0000344 bool isOverridingProperty = false;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000345 DeclPtrTy Property = Actions.ActOnProperty(CurScope, AtLoc, FD, OCDS,
346 GetterSel, SetterSel,
Mike Stump1eb44332009-09-09 15:08:12 +0000347 interfaceDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000348 &isOverridingProperty,
349 MethodImplKind);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +0000350 if (!isOverridingProperty)
351 allProperties.push_back(Property);
Chris Lattnere82a10f2008-10-20 05:46:22 +0000352 }
Chris Lattnera2449b22008-10-20 05:57:40 +0000353 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000354 }
Steve Naroff294494e2007-08-22 16:35:03 +0000355 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000356
357 // We break out of the big loop in two cases: when we see @end or when we see
358 // EOF. In the former case, eat the @end. In the later case, emit an error.
359 if (Tok.isObjCAtKeyword(tok::objc_end))
360 ConsumeToken(); // the "end" identifier
361 else
362 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Chris Lattnera2449b22008-10-20 05:57:40 +0000364 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000365 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8a779312008-06-06 16:45:15 +0000366 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000367 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000368 allProperties.data(), allProperties.size(),
369 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000370}
371
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000372/// Parse property attribute declarations.
373///
374/// property-attr-decl: '(' property-attrlist ')'
375/// property-attrlist:
376/// property-attribute
377/// property-attrlist ',' property-attribute
378/// property-attribute:
379/// getter '=' identifier
380/// setter '=' identifier ':'
381/// readonly
382/// readwrite
383/// assign
384/// retain
385/// copy
386/// nonatomic
387///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000388void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000389 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000390 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000392 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000393 if (Tok.is(tok::code_completion)) {
394 Actions.CodeCompleteObjCProperty(CurScope, DS);
395 ConsumeToken();
396 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000397 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000399 // If this is not an identifier at all, bail out early.
400 if (II == 0) {
401 MatchRHSPunctuation(tok::r_paren, LHSLoc);
402 return;
403 }
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Chris Lattner156b0612008-10-20 07:37:22 +0000405 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Chris Lattner92e62b02008-11-20 04:42:34 +0000407 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000408 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000409 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000410 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000411 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000412 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000413 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000414 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000415 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000416 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000417 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000418 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000419 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattnere00da7c2008-10-20 07:39:53 +0000420 // getter/setter require extra treatment.
Chris Lattner156b0612008-10-20 07:37:22 +0000421 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
422 tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000423 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Chris Lattner8ca329c2008-10-20 07:24:39 +0000425 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000426 Diag(Tok, diag::err_expected_ident);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000427 SkipUntil(tok::r_paren);
428 return;
429 }
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Chris Lattner5fd80fa2008-10-20 07:43:01 +0000431 if (II->getName()[0] == 's') {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000432 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
433 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000434 ConsumeToken(); // consume method name
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Chris Lattner156b0612008-10-20 07:37:22 +0000436 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
437 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000438 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000439 } else {
440 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
441 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000442 ConsumeToken(); // consume method name
Chris Lattner8ca329c2008-10-20 07:24:39 +0000443 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000444 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000445 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000446 SkipUntil(tok::r_paren);
447 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000448 }
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Chris Lattner156b0612008-10-20 07:37:22 +0000450 if (Tok.isNot(tok::comma))
451 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Chris Lattner156b0612008-10-20 07:37:22 +0000453 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000454 }
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Chris Lattner156b0612008-10-20 07:37:22 +0000456 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000457}
458
Steve Naroff3536b442007-09-06 21:24:23 +0000459/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000460/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000461/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000462///
463/// objc-instance-method: '-'
464/// objc-class-method: '+'
465///
Steve Naroff4985ace2007-08-22 18:35:33 +0000466/// objc-method-attributes: [OBJC2]
467/// __attribute__((deprecated))
468///
Mike Stump1eb44332009-09-09 15:08:12 +0000469Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000470 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000471 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000472
Mike Stump1eb44332009-09-09 15:08:12 +0000473 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000474 SourceLocation mLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Chris Lattnerb28317a2009-03-28 19:18:32 +0000476 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000477 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000478 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000479 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000480}
481
482/// objc-selector:
483/// identifier
484/// one of
485/// enum struct union if else while do for switch case default
486/// break continue return goto asm sizeof typeof __alignof
487/// unsigned long const short volatile signed restrict _Complex
488/// in out inout bycopy byref oneway int char float double void _Bool
489///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000490IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000491 switch (Tok.getKind()) {
492 default:
493 return 0;
494 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000495 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000496 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000497 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000498 case tok::kw_break:
499 case tok::kw_case:
500 case tok::kw_catch:
501 case tok::kw_char:
502 case tok::kw_class:
503 case tok::kw_const:
504 case tok::kw_const_cast:
505 case tok::kw_continue:
506 case tok::kw_default:
507 case tok::kw_delete:
508 case tok::kw_do:
509 case tok::kw_double:
510 case tok::kw_dynamic_cast:
511 case tok::kw_else:
512 case tok::kw_enum:
513 case tok::kw_explicit:
514 case tok::kw_export:
515 case tok::kw_extern:
516 case tok::kw_false:
517 case tok::kw_float:
518 case tok::kw_for:
519 case tok::kw_friend:
520 case tok::kw_goto:
521 case tok::kw_if:
522 case tok::kw_inline:
523 case tok::kw_int:
524 case tok::kw_long:
525 case tok::kw_mutable:
526 case tok::kw_namespace:
527 case tok::kw_new:
528 case tok::kw_operator:
529 case tok::kw_private:
530 case tok::kw_protected:
531 case tok::kw_public:
532 case tok::kw_register:
533 case tok::kw_reinterpret_cast:
534 case tok::kw_restrict:
535 case tok::kw_return:
536 case tok::kw_short:
537 case tok::kw_signed:
538 case tok::kw_sizeof:
539 case tok::kw_static:
540 case tok::kw_static_cast:
541 case tok::kw_struct:
542 case tok::kw_switch:
543 case tok::kw_template:
544 case tok::kw_this:
545 case tok::kw_throw:
546 case tok::kw_true:
547 case tok::kw_try:
548 case tok::kw_typedef:
549 case tok::kw_typeid:
550 case tok::kw_typename:
551 case tok::kw_typeof:
552 case tok::kw_union:
553 case tok::kw_unsigned:
554 case tok::kw_using:
555 case tok::kw_virtual:
556 case tok::kw_void:
557 case tok::kw_volatile:
558 case tok::kw_wchar_t:
559 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000560 case tok::kw__Bool:
561 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000562 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000563 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000564 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000565 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000566 }
Steve Naroff294494e2007-08-22 16:35:03 +0000567}
568
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000569/// objc-for-collection-in: 'in'
570///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000571bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000572 // FIXME: May have to do additional look-ahead to only allow for
573 // valid tokens following an 'in'; such as an identifier, unary operators,
574 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000575 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000576 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000577}
578
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000579/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000580/// qualifier list and builds their bitmask representation in the input
581/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000582///
583/// objc-type-qualifiers:
584/// objc-type-qualifier
585/// objc-type-qualifiers objc-type-qualifier
586///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000587void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000588 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000589 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000590 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Chris Lattnere8b724d2007-12-12 06:56:32 +0000592 const IdentifierInfo *II = Tok.getIdentifierInfo();
593 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000594 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000595 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000597 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000598 switch (i) {
599 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000600 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
601 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
602 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
603 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
604 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
605 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000606 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000607 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000608 ConsumeToken();
609 II = 0;
610 break;
611 }
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Chris Lattnere8b724d2007-12-12 06:56:32 +0000613 // If this wasn't a recognized qualifier, bail out.
614 if (II) return;
615 }
616}
617
618/// objc-type-name:
619/// '(' objc-type-qualifiers[opt] type-name ')'
620/// '(' objc-type-qualifiers[opt] ')'
621///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000622Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000623 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Chris Lattner4a76b292008-10-22 03:52:06 +0000625 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000626 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000628 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000629 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000630
Chris Lattner4a76b292008-10-22 03:52:06 +0000631 TypeTy *Ty = 0;
Douglas Gregor809070a2009-02-18 17:45:20 +0000632 if (isTypeSpecifierQualifier()) {
633 TypeResult TypeSpec = ParseTypeName();
634 if (!TypeSpec.isInvalid())
635 Ty = TypeSpec.get();
636 }
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Steve Naroffd7333c22008-10-21 14:15:04 +0000638 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000639 ConsumeParen();
640 else if (Tok.getLocation() == TypeStartLoc) {
641 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000642 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000643 SkipUntil(tok::r_paren);
644 } else {
645 // Otherwise, we found *something*, but didn't get a ')' in the right
646 // place. Emit an error then return what we have as the type.
647 MatchRHSPunctuation(tok::r_paren, LParenLoc);
648 }
Steve Narofff28b2642007-09-05 23:30:30 +0000649 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000650}
651
652/// objc-method-decl:
653/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000654/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000655/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000656/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000657///
658/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000659/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000660/// objc-keyword-selector objc-keyword-decl
661///
662/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000663/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
664/// objc-selector ':' objc-keyword-attributes[opt] identifier
665/// ':' objc-type-name objc-keyword-attributes[opt] identifier
666/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000667///
Steve Naroff4985ace2007-08-22 18:35:33 +0000668/// objc-parmlist:
669/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000670///
Steve Naroff4985ace2007-08-22 18:35:33 +0000671/// objc-parms:
672/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000673///
Steve Naroff4985ace2007-08-22 18:35:33 +0000674/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000675/// , ...
676///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000677/// objc-keyword-attributes: [OBJC2]
678/// __attribute__((unused))
679///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000680Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
681 tok::TokenKind mType,
682 DeclPtrTy IDecl,
683 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnere8904e92008-08-23 01:48:03 +0000684 // Parse the return type if present.
Chris Lattnerff384912007-10-07 02:00:24 +0000685 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000686 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000687 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000688 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Steve Naroffbef11852007-10-26 20:53:56 +0000690 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000691 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000692
Steve Naroff84c43102009-02-11 20:43:13 +0000693 // An unnamed colon is valid.
694 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000695 Diag(Tok, diag::err_expected_selector_for_method)
696 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000697 // Skip until we get a ; or {}.
698 SkipUntil(tok::r_brace);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000699 return DeclPtrTy();
Chris Lattnere8904e92008-08-23 01:48:03 +0000700 }
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000702 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattnerdf195262007-10-09 17:51:17 +0000703 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000704 // If attributes exist after the method, parse them.
705 AttributeList *MethodAttrs = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000706 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000707 MethodAttrs = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Chris Lattnerff384912007-10-07 02:00:24 +0000709 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroffbef11852007-10-26 20:53:56 +0000710 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000711 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000712 0, CargNames, MethodAttrs,
713 MethodImplKind);
Chris Lattnerff384912007-10-07 02:00:24 +0000714 }
Steve Narofff28b2642007-09-05 23:30:30 +0000715
Steve Naroff68d331a2007-09-27 14:38:14 +0000716 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnere294d3f2009-04-11 18:57:04 +0000717 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Chris Lattnerff384912007-10-07 02:00:24 +0000719 while (1) {
Chris Lattnere294d3f2009-04-11 18:57:04 +0000720 Action::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Chris Lattnerff384912007-10-07 02:00:24 +0000722 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000723 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000724 Diag(Tok, diag::err_expected_colon);
725 break;
726 }
727 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Chris Lattnere294d3f2009-04-11 18:57:04 +0000729 ArgInfo.Type = 0;
730 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
731 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
732
Chris Lattnerff384912007-10-07 02:00:24 +0000733 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000734 ArgInfo.ArgAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000735 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnere294d3f2009-04-11 18:57:04 +0000736 ArgInfo.ArgAttrs = ParseAttributes();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000737
Chris Lattnerdf195262007-10-09 17:51:17 +0000738 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000739 Diag(Tok, diag::err_expected_ident); // missing argument name.
740 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000741 }
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Chris Lattnere294d3f2009-04-11 18:57:04 +0000743 ArgInfo.Name = Tok.getIdentifierInfo();
744 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000745 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Chris Lattnere294d3f2009-04-11 18:57:04 +0000747 ArgInfos.push_back(ArgInfo);
748 KeyIdents.push_back(SelIdent);
749
Chris Lattnerff384912007-10-07 02:00:24 +0000750 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000751 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000752 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000753 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000754 break;
755 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000756 }
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Steve Naroff335eafa2007-11-15 12:35:21 +0000758 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Chris Lattnerff384912007-10-07 02:00:24 +0000760 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000761 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000762 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000763 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000764 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000765 ConsumeToken();
766 break;
767 }
Chris Lattnerff384912007-10-07 02:00:24 +0000768 DeclSpec DS;
769 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000770 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +0000771 Declarator ParmDecl(DS, Declarator::PrototypeContext);
772 ParseDeclarator(ParmDecl);
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000773 CargNames.push_back(ParmDecl);
Chris Lattnerff384912007-10-07 02:00:24 +0000774 }
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Chris Lattnerff384912007-10-07 02:00:24 +0000776 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000777 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000778 AttributeList *MethodAttrs = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000779 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000780 MethodAttrs = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000782 if (KeyIdents.size() == 0)
783 return DeclPtrTy();
Chris Lattnerff384912007-10-07 02:00:24 +0000784 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
785 &KeyIdents[0]);
Steve Naroffbef11852007-10-26 20:53:56 +0000786 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000787 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000788 &ArgInfos[0], CargNames, MethodAttrs,
Steve Naroff335eafa2007-11-15 12:35:21 +0000789 MethodImplKind, isVariadic);
Steve Naroff294494e2007-08-22 16:35:03 +0000790}
791
Steve Naroffdac269b2007-08-20 21:31:48 +0000792/// objc-protocol-refs:
793/// '<' identifier-list '>'
794///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000795bool Parser::
Chris Lattnerb28317a2009-03-28 19:18:32 +0000796ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000797 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
798 bool WarnOnDeclarations,
799 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +0000800 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000802 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Chris Lattnere13b9592008-07-26 04:03:38 +0000804 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Chris Lattnere13b9592008-07-26 04:03:38 +0000806 while (1) {
807 if (Tok.isNot(tok::identifier)) {
808 Diag(Tok, diag::err_expected_ident);
809 SkipUntil(tok::greater);
810 return true;
811 }
812 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
813 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000814 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +0000815 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Chris Lattnere13b9592008-07-26 04:03:38 +0000817 if (Tok.isNot(tok::comma))
818 break;
819 ConsumeToken();
820 }
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Chris Lattnere13b9592008-07-26 04:03:38 +0000822 // Consume the '>'.
823 if (Tok.isNot(tok::greater)) {
824 Diag(Tok, diag::err_expected_greater);
825 return true;
826 }
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Chris Lattnere13b9592008-07-26 04:03:38 +0000828 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Chris Lattnere13b9592008-07-26 04:03:38 +0000830 // Convert the list of protocols identifiers into a list of protocol decls.
831 Actions.FindProtocolDeclaration(WarnOnDeclarations,
832 &ProtocolIdents[0], ProtocolIdents.size(),
833 Protocols);
834 return false;
835}
836
Steve Naroffdac269b2007-08-20 21:31:48 +0000837/// objc-class-instance-variables:
838/// '{' objc-instance-variable-decl-list[opt] '}'
839///
840/// objc-instance-variable-decl-list:
841/// objc-visibility-spec
842/// objc-instance-variable-decl ';'
843/// ';'
844/// objc-instance-variable-decl-list objc-visibility-spec
845/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
846/// objc-instance-variable-decl-list ';'
847///
848/// objc-visibility-spec:
849/// @private
850/// @protected
851/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000852/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000853///
854/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +0000855/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +0000856///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000857void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff60fccee2007-10-29 21:38:07 +0000858 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000859 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattnerb28317a2009-03-28 19:18:32 +0000860 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000861 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
862
Douglas Gregor1a0d31a2009-01-12 18:45:55 +0000863 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +0000864
Steve Naroffddbff782007-08-21 21:17:12 +0000865 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000867 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000868 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000869 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000870 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Steve Naroffddbff782007-08-21 21:17:12 +0000872 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000873 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000874 Diag(Tok, diag::ext_extra_struct_semi);
875 ConsumeToken();
876 continue;
877 }
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Steve Naroffddbff782007-08-21 21:17:12 +0000879 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000880 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000881 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000882 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000883 case tok::objc_private:
884 case tok::objc_public:
885 case tok::objc_protected:
886 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000887 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000888 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000889 continue;
Steve Naroffddbff782007-08-21 21:17:12 +0000890 default:
891 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000892 continue;
893 }
894 }
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Chris Lattnere1359422008-04-10 06:46:29 +0000896 // Parse all the comma separated declarators.
897 DeclSpec DS;
898 FieldDeclarators.clear();
899 ParseStructDeclaration(DS, FieldDeclarators);
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Chris Lattnere1359422008-04-10 06:46:29 +0000901 // Convert them all to fields.
902 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
903 FieldDeclarator &FD = FieldDeclarators[i];
904 // Install the declarator into interfaceDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000905 DeclPtrTy Field = Actions.ActOnIvar(CurScope,
906 DS.getSourceRange().getBegin(),
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000907 interfaceDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000908 FD.D, FD.BitfieldSize, visibility);
Chris Lattnere1359422008-04-10 06:46:29 +0000909 AllIvarDecls.push_back(Field);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000910 }
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Chris Lattnerdf195262007-10-09 17:51:17 +0000912 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000913 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +0000914 } else {
915 Diag(Tok, diag::err_expected_semi_decl_list);
916 // Skip to end of block or statement
917 SkipUntil(tok::r_brace, true, true);
918 }
919 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000920 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000921 // Call ActOnFields() even if we don't have any decls. This is useful
922 // for code rewriting tools that need to be aware of the empty list.
923 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000924 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +0000925 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +0000926 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000927}
Steve Naroffdac269b2007-08-20 21:31:48 +0000928
929/// objc-protocol-declaration:
930/// objc-protocol-definition
931/// objc-protocol-forward-reference
932///
933/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +0000934/// @protocol identifier
935/// objc-protocol-refs[opt]
936/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000937/// @end
938///
939/// objc-protocol-forward-reference:
940/// @protocol identifier-list ';'
941///
942/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000943/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000944/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000945Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
946 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000947 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000948 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
949 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Chris Lattnerdf195262007-10-09 17:51:17 +0000951 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000952 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000953 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000954 }
955 // Save the protocol name, then consume it.
956 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
957 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Chris Lattnerdf195262007-10-09 17:51:17 +0000959 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000960 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000961 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000962 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000963 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000964 }
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Chris Lattnerdf195262007-10-09 17:51:17 +0000966 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000967 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
968 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
969
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000970 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000971 while (1) {
972 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +0000973 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000974 Diag(Tok, diag::err_expected_ident);
975 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000976 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000977 }
Chris Lattner7caeabd2008-07-21 22:17:28 +0000978 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
979 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000980 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Chris Lattnerdf195262007-10-09 17:51:17 +0000982 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000983 break;
984 }
985 // Consume the ';'.
986 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000987 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Steve Naroffe440eb82007-10-10 17:32:04 +0000989 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000990 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000991 ProtocolRefs.size(),
992 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +0000993 }
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000995 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000996 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000997
Chris Lattnerb28317a2009-03-28 19:18:32 +0000998 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000999 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001000 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001001 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1002 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001003 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Chris Lattnerb28317a2009-03-28 19:18:32 +00001005 DeclPtrTy ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001006 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001007 ProtocolRefs.data(),
1008 ProtocolRefs.size(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +00001009 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001010 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001011 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001012}
Steve Naroffdac269b2007-08-20 21:31:48 +00001013
1014/// objc-implementation:
1015/// objc-class-implementation-prologue
1016/// objc-category-implementation-prologue
1017///
1018/// objc-class-implementation-prologue:
1019/// @implementation identifier objc-superclass[opt]
1020/// objc-class-instance-variables[opt]
1021///
1022/// objc-category-implementation-prologue:
1023/// @implementation identifier ( identifier )
Chris Lattnerb28317a2009-03-28 19:18:32 +00001024Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001025 SourceLocation atLoc) {
1026 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1027 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1028 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Chris Lattnerdf195262007-10-09 17:51:17 +00001030 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001031 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001032 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001033 }
1034 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001035 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001036 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001037
1038 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001039 // we have a category implementation.
1040 SourceLocation lparenLoc = ConsumeParen();
1041 SourceLocation categoryLoc, rparenLoc;
1042 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Chris Lattnerdf195262007-10-09 17:51:17 +00001044 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001045 categoryId = Tok.getIdentifierInfo();
1046 categoryLoc = ConsumeToken();
1047 } else {
1048 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001049 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001050 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001051 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001052 Diag(Tok, diag::err_expected_rparen);
1053 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001054 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001055 }
1056 rparenLoc = ConsumeParen();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001057 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001058 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001059 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001060 ObjCImpDecl = ImplCatType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001061 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001062 }
1063 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001064 SourceLocation superClassLoc;
1065 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001066 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001067 // We have a super class
1068 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001069 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001070 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001071 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001072 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001073 superClassId = Tok.getIdentifierInfo();
1074 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001075 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001076 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001077 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001078 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Steve Naroff60fccee2007-10-29 21:38:07 +00001080 if (Tok.is(tok::l_brace)) // we have ivars
1081 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001082 ObjCImpDecl = ImplClsType;
Mike Stump1eb44332009-09-09 15:08:12 +00001083
Chris Lattnerb28317a2009-03-28 19:18:32 +00001084 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001085}
Steve Naroff60fccee2007-10-29 21:38:07 +00001086
Chris Lattnerb28317a2009-03-28 19:18:32 +00001087Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001088 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1089 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001090 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001091 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001092 if (ObjCImpDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001093 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001094 ObjCImpDecl = DeclPtrTy();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001095 }
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001096 else
1097 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001098 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001099}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001100
1101/// compatibility-alias-decl:
1102/// @compatibility_alias alias-name class-name ';'
1103///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001104Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001105 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1106 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1107 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001108 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001109 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001110 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001111 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001112 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1113 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001114 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001115 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001116 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001117 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001118 IdentifierInfo *classId = Tok.getIdentifierInfo();
1119 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1120 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001121 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001122 return DeclPtrTy();
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001123 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001124 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1125 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001126}
1127
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001128/// property-synthesis:
1129/// @synthesize property-ivar-list ';'
1130///
1131/// property-ivar-list:
1132/// property-ivar
1133/// property-ivar-list ',' property-ivar
1134///
1135/// property-ivar:
1136/// identifier
1137/// identifier '=' identifier
1138///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001139Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001140 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1141 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001142 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnerdf195262007-10-09 17:51:17 +00001143 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001144 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001145 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001146 }
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Chris Lattnerdf195262007-10-09 17:51:17 +00001148 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001149 IdentifierInfo *propertyIvar = 0;
1150 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1151 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001152 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001153 // property '=' ivar-name
1154 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +00001155 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001156 Diag(Tok, diag::err_expected_ident);
1157 break;
1158 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001159 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001160 ConsumeToken(); // consume ivar-name
1161 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001162 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1163 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001164 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001165 break;
1166 ConsumeToken(); // consume ','
1167 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001168 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001169 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001170 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001171}
1172
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001173/// property-dynamic:
1174/// @dynamic property-list
1175///
1176/// property-list:
1177/// identifier
1178/// property-list ',' identifier
1179///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001180Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001181 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1182 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1183 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001184 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001185 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001186 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001187 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001188 while (Tok.is(tok::identifier)) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001189 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1190 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1191 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1192 propertyId, 0);
1193
Chris Lattnerdf195262007-10-09 17:51:17 +00001194 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001195 break;
1196 ConsumeToken(); // consume ','
1197 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001198 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001199 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001200 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001201}
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001203/// objc-throw-statement:
1204/// throw expression[opt];
1205///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001206Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001207 OwningExprResult Res(Actions);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001208 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001209 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001210 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001211 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001212 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001213 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001214 }
1215 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001216 ConsumeToken(); // consume ';'
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001217 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001218}
1219
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001220/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001221/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001222///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001223Parser::OwningStmtResult
1224Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001225 ConsumeToken(); // consume synchronized
1226 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001227 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001228 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001229 }
1230 ConsumeParen(); // '('
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001231 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001232 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001233 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001234 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001235 }
1236 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001237 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001238 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001239 }
1240 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001241 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001242 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001243 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001244 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001245 // Enter a scope to hold everything within the compound stmt. Compound
1246 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001247 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001248
Sebastian Redl61364dd2008-12-11 19:30:53 +00001249 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001250
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001251 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001252 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001253 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001254 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001255}
1256
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001257/// objc-try-catch-statement:
1258/// @try compound-statement objc-catch-list[opt]
1259/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1260///
1261/// objc-catch-list:
1262/// @catch ( parameter-declaration ) compound-statement
1263/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1264/// catch-parameter-declaration:
1265/// parameter-declaration
1266/// '...' [OBJC2]
1267///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001268Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001269 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001270
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001271 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001272 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001273 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001274 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001275 }
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001276 OwningStmtResult CatchStmts(Actions);
1277 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001278 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001279 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001280 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001281 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001282 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001283
Chris Lattnerdf195262007-10-09 17:51:17 +00001284 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001285 // At this point, we need to lookahead to determine if this @ is the start
1286 // of an @catch or @finally. We don't want to consume the @ token if this
1287 // is an @try or @encode or something else.
1288 Token AfterAt = GetLookAheadToken(1);
1289 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1290 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1291 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001292
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001293 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001294 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001295 DeclPtrTy FirstPart;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001296 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001297 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001298 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001299 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001300 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001301 DeclSpec DS;
1302 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001303 // For some odd reason, the name of the exception variable is
Mike Stump1eb44332009-09-09 15:08:12 +00001304 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff7ba138a2009-03-03 19:52:17 +00001305 // we must accept either 'declarator' or 'abstract-declarator' here.
1306 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1307 ParseDeclarator(ParmDecl);
1308
1309 // Inform the actions module about the parameter declarator, so it
1310 // gets added to the current scope.
1311 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001312 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001313 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001314
Steve Naroff93a25952009-04-07 22:56:58 +00001315 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001316
Steve Naroff93a25952009-04-07 22:56:58 +00001317 if (Tok.is(tok::r_paren))
1318 RParenLoc = ConsumeParen();
1319 else // Skip over garbage, until we get to ')'. Eat the ')'.
1320 SkipUntil(tok::r_paren, true, false);
1321
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001322 OwningStmtResult CatchBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001323 if (Tok.is(tok::l_brace))
1324 CatchBody = ParseCompoundStatementBody();
1325 else
1326 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001327 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001328 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001329 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff7ba138a2009-03-03 19:52:17 +00001330 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001331 move(CatchStmts));
Steve Naroff64515f32008-02-05 21:27:35 +00001332 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001333 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1334 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001335 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001336 }
1337 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001338 } else {
1339 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001340 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001341 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001342
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001343 OwningStmtResult FinallyBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001344 if (Tok.is(tok::l_brace))
1345 FinallyBody = ParseCompoundStatementBody();
1346 else
1347 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001348 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001349 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001350 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001351 move(FinallyBody));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001352 catch_or_finally_seen = true;
1353 break;
1354 }
1355 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001356 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001357 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001358 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001359 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001360 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1361 move(FinallyStmt));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001362}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001363
Steve Naroff3536b442007-09-06 21:24:23 +00001364/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001365///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001366Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1367 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001368
Chris Lattner49f28ca2009-03-05 08:00:35 +00001369 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1370 PP.getSourceManager(),
1371 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001373 // parse optional ';'
Chris Lattnerdf195262007-10-09 17:51:17 +00001374 if (Tok.is(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001375 ConsumeToken();
1376
Steve Naroff409be832007-11-11 19:54:21 +00001377 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001378 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001379 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Steve Naroff409be832007-11-11 19:54:21 +00001381 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1382 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Steve Naroff409be832007-11-11 19:54:21 +00001384 // If we didn't find the '{', bail out.
1385 if (Tok.isNot(tok::l_brace))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001386 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001387 }
Steve Naroff409be832007-11-11 19:54:21 +00001388 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001389
Steve Naroff409be832007-11-11 19:54:21 +00001390 // Enter a scope for the method body.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001391 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Steve Naroff409be832007-11-11 19:54:21 +00001393 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001394 // specified Declarator for the method.
Steve Naroffebf64432009-02-28 16:59:13 +00001395 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001396
1397 OwningStmtResult FnBody(ParseCompoundStatementBody());
1398
Steve Naroff409be832007-11-11 19:54:21 +00001399 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001400 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001401 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1402 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001403
Steve Naroff32ce8372009-03-02 22:00:56 +00001404 // TODO: Pass argument information.
1405 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump1eb44332009-09-09 15:08:12 +00001406
Steve Naroff409be832007-11-11 19:54:21 +00001407 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001408 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001409
Steve Naroff71c0a952007-11-13 23:01:27 +00001410 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001411}
Anders Carlsson55085182007-08-21 17:43:55 +00001412
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001413Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroff64515f32008-02-05 21:27:35 +00001414 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001415 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001416 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1417 return ParseObjCThrowStmt(AtLoc);
1418 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1419 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001420 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001421 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001422 // If the expression is invalid, skip ahead to the next semicolon. Not
1423 // doing this opens us up to the possibility of infinite loops if
1424 // ParseExpression does not consume any tokens.
1425 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001426 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001427 }
1428 // Otherwise, eat the semicolon.
1429 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson6b1d2832009-05-17 21:11:30 +00001430 return Actions.ActOnExprStmt(Actions.FullExpr(Res));
Steve Naroff64515f32008-02-05 21:27:35 +00001431}
1432
Sebastian Redl1d922962008-12-13 15:32:12 +00001433Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001434 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001435 case tok::string_literal: // primary-expression: string-literal
1436 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001437 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001438 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001439 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001440 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001441
Chris Lattner4fef81d2008-08-05 06:19:09 +00001442 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1443 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001444 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001445 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001446 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001447 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001448 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001449 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001450 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001451 }
Anders Carlsson55085182007-08-21 17:43:55 +00001452 }
Anders Carlsson55085182007-08-21 17:43:55 +00001453}
1454
Mike Stump1eb44332009-09-09 15:08:12 +00001455/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001456/// '[' objc-receiver objc-message-args ']'
1457///
1458/// objc-receiver:
1459/// expression
1460/// class-name
1461/// type-name
Sebastian Redl1d922962008-12-13 15:32:12 +00001462Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001463 assert(Tok.is(tok::l_square) && "'[' expected");
1464 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1465
1466 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001467 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001468 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahaniand2869922009-04-08 19:50:10 +00001469 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1470 SourceLocation NameLoc = ConsumeToken();
1471 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1472 ExprArg(Actions));
1473 }
Chris Lattner699b6612008-01-25 18:59:06 +00001474 }
1475
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001476 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001477 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001478 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001479 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001480 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001481
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001482 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001483 0, move(Res));
Chris Lattner699b6612008-01-25 18:59:06 +00001484}
Sebastian Redl1d922962008-12-13 15:32:12 +00001485
Chris Lattner699b6612008-01-25 18:59:06 +00001486/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1487/// the rest of a message expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001488///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001489/// objc-message-args:
1490/// objc-selector
1491/// objc-keywordarg-list
1492///
1493/// objc-keywordarg-list:
1494/// objc-keywordarg
1495/// objc-keywordarg-list objc-keywordarg
1496///
Mike Stump1eb44332009-09-09 15:08:12 +00001497/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001498/// selector-name[opt] ':' objc-keywordexpr
1499///
1500/// objc-keywordexpr:
1501/// nonempty-expr-list
1502///
1503/// nonempty-expr-list:
1504/// assignment-expression
1505/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001506///
1507Parser::OwningExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001508Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +00001509 SourceLocation NameLoc,
Chris Lattner699b6612008-01-25 18:59:06 +00001510 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +00001511 ExprArg ReceiverExpr) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001512 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001513 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001514 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001515
Anders Carlssonff975cf2009-02-14 18:21:46 +00001516 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Steve Naroff68d331a2007-09-27 14:38:14 +00001518 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001519 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00001520
Chris Lattnerdf195262007-10-09 17:51:17 +00001521 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001522 while (1) {
1523 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001524 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001525
Chris Lattnerdf195262007-10-09 17:51:17 +00001526 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001527 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001528 // We must manually skip to a ']', otherwise the expression skipper will
1529 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1530 // the enclosing expression.
1531 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001532 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001533 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001534
Steve Naroff68d331a2007-09-27 14:38:14 +00001535 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001536 /// Parse the expression after ':'
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001537 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001538 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001539 // We must manually skip to a ']', otherwise the expression skipper will
1540 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1541 // the enclosing expression.
1542 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001543 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00001544 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001545
Steve Naroff37387c92007-09-17 20:25:27 +00001546 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001547 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00001548
Steve Naroff37387c92007-09-17 20:25:27 +00001549 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00001550 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001551 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001552 break;
1553 // We have a selector or a colon, continue parsing.
1554 }
1555 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001556 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001557 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00001558 /// Parse the expression after ','
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001559 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001560 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001561 // We must manually skip to a ']', otherwise the expression skipper will
1562 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1563 // the enclosing expression.
1564 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001565 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00001566 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001567
Steve Naroff49f109c2007-11-15 13:05:42 +00001568 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001569 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001570 }
1571 } else if (!selIdent) {
1572 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00001573
Chris Lattner4fef81d2008-08-05 06:19:09 +00001574 // We must manually skip to a ']', otherwise the expression skipper will
1575 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1576 // the enclosing expression.
1577 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001578 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001579 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001580
Chris Lattnerdf195262007-10-09 17:51:17 +00001581 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001582 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001583 // We must manually skip to a ']', otherwise the expression skipper will
1584 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1585 // the enclosing expression.
1586 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001587 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001588 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001589
Chris Lattner699b6612008-01-25 18:59:06 +00001590 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00001591
Steve Naroff29238a02007-10-05 18:42:47 +00001592 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001593 if (nKeys == 0)
1594 KeyIdents.push_back(selIdent);
1595 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001596
Chris Lattnerff384912007-10-07 02:00:24 +00001597 // We've just parsed a keyword message.
Sebastian Redl1d922962008-12-13 15:32:12 +00001598 if (ReceiverName)
1599 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump1eb44332009-09-09 15:08:12 +00001600 LBracLoc, NameLoc, SelectorLoc,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001601 RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001602 KeyExprs.take(), KeyExprs.size()));
1603 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001604 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001605 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001606}
1607
Sebastian Redl1d922962008-12-13 15:32:12 +00001608Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl20df9b72008-12-11 22:51:44 +00001609 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00001610 if (Res.isInvalid()) return move(Res);
1611
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001612 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1613 // expressions. At this point, we know that the only valid thing that starts
1614 // with '@' is an @"".
1615 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001616 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001617 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00001618 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001619
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001620 while (Tok.is(tok::at)) {
1621 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001622
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001623 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001624 if (!isTokenStringLiteral())
1625 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001626
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001627 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001628 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00001629 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001630
Sebastian Redleffa8d12008-12-10 00:02:53 +00001631 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001632 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001633
1634 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1635 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00001636}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001637
1638/// objc-encode-expression:
1639/// @encode ( type-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001640Parser::OwningExprResult
1641Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001642 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00001643
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001644 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001645
Chris Lattner4fef81d2008-08-05 06:19:09 +00001646 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001647 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1648
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001649 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001650
Douglas Gregor809070a2009-02-18 17:45:20 +00001651 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00001652
Anders Carlsson4988ae32007-08-23 15:31:37 +00001653 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001654
Douglas Gregor809070a2009-02-18 17:45:20 +00001655 if (Ty.isInvalid())
1656 return ExprError();
1657
Mike Stump1eb44332009-09-09 15:08:12 +00001658 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00001659 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001660}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001661
1662/// objc-protocol-expression
1663/// @protocol ( protocol-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001664Parser::OwningExprResult
1665Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001666 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001667
Chris Lattner4fef81d2008-08-05 06:19:09 +00001668 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001669 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1670
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001671 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001672
Chris Lattner4fef81d2008-08-05 06:19:09 +00001673 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00001674 return ExprError(Diag(Tok, diag::err_expected_ident));
1675
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001676 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001677 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001678
Anders Carlsson4988ae32007-08-23 15:31:37 +00001679 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001680
Sebastian Redl1d922962008-12-13 15:32:12 +00001681 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1682 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001683}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001684
1685/// objc-selector-expression
1686/// @selector '(' objc-keyword-selector ')'
Sebastian Redl1d922962008-12-13 15:32:12 +00001687Parser::OwningExprResult
1688Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001689 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001690
Chris Lattner4fef81d2008-08-05 06:19:09 +00001691 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001692 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1693
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001694 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001695 SourceLocation LParenLoc = ConsumeParen();
1696 SourceLocation sLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001697 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001698 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1699 return ExprError(Diag(Tok, diag::err_expected_ident));
1700
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001701 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001702 unsigned nColons = 0;
1703 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001704 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001705 if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00001706 return ExprError(Diag(Tok, diag::err_expected_colon));
1707
Chris Lattnercb53b362007-12-27 19:57:00 +00001708 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001709 ConsumeToken(); // Eat the ':'.
1710 if (Tok.is(tok::r_paren))
1711 break;
1712 // Check for another keyword selector.
1713 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001714 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001715 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001716 if (!SelIdent && Tok.isNot(tok::colon))
1717 break;
1718 }
Steve Naroff887407e2007-12-05 22:21:29 +00001719 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001720 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001721 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001722 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1723 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00001724 }