blob: 9fe4c1ec12e24d477c8e71e3fef99bf2329b8f46 [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"
Sebastian Redla55e52c2008-11-25 22:21:31 +000017#include "AstGuard.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/Basic/Diagnostic.h"
19#include "llvm/ADT/SmallVector.h"
20using namespace clang;
21
22
23/// ParseExternalDeclaration:
24/// external-declaration: [C99 6.9]
25/// [OBJC] objc-class-definition
Steve Naroff91fa0b72007-10-29 21:39:29 +000026/// [OBJC] objc-class-declaration
27/// [OBJC] objc-alias-declaration
28/// [OBJC] objc-protocol-definition
29/// [OBJC] objc-method-definition
30/// [OBJC] '@' 'end'
Steve Naroffdac269b2007-08-20 21:31:48 +000031Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000032 SourceLocation AtLoc = ConsumeToken(); // the "@"
33
Steve Naroff861cf3e2007-08-23 18:16:40 +000034 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000035 case tok::objc_class:
36 return ParseObjCAtClassDeclaration(AtLoc);
37 case tok::objc_interface:
38 return ParseObjCAtInterfaceDeclaration(AtLoc);
39 case tok::objc_protocol:
40 return ParseObjCAtProtocolDeclaration(AtLoc);
41 case tok::objc_implementation:
42 return ParseObjCAtImplementationDeclaration(AtLoc);
43 case tok::objc_end:
44 return ParseObjCAtEndDeclaration(AtLoc);
45 case tok::objc_compatibility_alias:
46 return ParseObjCAtAliasDeclaration(AtLoc);
47 case tok::objc_synthesize:
48 return ParseObjCPropertySynthesize(AtLoc);
49 case tok::objc_dynamic:
50 return ParseObjCPropertyDynamic(AtLoc);
51 default:
52 Diag(AtLoc, diag::err_unexpected_at);
53 SkipUntil(tok::semi);
54 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000055 }
56}
57
58///
59/// objc-class-declaration:
60/// '@' 'class' identifier-list ';'
61///
Steve Naroffdac269b2007-08-20 21:31:48 +000062Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000063 ConsumeToken(); // the identifier "class"
64 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
65
66 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000067 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000068 Diag(Tok, diag::err_expected_ident);
69 SkipUntil(tok::semi);
Steve Naroffdac269b2007-08-20 21:31:48 +000070 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000071 }
Reid Spencer5f016e22007-07-11 17:01:13 +000072 ClassNames.push_back(Tok.getIdentifierInfo());
73 ConsumeToken();
74
Chris Lattnerdf195262007-10-09 17:51:17 +000075 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000076 break;
77
78 ConsumeToken();
79 }
80
81 // Consume the ';'.
82 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Steve Naroffdac269b2007-08-20 21:31:48 +000083 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000084
Steve Naroffe440eb82007-10-10 17:32:04 +000085 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff3536b442007-09-06 21:24:23 +000086 &ClassNames[0], ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000087}
88
Steve Naroffdac269b2007-08-20 21:31:48 +000089///
90/// objc-interface:
91/// objc-class-interface-attributes[opt] objc-class-interface
92/// objc-category-interface
93///
94/// objc-class-interface:
95/// '@' 'interface' identifier objc-superclass[opt]
96/// objc-protocol-refs[opt]
97/// objc-class-instance-variables[opt]
98/// objc-interface-decl-list
99/// @end
100///
101/// objc-category-interface:
102/// '@' 'interface' identifier '(' identifier[opt] ')'
103/// objc-protocol-refs[opt]
104/// objc-interface-decl-list
105/// @end
106///
107/// objc-superclass:
108/// ':' identifier
109///
110/// objc-class-interface-attributes:
111/// __attribute__((visibility("default")))
112/// __attribute__((visibility("hidden")))
113/// __attribute__((deprecated))
114/// __attribute__((unavailable))
115/// __attribute__((objc_exception)) - used by NSException on 64-bit
116///
117Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration(
118 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000119 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000120 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
121 ConsumeToken(); // the "interface" identifier
122
Chris Lattnerdf195262007-10-09 17:51:17 +0000123 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000124 Diag(Tok, diag::err_expected_ident); // missing class or category name.
125 return 0;
126 }
127 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000128 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000129 SourceLocation nameLoc = ConsumeToken();
130
Chris Lattnerdf195262007-10-09 17:51:17 +0000131 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000132 SourceLocation lparenLoc = ConsumeParen();
133 SourceLocation categoryLoc, rparenLoc;
134 IdentifierInfo *categoryId = 0;
135
Steve Naroff527fe232007-08-23 19:56:30 +0000136 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000137 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000138 categoryId = Tok.getIdentifierInfo();
139 categoryLoc = ConsumeToken();
Steve Naroff527fe232007-08-23 19:56:30 +0000140 } else if (!getLang().ObjC2) {
141 Diag(Tok, diag::err_expected_ident); // missing category name.
142 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000143 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000144 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000145 Diag(Tok, diag::err_expected_rparen);
146 SkipUntil(tok::r_paren, false); // don't stop at ';'
147 return 0;
148 }
149 rparenLoc = ConsumeParen();
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000150
Steve Naroffdac269b2007-08-20 21:31:48 +0000151 // Next, we need to check for any protocol references.
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000152 SourceLocation EndProtoLoc;
153 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
154 if (Tok.is(tok::less) &&
155 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
156 return 0;
157
Steve Naroffdac269b2007-08-20 21:31:48 +0000158 if (attrList) // categories don't support attributes.
159 Diag(Tok, diag::err_objc_no_attributes_on_category);
160
Steve Naroffe440eb82007-10-10 17:32:04 +0000161 DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
Steve Naroff3a165b02007-10-03 21:00:46 +0000162 nameId, nameLoc, categoryId, categoryLoc,
Steve Naroff423cb562007-10-30 13:30:57 +0000163 &ProtocolRefs[0], ProtocolRefs.size(),
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000164 EndProtoLoc);
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000165
166 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000167 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000168 }
169 // Parse a class interface.
170 IdentifierInfo *superClassId = 0;
171 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000172
Chris Lattnerdf195262007-10-09 17:51:17 +0000173 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000174 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000175 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000176 Diag(Tok, diag::err_expected_ident); // missing super class name.
177 return 0;
178 }
179 superClassId = Tok.getIdentifierInfo();
180 superClassLoc = ConsumeToken();
181 }
182 // Next, we need to check for any protocol references.
Chris Lattner06036d32008-07-26 04:13:19 +0000183 llvm::SmallVector<Action::DeclTy*, 8> ProtocolRefs;
184 SourceLocation EndProtoLoc;
185 if (Tok.is(tok::less) &&
186 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
187 return 0;
188
189 DeclTy *ClsType =
190 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
191 superClassId, superClassLoc,
192 &ProtocolRefs[0], ProtocolRefs.size(),
193 EndProtoLoc, attrList);
Steve Narofff28b2642007-09-05 23:30:30 +0000194
Chris Lattnerdf195262007-10-09 17:51:17 +0000195 if (Tok.is(tok::l_brace))
Steve Naroff60fccee2007-10-29 21:38:07 +0000196 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000197
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000198 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000199 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000200}
201
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000202/// constructSetterName - Return the setter name for the given
203/// identifier, i.e. "set" + Name where the initial character of Name
204/// has been capitalized.
205static IdentifierInfo *constructSetterName(IdentifierTable &Idents,
206 const IdentifierInfo *Name) {
Chris Lattneredc66f32008-11-19 07:41:27 +0000207 llvm::SmallString<100> SelectorName;
Chris Lattner69d27b92008-11-20 07:09:32 +0000208 SelectorName = "set";
Chris Lattneredc66f32008-11-19 07:41:27 +0000209 SelectorName.append(Name->getName(), Name->getName()+Name->getLength());
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000210 SelectorName[3] = toupper(SelectorName[3]);
Chris Lattneredc66f32008-11-19 07:41:27 +0000211 return &Idents.get(&SelectorName[0], &SelectorName[SelectorName.size()]);
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000212}
213
Steve Naroffdac269b2007-08-20 21:31:48 +0000214/// objc-interface-decl-list:
215/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000216/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000217/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000218/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000219/// objc-interface-decl-list declaration
220/// objc-interface-decl-list ';'
221///
Steve Naroff294494e2007-08-22 16:35:03 +0000222/// objc-method-requirement: [OBJC2]
223/// @required
224/// @optional
225///
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000226void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000227 tok::ObjCKeywordKind contextKey) {
Ted Kremenek8a779312008-06-06 16:45:15 +0000228 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000229 llvm::SmallVector<DeclTy*, 16> allProperties;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000230 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff60fccee2007-10-29 21:38:07 +0000231
Chris Lattnerbc662af2008-10-20 06:10:06 +0000232 SourceLocation AtEndLoc;
233
Steve Naroff294494e2007-08-22 16:35:03 +0000234 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000235 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000236 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
237 DeclTy *methodPrototype =
238 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000239 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000240 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
241 // method definitions.
Steve Naroffd16245b2007-09-17 15:07:43 +0000242 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff294494e2007-08-22 16:35:03 +0000243 continue;
244 }
Fariborz Jahanianf366b4c2007-12-11 18:34:51 +0000245
Chris Lattnere82a10f2008-10-20 05:46:22 +0000246 // Ignore excess semicolons.
247 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000248 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000249 continue;
250 }
251
Chris Lattnerbc662af2008-10-20 06:10:06 +0000252 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000253 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000254 break;
Chris Lattnere82a10f2008-10-20 05:46:22 +0000255
256 // If we don't have an @ directive, parse it as a function definition.
257 if (Tok.isNot(tok::at)) {
Steve Naroff4985ace2007-08-22 18:35:33 +0000258 // FIXME: as the name implies, this rule allows function definitions.
259 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff3536b442007-09-06 21:24:23 +0000260 ParseDeclarationOrFunctionDefinition();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000261 continue;
262 }
263
264 // Otherwise, we have an @ directive, eat the @.
265 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnera2449b22008-10-20 05:57:40 +0000266 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000267
Chris Lattnera2449b22008-10-20 05:57:40 +0000268 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattnere82a10f2008-10-20 05:46:22 +0000269 AtEndLoc = AtLoc;
270 break;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000271 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000272
Chris Lattnerbc662af2008-10-20 06:10:06 +0000273 // Eat the identifier.
274 ConsumeToken();
275
Chris Lattnera2449b22008-10-20 05:57:40 +0000276 switch (DirectiveKind) {
277 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000278 // FIXME: If someone forgets an @end on a protocol, this loop will
279 // continue to eat up tons of stuff and spew lots of nonsense errors. It
280 // would probably be better to bail out if we saw an @class or @interface
281 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000282 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000283 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000284 SkipUntil(tok::r_brace, tok::at);
285 break;
286
287 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000288 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000289 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000290 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000291 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000292 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000293 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000294 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000295 break;
296
297 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000298 if (!getLang().ObjC2)
299 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
300
Chris Lattnere82a10f2008-10-20 05:46:22 +0000301 ObjCDeclSpec OCDS;
Chris Lattnere82a10f2008-10-20 05:46:22 +0000302 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000303 if (Tok.is(tok::l_paren))
Chris Lattnere82a10f2008-10-20 05:46:22 +0000304 ParseObjCPropertyAttribute(OCDS);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000305
Chris Lattnere82a10f2008-10-20 05:46:22 +0000306 // Parse all the comma separated declarators.
307 DeclSpec DS;
308 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
309 ParseStructDeclaration(DS, FieldDeclarators);
310
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000311 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
312 tok::at);
313
Chris Lattnere82a10f2008-10-20 05:46:22 +0000314 // Convert them all to property declarations.
315 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
316 FieldDeclarator &FD = FieldDeclarators[i];
Chris Lattnerda3253d2008-10-20 06:33:53 +0000317 if (FD.D.getIdentifier() == 0) {
Chris Lattneref708fd2008-11-18 07:50:21 +0000318 Diag(AtLoc, diag::err_objc_property_requires_field_name)
319 << FD.D.getSourceRange();
Chris Lattnerda3253d2008-10-20 06:33:53 +0000320 continue;
321 }
322
Chris Lattnere82a10f2008-10-20 05:46:22 +0000323 // Install the property declarator into interfaceDecl.
Chris Lattnerda3253d2008-10-20 06:33:53 +0000324 IdentifierInfo *SelName =
325 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
326
Chris Lattnere82a10f2008-10-20 05:46:22 +0000327 Selector GetterSel =
Chris Lattnerda3253d2008-10-20 06:33:53 +0000328 PP.getSelectorTable().getNullarySelector(SelName);
Chris Lattnere82a10f2008-10-20 05:46:22 +0000329 IdentifierInfo *SetterName = OCDS.getSetterName();
330 if (!SetterName)
331 SetterName = constructSetterName(PP.getIdentifierTable(),
332 FD.D.getIdentifier());
333 Selector SetterSel =
334 PP.getSelectorTable().getUnarySelector(SetterName);
Chris Lattnerda3253d2008-10-20 06:33:53 +0000335 DeclTy *Property = Actions.ActOnProperty(CurScope, AtLoc, FD, OCDS,
336 GetterSel, SetterSel,
337 MethodImplKind);
Chris Lattnere82a10f2008-10-20 05:46:22 +0000338 allProperties.push_back(Property);
339 }
Chris Lattnera2449b22008-10-20 05:57:40 +0000340 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000341 }
Steve Naroff294494e2007-08-22 16:35:03 +0000342 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000343
344 // We break out of the big loop in two cases: when we see @end or when we see
345 // EOF. In the former case, eat the @end. In the later case, emit an error.
346 if (Tok.isObjCAtKeyword(tok::objc_end))
347 ConsumeToken(); // the "end" identifier
348 else
349 Diag(Tok, diag::err_objc_missing_end);
350
Chris Lattnera2449b22008-10-20 05:57:40 +0000351 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000352 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8a779312008-06-06 16:45:15 +0000353 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
354 allMethods.empty() ? 0 : &allMethods[0],
355 allMethods.size(),
356 allProperties.empty() ? 0 : &allProperties[0],
357 allProperties.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000358}
359
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000360/// Parse property attribute declarations.
361///
362/// property-attr-decl: '(' property-attrlist ')'
363/// property-attrlist:
364/// property-attribute
365/// property-attrlist ',' property-attribute
366/// property-attribute:
367/// getter '=' identifier
368/// setter '=' identifier ':'
369/// readonly
370/// readwrite
371/// assign
372/// retain
373/// copy
374/// nonatomic
375///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000376void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000377 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000378 SourceLocation LHSLoc = ConsumeParen(); // consume '('
379
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000380 while (1) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000381 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000382
383 // If this is not an identifier at all, bail out early.
384 if (II == 0) {
385 MatchRHSPunctuation(tok::r_paren, LHSLoc);
386 return;
387 }
388
Chris Lattner156b0612008-10-20 07:37:22 +0000389 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
390
Chris Lattner92e62b02008-11-20 04:42:34 +0000391 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000392 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000393 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000394 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000395 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000396 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000397 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000398 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000399 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000400 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000401 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000402 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000403 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattnere00da7c2008-10-20 07:39:53 +0000404 // getter/setter require extra treatment.
Chris Lattner156b0612008-10-20 07:37:22 +0000405 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
406 tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000407 return;
Chris Lattner156b0612008-10-20 07:37:22 +0000408
Chris Lattner8ca329c2008-10-20 07:24:39 +0000409 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000410 Diag(Tok, diag::err_expected_ident);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000411 SkipUntil(tok::r_paren);
412 return;
413 }
414
Chris Lattner5fd80fa2008-10-20 07:43:01 +0000415 if (II->getName()[0] == 's') {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000416 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
417 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000418 ConsumeToken(); // consume method name
419
420 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
421 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000422 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000423 } else {
424 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
425 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000426 ConsumeToken(); // consume method name
Chris Lattner8ca329c2008-10-20 07:24:39 +0000427 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000428 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000429 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000430 SkipUntil(tok::r_paren);
431 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000432 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000433
Chris Lattner156b0612008-10-20 07:37:22 +0000434 if (Tok.isNot(tok::comma))
435 break;
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000436
Chris Lattner156b0612008-10-20 07:37:22 +0000437 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000438 }
Chris Lattner156b0612008-10-20 07:37:22 +0000439
440 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000441}
442
Steve Naroff3536b442007-09-06 21:24:23 +0000443/// objc-method-proto:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000444/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000445/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000446///
447/// objc-instance-method: '-'
448/// objc-class-method: '+'
449///
Steve Naroff4985ace2007-08-22 18:35:33 +0000450/// objc-method-attributes: [OBJC2]
451/// __attribute__((deprecated))
452///
Fariborz Jahanian00933592007-09-18 00:25:23 +0000453Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000454 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000455 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000456
457 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000458 SourceLocation mLoc = ConsumeToken();
Steve Naroff294494e2007-08-22 16:35:03 +0000459
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000460 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000461 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000462 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000463 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000464}
465
466/// objc-selector:
467/// identifier
468/// one of
469/// enum struct union if else while do for switch case default
470/// break continue return goto asm sizeof typeof __alignof
471/// unsigned long const short volatile signed restrict _Complex
472/// in out inout bycopy byref oneway int char float double void _Bool
473///
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000474IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000475 switch (Tok.getKind()) {
476 default:
477 return 0;
478 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000479 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000480 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000481 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000482 case tok::kw_break:
483 case tok::kw_case:
484 case tok::kw_catch:
485 case tok::kw_char:
486 case tok::kw_class:
487 case tok::kw_const:
488 case tok::kw_const_cast:
489 case tok::kw_continue:
490 case tok::kw_default:
491 case tok::kw_delete:
492 case tok::kw_do:
493 case tok::kw_double:
494 case tok::kw_dynamic_cast:
495 case tok::kw_else:
496 case tok::kw_enum:
497 case tok::kw_explicit:
498 case tok::kw_export:
499 case tok::kw_extern:
500 case tok::kw_false:
501 case tok::kw_float:
502 case tok::kw_for:
503 case tok::kw_friend:
504 case tok::kw_goto:
505 case tok::kw_if:
506 case tok::kw_inline:
507 case tok::kw_int:
508 case tok::kw_long:
509 case tok::kw_mutable:
510 case tok::kw_namespace:
511 case tok::kw_new:
512 case tok::kw_operator:
513 case tok::kw_private:
514 case tok::kw_protected:
515 case tok::kw_public:
516 case tok::kw_register:
517 case tok::kw_reinterpret_cast:
518 case tok::kw_restrict:
519 case tok::kw_return:
520 case tok::kw_short:
521 case tok::kw_signed:
522 case tok::kw_sizeof:
523 case tok::kw_static:
524 case tok::kw_static_cast:
525 case tok::kw_struct:
526 case tok::kw_switch:
527 case tok::kw_template:
528 case tok::kw_this:
529 case tok::kw_throw:
530 case tok::kw_true:
531 case tok::kw_try:
532 case tok::kw_typedef:
533 case tok::kw_typeid:
534 case tok::kw_typename:
535 case tok::kw_typeof:
536 case tok::kw_union:
537 case tok::kw_unsigned:
538 case tok::kw_using:
539 case tok::kw_virtual:
540 case tok::kw_void:
541 case tok::kw_volatile:
542 case tok::kw_wchar_t:
543 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000544 case tok::kw__Bool:
545 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000546 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000547 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000548 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000549 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000550 }
Steve Naroff294494e2007-08-22 16:35:03 +0000551}
552
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000553/// objc-for-collection-in: 'in'
554///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000555bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000556 // FIXME: May have to do additional look-ahead to only allow for
557 // valid tokens following an 'in'; such as an identifier, unary operators,
558 // '[' etc.
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000559 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000560 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000561}
562
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000563/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000564/// qualifier list and builds their bitmask representation in the input
565/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000566///
567/// objc-type-qualifiers:
568/// objc-type-qualifier
569/// objc-type-qualifiers objc-type-qualifier
570///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000571void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000572 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000573 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000574 return;
575
576 const IdentifierInfo *II = Tok.getIdentifierInfo();
577 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000578 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000579 continue;
580
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000581 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000582 switch (i) {
583 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000584 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
585 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
586 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
587 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
588 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
589 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000590 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000591 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000592 ConsumeToken();
593 II = 0;
594 break;
595 }
596
597 // If this wasn't a recognized qualifier, bail out.
598 if (II) return;
599 }
600}
601
602/// objc-type-name:
603/// '(' objc-type-qualifiers[opt] type-name ')'
604/// '(' objc-type-qualifiers[opt] ')'
605///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000606Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000607 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff294494e2007-08-22 16:35:03 +0000608
Chris Lattner4a76b292008-10-22 03:52:06 +0000609 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000610 SourceLocation TypeStartLoc = Tok.getLocation();
Steve Naroff294494e2007-08-22 16:35:03 +0000611
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000612 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000613 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000614
Chris Lattner4a76b292008-10-22 03:52:06 +0000615 TypeTy *Ty = 0;
616 if (isTypeSpecifierQualifier())
Steve Narofff28b2642007-09-05 23:30:30 +0000617 Ty = ParseTypeName();
Chris Lattnere8904e92008-08-23 01:48:03 +0000618
Steve Naroffd7333c22008-10-21 14:15:04 +0000619 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000620 ConsumeParen();
621 else if (Tok.getLocation() == TypeStartLoc) {
622 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000623 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000624 SkipUntil(tok::r_paren);
625 } else {
626 // Otherwise, we found *something*, but didn't get a ')' in the right
627 // place. Emit an error then return what we have as the type.
628 MatchRHSPunctuation(tok::r_paren, LParenLoc);
629 }
Steve Narofff28b2642007-09-05 23:30:30 +0000630 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000631}
632
633/// objc-method-decl:
634/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000635/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000636/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000637/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000638///
639/// objc-keyword-selector:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000640/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000641/// objc-keyword-selector objc-keyword-decl
642///
643/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000644/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
645/// objc-selector ':' objc-keyword-attributes[opt] identifier
646/// ':' objc-type-name objc-keyword-attributes[opt] identifier
647/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000648///
Steve Naroff4985ace2007-08-22 18:35:33 +0000649/// objc-parmlist:
650/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000651///
Steve Naroff4985ace2007-08-22 18:35:33 +0000652/// objc-parms:
653/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000654///
Steve Naroff4985ace2007-08-22 18:35:33 +0000655/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000656/// , ...
657///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000658/// objc-keyword-attributes: [OBJC2]
659/// __attribute__((unused))
660///
Steve Naroffbef11852007-10-26 20:53:56 +0000661Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000662 tok::TokenKind mType,
663 DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000664 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff68d331a2007-09-27 14:38:14 +0000665{
Chris Lattnere8904e92008-08-23 01:48:03 +0000666 // Parse the return type if present.
Chris Lattnerff384912007-10-07 02:00:24 +0000667 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000668 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000669 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000670 ReturnType = ParseObjCTypeName(DSRet);
Chris Lattnere8904e92008-08-23 01:48:03 +0000671
Steve Naroffbef11852007-10-26 20:53:56 +0000672 SourceLocation selLoc;
673 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000674
675 if (!SelIdent) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000676 Diag(Tok, diag::err_expected_selector_for_method)
677 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000678 // Skip until we get a ; or {}.
679 SkipUntil(tok::r_brace);
680 return 0;
681 }
682
Chris Lattnerdf195262007-10-09 17:51:17 +0000683 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000684 // If attributes exist after the method, parse them.
685 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000686 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000687 MethodAttrs = ParseAttributes();
688
689 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroffbef11852007-10-26 20:53:56 +0000690 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000691 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000692 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerff384912007-10-07 02:00:24 +0000693 }
Steve Narofff28b2642007-09-05 23:30:30 +0000694
Steve Naroff68d331a2007-09-27 14:38:14 +0000695 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
696 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000697 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff68d331a2007-09-27 14:38:14 +0000698 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerff384912007-10-07 02:00:24 +0000699
700 Action::TypeTy *TypeInfo;
701 while (1) {
702 KeyIdents.push_back(SelIdent);
Steve Naroff68d331a2007-09-27 14:38:14 +0000703
Chris Lattnerff384912007-10-07 02:00:24 +0000704 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000705 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000706 Diag(Tok, diag::err_expected_colon);
707 break;
708 }
709 ConsumeToken(); // Eat the ':'.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000710 ObjCDeclSpec DSType;
Chris Lattnere8904e92008-08-23 01:48:03 +0000711 if (Tok.is(tok::l_paren)) // Parse the argument type.
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000712 TypeInfo = ParseObjCTypeName(DSType);
Chris Lattnerff384912007-10-07 02:00:24 +0000713 else
714 TypeInfo = 0;
715 KeyTypes.push_back(TypeInfo);
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000716 ArgTypeQuals.push_back(DSType);
Steve Narofff28b2642007-09-05 23:30:30 +0000717
Chris Lattnerff384912007-10-07 02:00:24 +0000718 // If attributes exist before the argument name, parse them.
Chris Lattnerdf195262007-10-09 17:51:17 +0000719 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000720 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000721
Chris Lattnerdf195262007-10-09 17:51:17 +0000722 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000723 Diag(Tok, diag::err_expected_ident); // missing argument name.
724 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000725 }
Chris Lattnerff384912007-10-07 02:00:24 +0000726 ArgNames.push_back(Tok.getIdentifierInfo());
727 ConsumeToken(); // Eat the identifier.
Steve Naroff29238a02007-10-05 18:42:47 +0000728
Chris Lattnerff384912007-10-07 02:00:24 +0000729 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000730 SourceLocation Loc;
731 SelIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000732 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000733 break;
734 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000735 }
Chris Lattnerff384912007-10-07 02:00:24 +0000736
Steve Naroff335eafa2007-11-15 12:35:21 +0000737 bool isVariadic = false;
738
Chris Lattnerff384912007-10-07 02:00:24 +0000739 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000740 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000741 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000742 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000743 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000744 ConsumeToken();
745 break;
746 }
Steve Naroff335eafa2007-11-15 12:35:21 +0000747 // FIXME: implement this...
Chris Lattnerff384912007-10-07 02:00:24 +0000748 // Parse the c-style argument declaration-specifier.
749 DeclSpec DS;
750 ParseDeclarationSpecifiers(DS);
751 // Parse the declarator.
752 Declarator ParmDecl(DS, Declarator::PrototypeContext);
753 ParseDeclarator(ParmDecl);
754 }
755
756 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000757 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000758 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000759 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000760 MethodAttrs = ParseAttributes();
761
762 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
763 &KeyIdents[0]);
Steve Naroffbef11852007-10-26 20:53:56 +0000764 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000765 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000766 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff335eafa2007-11-15 12:35:21 +0000767 &ArgNames[0], MethodAttrs,
768 MethodImplKind, isVariadic);
Steve Naroff294494e2007-08-22 16:35:03 +0000769}
770
Steve Naroffdac269b2007-08-20 21:31:48 +0000771/// objc-protocol-refs:
772/// '<' identifier-list '>'
773///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000774bool Parser::
Chris Lattnere13b9592008-07-26 04:03:38 +0000775ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclTy*> &Protocols,
776 bool WarnOnDeclarations, SourceLocation &EndLoc) {
777 assert(Tok.is(tok::less) && "expected <");
778
779 ConsumeToken(); // the "<"
780
781 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
782
783 while (1) {
784 if (Tok.isNot(tok::identifier)) {
785 Diag(Tok, diag::err_expected_ident);
786 SkipUntil(tok::greater);
787 return true;
788 }
789 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
790 Tok.getLocation()));
791 ConsumeToken();
792
793 if (Tok.isNot(tok::comma))
794 break;
795 ConsumeToken();
796 }
797
798 // Consume the '>'.
799 if (Tok.isNot(tok::greater)) {
800 Diag(Tok, diag::err_expected_greater);
801 return true;
802 }
803
804 EndLoc = ConsumeAnyToken();
805
806 // Convert the list of protocols identifiers into a list of protocol decls.
807 Actions.FindProtocolDeclaration(WarnOnDeclarations,
808 &ProtocolIdents[0], ProtocolIdents.size(),
809 Protocols);
810 return false;
811}
812
Steve Naroffdac269b2007-08-20 21:31:48 +0000813/// objc-class-instance-variables:
814/// '{' objc-instance-variable-decl-list[opt] '}'
815///
816/// objc-instance-variable-decl-list:
817/// objc-visibility-spec
818/// objc-instance-variable-decl ';'
819/// ';'
820/// objc-instance-variable-decl-list objc-visibility-spec
821/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
822/// objc-instance-variable-decl-list ';'
823///
824/// objc-visibility-spec:
825/// @private
826/// @protected
827/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000828/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000829///
830/// objc-instance-variable-decl:
831/// struct-declaration
832///
Steve Naroff60fccee2007-10-29 21:38:07 +0000833void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
834 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000835 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000836 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000837 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
838
Steve Naroffddbff782007-08-21 21:17:12 +0000839 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffddbff782007-08-21 21:17:12 +0000840
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000841 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000842 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000843 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000844 // Each iteration of this loop reads one objc-instance-variable-decl.
845
846 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000847 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000848 Diag(Tok, diag::ext_extra_struct_semi);
849 ConsumeToken();
850 continue;
851 }
Chris Lattnere1359422008-04-10 06:46:29 +0000852
Steve Naroffddbff782007-08-21 21:17:12 +0000853 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000854 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000855 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000856 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000857 case tok::objc_private:
858 case tok::objc_public:
859 case tok::objc_protected:
860 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000861 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000862 ConsumeToken();
863 continue;
864 default:
865 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000866 continue;
867 }
868 }
Chris Lattnere1359422008-04-10 06:46:29 +0000869
870 // Parse all the comma separated declarators.
871 DeclSpec DS;
872 FieldDeclarators.clear();
873 ParseStructDeclaration(DS, FieldDeclarators);
874
875 // Convert them all to fields.
876 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
877 FieldDeclarator &FD = FieldDeclarators[i];
878 // Install the declarator into interfaceDecl.
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000879 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattnere1359422008-04-10 06:46:29 +0000880 DS.getSourceRange().getBegin(),
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000881 FD.D, FD.BitfieldSize, visibility);
Chris Lattnere1359422008-04-10 06:46:29 +0000882 AllIvarDecls.push_back(Field);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000883 }
Steve Naroff3536b442007-09-06 21:24:23 +0000884
Chris Lattnerdf195262007-10-09 17:51:17 +0000885 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000886 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +0000887 } else {
888 Diag(Tok, diag::err_expected_semi_decl_list);
889 // Skip to end of block or statement
890 SkipUntil(tok::r_brace, true, true);
891 }
892 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000893 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000894 // Call ActOnFields() even if we don't have any decls. This is useful
895 // for code rewriting tools that need to be aware of the empty list.
896 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
897 &AllIvarDecls[0], AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +0000898 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +0000899 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000900}
Steve Naroffdac269b2007-08-20 21:31:48 +0000901
902/// objc-protocol-declaration:
903/// objc-protocol-definition
904/// objc-protocol-forward-reference
905///
906/// objc-protocol-definition:
907/// @protocol identifier
908/// objc-protocol-refs[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000909/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000910/// @end
911///
912/// objc-protocol-forward-reference:
913/// @protocol identifier-list ';'
914///
915/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000916/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000917/// semicolon in the first alternative if objc-protocol-refs are omitted.
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000918Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
919 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000920 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000921 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
922 ConsumeToken(); // the "protocol" identifier
923
Chris Lattnerdf195262007-10-09 17:51:17 +0000924 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000925 Diag(Tok, diag::err_expected_ident); // missing protocol name.
926 return 0;
927 }
928 // Save the protocol name, then consume it.
929 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
930 SourceLocation nameLoc = ConsumeToken();
931
Chris Lattnerdf195262007-10-09 17:51:17 +0000932 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000933 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000934 ConsumeToken();
Chris Lattner7caeabd2008-07-21 22:17:28 +0000935 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000936 }
Chris Lattner7caeabd2008-07-21 22:17:28 +0000937
Chris Lattnerdf195262007-10-09 17:51:17 +0000938 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000939 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
940 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
941
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000942 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000943 while (1) {
944 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +0000945 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000946 Diag(Tok, diag::err_expected_ident);
947 SkipUntil(tok::semi);
948 return 0;
949 }
Chris Lattner7caeabd2008-07-21 22:17:28 +0000950 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
951 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000952 ConsumeToken(); // the identifier
953
Chris Lattnerdf195262007-10-09 17:51:17 +0000954 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000955 break;
956 }
957 // Consume the ';'.
958 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
959 return 0;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000960
Steve Naroffe440eb82007-10-10 17:32:04 +0000961 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroff37e58d12007-10-02 22:39:18 +0000962 &ProtocolRefs[0],
963 ProtocolRefs.size());
Chris Lattner7caeabd2008-07-21 22:17:28 +0000964 }
965
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000966 // Last, and definitely not least, parse a protocol declaration.
Chris Lattnere13b9592008-07-26 04:03:38 +0000967 SourceLocation EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000968
Chris Lattnere13b9592008-07-26 04:03:38 +0000969 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000970 if (Tok.is(tok::less) &&
Chris Lattnere13b9592008-07-26 04:03:38 +0000971 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattner7caeabd2008-07-21 22:17:28 +0000972 return 0;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000973
Chris Lattnere13b9592008-07-26 04:03:38 +0000974 DeclTy *ProtoType =
975 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
976 &ProtocolRefs[0], ProtocolRefs.size(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000977 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000978 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000979 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +0000980}
Steve Naroffdac269b2007-08-20 21:31:48 +0000981
982/// objc-implementation:
983/// objc-class-implementation-prologue
984/// objc-category-implementation-prologue
985///
986/// objc-class-implementation-prologue:
987/// @implementation identifier objc-superclass[opt]
988/// objc-class-instance-variables[opt]
989///
990/// objc-category-implementation-prologue:
991/// @implementation identifier ( identifier )
992
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000993Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
994 SourceLocation atLoc) {
995 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
996 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
997 ConsumeToken(); // the "implementation" identifier
998
Chris Lattnerdf195262007-10-09 17:51:17 +0000999 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001000 Diag(Tok, diag::err_expected_ident); // missing class or category name.
1001 return 0;
1002 }
1003 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001004 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001005 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1006
Chris Lattnerdf195262007-10-09 17:51:17 +00001007 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001008 // we have a category implementation.
1009 SourceLocation lparenLoc = ConsumeParen();
1010 SourceLocation categoryLoc, rparenLoc;
1011 IdentifierInfo *categoryId = 0;
1012
Chris Lattnerdf195262007-10-09 17:51:17 +00001013 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001014 categoryId = Tok.getIdentifierInfo();
1015 categoryLoc = ConsumeToken();
1016 } else {
1017 Diag(Tok, diag::err_expected_ident); // missing category name.
1018 return 0;
1019 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001020 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001021 Diag(Tok, diag::err_expected_rparen);
1022 SkipUntil(tok::r_paren, false); // don't stop at ';'
1023 return 0;
1024 }
1025 rparenLoc = ConsumeParen();
Steve Naroffe440eb82007-10-10 17:32:04 +00001026 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001027 atLoc, nameId, nameLoc, categoryId,
1028 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001029 ObjCImpDecl = ImplCatType;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +00001030 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001031 }
1032 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001033 SourceLocation superClassLoc;
1034 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001035 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001036 // We have a super class
1037 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001038 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001039 Diag(Tok, diag::err_expected_ident); // missing super class name.
1040 return 0;
1041 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001042 superClassId = Tok.getIdentifierInfo();
1043 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001044 }
Steve Naroffe440eb82007-10-10 17:32:04 +00001045 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001046 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001047 superClassId, superClassLoc);
1048
Steve Naroff60fccee2007-10-29 21:38:07 +00001049 if (Tok.is(tok::l_brace)) // we have ivars
1050 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001051 ObjCImpDecl = ImplClsType;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001052
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +00001053 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001054}
Steve Naroff60fccee2007-10-29 21:38:07 +00001055
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001056Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1057 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1058 "ParseObjCAtEndDeclaration(): Expected @end");
1059 ConsumeToken(); // the "end" identifier
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001060 if (ObjCImpDecl)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001061 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001062 else
1063 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001064 return ObjCImpDecl;
Steve Naroffdac269b2007-08-20 21:31:48 +00001065}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001066
1067/// compatibility-alias-decl:
1068/// @compatibility_alias alias-name class-name ';'
1069///
1070Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1071 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1072 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1073 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001074 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001075 Diag(Tok, diag::err_expected_ident);
1076 return 0;
1077 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001078 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1079 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001080 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001081 Diag(Tok, diag::err_expected_ident);
1082 return 0;
1083 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001084 IdentifierInfo *classId = Tok.getIdentifierInfo();
1085 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1086 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001087 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001088 return 0;
1089 }
1090 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1091 aliasId, aliasLoc,
1092 classId, classLoc);
1093 return ClsType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001094}
1095
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001096/// property-synthesis:
1097/// @synthesize property-ivar-list ';'
1098///
1099/// property-ivar-list:
1100/// property-ivar
1101/// property-ivar-list ',' property-ivar
1102///
1103/// property-ivar:
1104/// identifier
1105/// identifier '=' identifier
1106///
1107Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1108 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1109 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001110 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnerdf195262007-10-09 17:51:17 +00001111 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001112 Diag(Tok, diag::err_expected_ident);
1113 return 0;
1114 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001115 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001116 IdentifierInfo *propertyIvar = 0;
1117 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1118 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001119 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001120 // property '=' ivar-name
1121 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +00001122 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001123 Diag(Tok, diag::err_expected_ident);
1124 break;
1125 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001126 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001127 ConsumeToken(); // consume ivar-name
1128 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001129 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1130 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001131 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001132 break;
1133 ConsumeToken(); // consume ','
1134 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001135 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001136 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001137 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001138}
1139
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001140/// property-dynamic:
1141/// @dynamic property-list
1142///
1143/// property-list:
1144/// identifier
1145/// property-list ',' identifier
1146///
1147Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1148 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1149 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1150 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001151 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001152 Diag(Tok, diag::err_expected_ident);
1153 return 0;
1154 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001155 while (Tok.is(tok::identifier)) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001156 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1157 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1158 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1159 propertyId, 0);
1160
Chris Lattnerdf195262007-10-09 17:51:17 +00001161 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001162 break;
1163 ConsumeToken(); // consume ','
1164 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001165 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001166 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001167 return 0;
1168}
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001169
1170/// objc-throw-statement:
1171/// throw expression[opt];
1172///
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001173Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1174 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001175 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001176 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001177 Res = ParseExpression();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001178 if (Res.isInvalid) {
1179 SkipUntil(tok::semi);
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001180 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001181 }
1182 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001183 ConsumeToken(); // consume ';'
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001184 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001185}
1186
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001187/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001188/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001189///
1190Parser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001191 ConsumeToken(); // consume synchronized
1192 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001193 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001194 return true;
1195 }
1196 ConsumeParen(); // '('
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001197 ExprResult Res = ParseExpression();
Sebastian Redla55e52c2008-11-25 22:21:31 +00001198 ExprGuard ResGuard(Actions, Res);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001199 if (Res.isInvalid) {
1200 SkipUntil(tok::semi);
1201 return true;
1202 }
1203 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001204 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001205 return true;
1206 }
1207 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001208 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001209 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001210 return true;
1211 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001212 // Enter a scope to hold everything within the compound stmt. Compound
1213 // statements can always hold declarations.
1214 EnterScope(Scope::DeclScope);
1215
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001216 StmtResult SynchBody = ParseCompoundStatementBody();
Steve Naroff3ac438c2008-06-04 20:36:13 +00001217
1218 ExitScope();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001219 if (SynchBody.isInvalid)
1220 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001221 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, ResGuard.take(),
1222 SynchBody.Val);
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001223}
1224
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001225/// objc-try-catch-statement:
1226/// @try compound-statement objc-catch-list[opt]
1227/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1228///
1229/// objc-catch-list:
1230/// @catch ( parameter-declaration ) compound-statement
1231/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1232/// catch-parameter-declaration:
1233/// parameter-declaration
1234/// '...' [OBJC2]
1235///
Chris Lattner6b884502008-03-10 06:06:04 +00001236Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001237 bool catch_or_finally_seen = false;
Steve Naroff64515f32008-02-05 21:27:35 +00001238
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001239 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001240 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001241 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanianb210bd02007-11-01 21:12:44 +00001242 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001243 }
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001244 StmtResult CatchStmts;
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001245 StmtResult FinallyStmt;
Ted Kremenek3527b592008-09-26 17:32:47 +00001246 EnterScope(Scope::DeclScope);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001247 StmtResult TryBody = ParseCompoundStatementBody();
Ted Kremenek3527b592008-09-26 17:32:47 +00001248 ExitScope();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001249 if (TryBody.isInvalid)
1250 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001251 ExprGuard TryGuard(Actions, TryBody);
1252 ExprGuard CatchGuard(Actions), FinallyGuard(Actions);
1253
Chris Lattnerdf195262007-10-09 17:51:17 +00001254 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001255 // At this point, we need to lookahead to determine if this @ is the start
1256 // of an @catch or @finally. We don't want to consume the @ token if this
1257 // is an @try or @encode or something else.
1258 Token AfterAt = GetLookAheadToken(1);
1259 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1260 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1261 break;
1262
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001263 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001264 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001265 StmtTy *FirstPart = 0;
1266 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001267 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001268 ConsumeParen();
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001269 EnterScope(Scope::DeclScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001270 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001271 DeclSpec DS;
1272 ParseDeclarationSpecifiers(DS);
Steve Naroff459a1e22008-06-03 05:36:54 +00001273 // For some odd reason, the name of the exception variable is
1274 // optional. As a result, we need to use PrototypeContext.
1275 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001276 ParseDeclarator(DeclaratorInfo);
Steve Naroff459a1e22008-06-03 05:36:54 +00001277 if (DeclaratorInfo.getIdentifier()) {
1278 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
Daniel Dunbar914701e2008-08-05 16:28:08 +00001279 DeclaratorInfo, 0);
Steve Naroff459a1e22008-06-03 05:36:54 +00001280 StmtResult stmtResult =
1281 Actions.ActOnDeclStmt(aBlockVarDecl,
1282 DS.getSourceRange().getBegin(),
1283 DeclaratorInfo.getSourceRange().getEnd());
1284 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
1285 }
Steve Naroff64515f32008-02-05 21:27:35 +00001286 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001287 ConsumeToken(); // consume '...'
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001288 SourceLocation RParenLoc = ConsumeParen();
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001289
1290 StmtResult CatchBody(true);
1291 if (Tok.is(tok::l_brace))
1292 CatchBody = ParseCompoundStatementBody();
1293 else
1294 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001295 if (CatchBody.isInvalid)
1296 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001297 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001298 FirstPart, CatchBody.Val, CatchGuard.take());
1299 CatchGuard.reset(CatchStmts);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001300 ExitScope();
Steve Naroff64515f32008-02-05 21:27:35 +00001301 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001302 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1303 << "@catch clause";
Fariborz Jahanianb210bd02007-11-01 21:12:44 +00001304 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001305 }
1306 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001307 } else {
1308 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001309 ConsumeToken(); // consume finally
Ted Kremenekaefc3662008-09-26 00:31:16 +00001310 EnterScope(Scope::DeclScope);
1311
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001312
1313 StmtResult FinallyBody(true);
1314 if (Tok.is(tok::l_brace))
1315 FinallyBody = ParseCompoundStatementBody();
1316 else
1317 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001318 if (FinallyBody.isInvalid)
1319 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001320 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001321 FinallyBody.Val);
Sebastian Redla55e52c2008-11-25 22:21:31 +00001322 FinallyGuard.reset(FinallyStmt);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001323 catch_or_finally_seen = true;
Ted Kremenekaefc3662008-09-26 00:31:16 +00001324 ExitScope();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001325 break;
1326 }
1327 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001328 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001329 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001330 return true;
1331 }
Sebastian Redla55e52c2008-11-25 22:21:31 +00001332 return Actions.ActOnObjCAtTryStmt(atLoc, TryGuard.take(), CatchGuard.take(),
1333 FinallyGuard.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001334}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001335
Steve Naroff3536b442007-09-06 21:24:23 +00001336/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001337///
Steve Naroff71c0a952007-11-13 23:01:27 +00001338Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001339 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001340 // parse optional ';'
Chris Lattnerdf195262007-10-09 17:51:17 +00001341 if (Tok.is(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001342 ConsumeToken();
1343
Steve Naroff409be832007-11-11 19:54:21 +00001344 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001345 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001346 Diag(Tok, diag::err_expected_method_body);
Steve Naroff409be832007-11-11 19:54:21 +00001347
1348 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1349 SkipUntil(tok::l_brace, true, true);
1350
1351 // If we didn't find the '{', bail out.
1352 if (Tok.isNot(tok::l_brace))
Steve Naroff71c0a952007-11-13 23:01:27 +00001353 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001354 }
Steve Naroff409be832007-11-11 19:54:21 +00001355 SourceLocation BraceLoc = Tok.getLocation();
1356
1357 // Enter a scope for the method body.
1358 EnterScope(Scope::FnScope|Scope::DeclScope);
1359
1360 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001361 // specified Declarator for the method.
1362 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Steve Naroff409be832007-11-11 19:54:21 +00001363
1364 StmtResult FnBody = ParseCompoundStatementBody();
1365
1366 // If the function body could not be parsed, make a bogus compoundstmt.
1367 if (FnBody.isInvalid)
1368 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1369
1370 // Leave the function body scope.
1371 ExitScope();
1372
1373 // TODO: Pass argument information.
Steve Naroff394f3f42008-07-25 17:57:26 +00001374 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff71c0a952007-11-13 23:01:27 +00001375 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001376}
Anders Carlsson55085182007-08-21 17:43:55 +00001377
Steve Naroff64515f32008-02-05 21:27:35 +00001378Parser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
1379 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001380 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001381 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1382 return ParseObjCThrowStmt(AtLoc);
1383 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1384 return ParseObjCSynchronizedStmt(AtLoc);
1385 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
1386 if (Res.isInvalid) {
1387 // If the expression is invalid, skip ahead to the next semicolon. Not
1388 // doing this opens us up to the possibility of infinite loops if
1389 // ParseExpression does not consume any tokens.
1390 SkipUntil(tok::semi);
1391 return true;
1392 }
1393 // Otherwise, eat the semicolon.
1394 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1395 return Actions.ActOnExprStmt(Res.Val);
1396}
1397
Steve Naroffa642beb2007-10-15 20:55:58 +00001398Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001399 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001400 case tok::string_literal: // primary-expression: string-literal
1401 case tok::wide_string_literal:
1402 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1403 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001404 if (Tok.getIdentifierInfo() == 0)
1405 return Diag(AtLoc, diag::err_unexpected_at);
1406
1407 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1408 case tok::objc_encode:
1409 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1410 case tok::objc_protocol:
1411 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
1412 case tok::objc_selector:
1413 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
1414 default:
Chris Lattner1ab3b962008-11-18 07:48:38 +00001415 return Diag(AtLoc, diag::err_unexpected_at);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001416 }
Anders Carlsson55085182007-08-21 17:43:55 +00001417 }
Anders Carlsson55085182007-08-21 17:43:55 +00001418}
1419
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001420/// objc-message-expr:
1421/// '[' objc-receiver objc-message-args ']'
1422///
1423/// objc-receiver:
1424/// expression
1425/// class-name
1426/// type-name
Chris Lattner699b6612008-01-25 18:59:06 +00001427Parser::ExprResult Parser::ParseObjCMessageExpression() {
1428 assert(Tok.is(tok::l_square) && "'[' expected");
1429 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1430
1431 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001432 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001433 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Steve Naroff5cb93b82008-11-19 15:54:23 +00001434 SourceLocation NameLoc = ConsumeToken();
1435 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName, 0);
Chris Lattner699b6612008-01-25 18:59:06 +00001436 }
1437
Chris Lattner69d349a2008-09-19 17:44:00 +00001438 ExprResult Res = ParseExpression();
Chris Lattner699b6612008-01-25 18:59:06 +00001439 if (Res.isInvalid) {
Chris Lattner5c749422008-01-25 19:43:26 +00001440 SkipUntil(tok::r_square);
Chris Lattner699b6612008-01-25 18:59:06 +00001441 return Res;
1442 }
Chris Lattner4fef81d2008-08-05 06:19:09 +00001443
Steve Naroff5cb93b82008-11-19 15:54:23 +00001444 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), 0, Res.Val);
Chris Lattner699b6612008-01-25 18:59:06 +00001445}
1446
1447/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1448/// the rest of a message expression.
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001449///
1450/// objc-message-args:
1451/// objc-selector
1452/// objc-keywordarg-list
1453///
1454/// objc-keywordarg-list:
1455/// objc-keywordarg
1456/// objc-keywordarg-list objc-keywordarg
1457///
1458/// objc-keywordarg:
1459/// selector-name[opt] ':' objc-keywordexpr
1460///
1461/// objc-keywordexpr:
1462/// nonempty-expr-list
1463///
1464/// nonempty-expr-list:
1465/// assignment-expression
1466/// nonempty-expr-list , assignment-expression
1467///
Chris Lattner699b6612008-01-25 18:59:06 +00001468Parser::ExprResult
1469Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +00001470 SourceLocation NameLoc,
Chris Lattner699b6612008-01-25 18:59:06 +00001471 IdentifierInfo *ReceiverName,
1472 ExprTy *ReceiverExpr) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001473 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001474 SourceLocation Loc;
1475 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001476
1477 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001478 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00001479
Chris Lattnerdf195262007-10-09 17:51:17 +00001480 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001481 while (1) {
1482 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001483 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001484
Chris Lattnerdf195262007-10-09 17:51:17 +00001485 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001486 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001487 // We must manually skip to a ']', otherwise the expression skipper will
1488 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1489 // the enclosing expression.
1490 SkipUntil(tok::r_square);
Steve Naroff37387c92007-09-17 20:25:27 +00001491 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001492 }
Chris Lattner4fef81d2008-08-05 06:19:09 +00001493
Steve Naroff68d331a2007-09-27 14:38:14 +00001494 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001495 /// Parse the expression after ':'
Steve Naroff37387c92007-09-17 20:25:27 +00001496 ExprResult Res = ParseAssignmentExpression();
1497 if (Res.isInvalid) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001498 // We must manually skip to a ']', otherwise the expression skipper will
1499 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1500 // the enclosing expression.
1501 SkipUntil(tok::r_square);
Steve Naroff37387c92007-09-17 20:25:27 +00001502 return Res;
1503 }
Chris Lattner4fef81d2008-08-05 06:19:09 +00001504
Steve Naroff37387c92007-09-17 20:25:27 +00001505 // We have a valid expression.
Steve Naroff68d331a2007-09-27 14:38:14 +00001506 KeyExprs.push_back(Res.Val);
Steve Naroff37387c92007-09-17 20:25:27 +00001507
1508 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001509 selIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001510 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001511 break;
1512 // We have a selector or a colon, continue parsing.
1513 }
1514 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001515 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001516 ConsumeToken(); // Eat the ','.
1517 /// Parse the expression after ','
1518 ExprResult Res = ParseAssignmentExpression();
1519 if (Res.isInvalid) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001520 // We must manually skip to a ']', otherwise the expression skipper will
1521 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1522 // the enclosing expression.
1523 SkipUntil(tok::r_square);
Steve Naroff49f109c2007-11-15 13:05:42 +00001524 return Res;
1525 }
Chris Lattner4fef81d2008-08-05 06:19:09 +00001526
Steve Naroff49f109c2007-11-15 13:05:42 +00001527 // We have a valid expression.
1528 KeyExprs.push_back(Res.Val);
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001529 }
1530 } else if (!selIdent) {
1531 Diag(Tok, diag::err_expected_ident); // missing selector name.
Chris Lattner4fef81d2008-08-05 06:19:09 +00001532
1533 // We must manually skip to a ']', otherwise the expression skipper will
1534 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1535 // the enclosing expression.
1536 SkipUntil(tok::r_square);
Fariborz Jahanian0ba0aa12008-01-02 18:09:46 +00001537 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001538 }
Chris Lattnerff384912007-10-07 02:00:24 +00001539
Chris Lattnerdf195262007-10-09 17:51:17 +00001540 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001541 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001542 // We must manually skip to a ']', otherwise the expression skipper will
1543 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1544 // the enclosing expression.
1545 SkipUntil(tok::r_square);
Fariborz Jahanian0ba0aa12008-01-02 18:09:46 +00001546 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001547 }
Chris Lattner4fef81d2008-08-05 06:19:09 +00001548
Chris Lattner699b6612008-01-25 18:59:06 +00001549 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Steve Naroff37387c92007-09-17 20:25:27 +00001550
Steve Naroff29238a02007-10-05 18:42:47 +00001551 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001552 if (nKeys == 0)
1553 KeyIdents.push_back(selIdent);
1554 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1555
1556 // We've just parsed a keyword message.
Steve Naroff37387c92007-09-17 20:25:27 +00001557 if (ReceiverName)
Fariborz Jahanian0523aaf2007-11-12 20:13:27 +00001558 return Actions.ActOnClassMessage(CurScope,
Steve Naroff5cb93b82008-11-19 15:54:23 +00001559 ReceiverName, Sel,
1560 LBracLoc, NameLoc, RBracLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001561 KeyExprs.take(), KeyExprs.size());
Chris Lattner699b6612008-01-25 18:59:06 +00001562 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001563 KeyExprs.take(), KeyExprs.size());
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001564}
1565
Steve Naroffbeaf2992007-11-03 11:27:19 +00001566Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001567 ExprResult Res = ParseStringLiteralExpression();
Anders Carlsson55085182007-08-21 17:43:55 +00001568 if (Res.isInvalid) return Res;
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001569
1570 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1571 // expressions. At this point, we know that the only valid thing that starts
1572 // with '@' is an @"".
1573 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001574 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001575 AtLocs.push_back(AtLoc);
1576 AtStrings.push_back(Res.Val);
1577
1578 while (Tok.is(tok::at)) {
1579 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001580
Sebastian Redla55e52c2008-11-25 22:21:31 +00001581 ExprResult Lit(true); // Invalid unless there is a string literal.
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001582 if (isTokenStringLiteral())
Sebastian Redla55e52c2008-11-25 22:21:31 +00001583 Lit = ParseStringLiteralExpression();
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001584 else
1585 Diag(Tok, diag::err_objc_concat_string);
1586
Sebastian Redla55e52c2008-11-25 22:21:31 +00001587 if (Lit.isInvalid)
1588 return Lit;
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001589
Sebastian Redla55e52c2008-11-25 22:21:31 +00001590 AtStrings.push_back(Lit.Val);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001591 }
Fariborz Jahanian79a99f22007-12-12 23:55:49 +00001592
Sebastian Redla55e52c2008-11-25 22:21:31 +00001593 return Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001594 AtStrings.size());
Anders Carlsson55085182007-08-21 17:43:55 +00001595}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001596
1597/// objc-encode-expression:
1598/// @encode ( type-name )
Chris Lattner674af952007-10-16 22:51:17 +00001599Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001600 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001601
1602 SourceLocation EncLoc = ConsumeToken();
1603
Chris Lattner4fef81d2008-08-05 06:19:09 +00001604 if (Tok.isNot(tok::l_paren))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001605 return Diag(Tok, diag::err_expected_lparen_after) << "@encode";
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001606
1607 SourceLocation LParenLoc = ConsumeParen();
1608
1609 TypeTy *Ty = ParseTypeName();
1610
Anders Carlsson4988ae32007-08-23 15:31:37 +00001611 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001612
Chris Lattner674af952007-10-16 22:51:17 +00001613 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson4988ae32007-08-23 15:31:37 +00001614 RParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001615}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001616
1617/// objc-protocol-expression
1618/// @protocol ( protocol-name )
Chris Lattner1ab3b962008-11-18 07:48:38 +00001619Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001620 SourceLocation ProtoLoc = ConsumeToken();
1621
Chris Lattner4fef81d2008-08-05 06:19:09 +00001622 if (Tok.isNot(tok::l_paren))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001623 return Diag(Tok, diag::err_expected_lparen_after) << "@protocol";
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001624
1625 SourceLocation LParenLoc = ConsumeParen();
1626
Chris Lattner4fef81d2008-08-05 06:19:09 +00001627 if (Tok.isNot(tok::identifier))
1628 return Diag(Tok, diag::err_expected_ident);
1629
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001630 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001631 ConsumeToken();
1632
Anders Carlsson4988ae32007-08-23 15:31:37 +00001633 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001634
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001635 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1636 LParenLoc, RParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001637}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001638
1639/// objc-selector-expression
1640/// @selector '(' objc-keyword-selector ')'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001641Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001642 SourceLocation SelectorLoc = ConsumeToken();
1643
Chris Lattner4fef81d2008-08-05 06:19:09 +00001644 if (Tok.isNot(tok::l_paren))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001645 return Diag(Tok, diag::err_expected_lparen_after) << "@selector";
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001646
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001647 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001648 SourceLocation LParenLoc = ConsumeParen();
1649 SourceLocation sLoc;
1650 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001651 if (!SelIdent && Tok.isNot(tok::colon))
1652 return Diag(Tok, diag::err_expected_ident); // missing selector name.
1653
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001654 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001655 unsigned nColons = 0;
1656 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001657 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001658 if (Tok.isNot(tok::colon))
1659 return Diag(Tok, diag::err_expected_colon);
1660
Chris Lattnercb53b362007-12-27 19:57:00 +00001661 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001662 ConsumeToken(); // Eat the ':'.
1663 if (Tok.is(tok::r_paren))
1664 break;
1665 // Check for another keyword selector.
1666 SourceLocation Loc;
1667 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001668 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001669 if (!SelIdent && Tok.isNot(tok::colon))
1670 break;
1671 }
Steve Naroff887407e2007-12-05 22:21:29 +00001672 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001673 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001674 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001675 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001676 RParenLoc);
Gabor Greif58065b22007-10-19 15:38:32 +00001677 }