blob: 65bd79d6b4f2ebac8c82b46a21ea5e131315d556 [file] [log] [blame]
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerda59c2f2006-11-05 02:08: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 Narofff1bc45b2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Fariborz Jahanian9e63b982007-11-01 23:59:59 +000016#include "clang/Parse/Scope.h"
Chris Lattner60f36222009-01-29 05:15:15 +000017#include "clang/Parse/ParseDiagnostic.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000018#include "llvm/ADT/SmallVector.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000019using namespace clang;
20
21
Chris Lattner3a907162008-12-08 21:53:24 +000022/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000023/// external-declaration: [C99 6.9]
24/// [OBJC] objc-class-definition
Steve Naroffe0933392007-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 Lattner83f095c2009-03-28 19:18:32 +000030Parser::DeclPtrTy Parser::ParseObjCAtDirectives() {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000031 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump11289f42009-09-09 15:08:12 +000032
Steve Naroff7c348172007-08-23 18:16:40 +000033 switch (Tok.getObjCKeywordID()) {
Chris Lattnerce90ef52008-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 Lattner83f095c2009-03-28 19:18:32 +000053 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +000054 }
55}
56
57///
Mike Stump11289f42009-09-09 15:08:12 +000058/// objc-class-declaration:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000059/// '@' 'class' identifier-list ';'
Mike Stump11289f42009-09-09 15:08:12 +000060///
Chris Lattner83f095c2009-03-28 19:18:32 +000061Parser::DeclPtrTy Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000062 ConsumeToken(); // the identifier "class"
Chris Lattner23b7eb62007-06-15 23:05:46 +000063 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
Ted Kremeneka26da852009-11-17 23:12:20 +000064 llvm::SmallVector<SourceLocation, 8> ClassLocs;
65
Mike Stump11289f42009-09-09 15:08:12 +000066
Chris Lattnerda59c2f2006-11-05 02:08:13 +000067 while (1) {
Chris Lattner0ef13522007-10-09 17:51:17 +000068 if (Tok.isNot(tok::identifier)) {
Chris Lattnerbd638922006-11-10 05:19:25 +000069 Diag(Tok, diag::err_expected_ident);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000070 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +000071 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +000072 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +000073 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +000074 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +000075 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +000076
Chris Lattner0ef13522007-10-09 17:51:17 +000077 if (Tok.isNot(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +000078 break;
Mike Stump11289f42009-09-09 15:08:12 +000079
Chris Lattnerda59c2f2006-11-05 02:08:13 +000080 ConsumeToken();
81 }
Mike Stump11289f42009-09-09 15:08:12 +000082
Chris Lattnerda59c2f2006-11-05 02:08:13 +000083 // Consume the ';'.
84 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Chris Lattner83f095c2009-03-28 19:18:32 +000085 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +000086
Ted Kremeneka26da852009-11-17 23:12:20 +000087 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
88 ClassLocs.data(),
89 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +000090}
91
Steve Naroff1eb1ad62007-08-20 21:31:48 +000092///
93/// objc-interface:
94/// objc-class-interface-attributes[opt] objc-class-interface
95/// objc-category-interface
96///
97/// objc-class-interface:
Mike Stump11289f42009-09-09 15:08:12 +000098/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +000099/// objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000100/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000101/// objc-interface-decl-list
102/// @end
103///
104/// objc-category-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000105/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000106/// objc-protocol-refs[opt]
107/// objc-interface-decl-list
108/// @end
109///
110/// objc-superclass:
111/// ':' identifier
112///
113/// objc-class-interface-attributes:
114/// __attribute__((visibility("default")))
115/// __attribute__((visibility("hidden")))
116/// __attribute__((deprecated))
117/// __attribute__((unavailable))
118/// __attribute__((objc_exception)) - used by NSException on 64-bit
119///
Chris Lattner83f095c2009-03-28 19:18:32 +0000120Parser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration(
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000121 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff7c348172007-08-23 18:16:40 +0000122 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000123 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
124 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000125
Chris Lattner0ef13522007-10-09 17:51:17 +0000126 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000127 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000128 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000129 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000130
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000131 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000132 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000133 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000134
Chris Lattner0ef13522007-10-09 17:51:17 +0000135 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000136 SourceLocation lparenLoc = ConsumeParen();
137 SourceLocation categoryLoc, rparenLoc;
138 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000139
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000140 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000141 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000142 categoryId = Tok.getIdentifierInfo();
143 categoryLoc = ConsumeToken();
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000144 } else if (!getLang().ObjC2) {
145 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000146 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000147 }
Chris Lattner0ef13522007-10-09 17:51:17 +0000148 if (Tok.isNot(tok::r_paren)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000149 Diag(Tok, diag::err_expected_rparen);
150 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner83f095c2009-03-28 19:18:32 +0000151 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000152 }
153 rparenLoc = ConsumeParen();
Mike Stump11289f42009-09-09 15:08:12 +0000154
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000155 // Next, we need to check for any protocol references.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000156 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000157 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000158 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerb1f3c942008-07-26 04:07:02 +0000159 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000160 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
161 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +0000162 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +0000163
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000164 if (attrList) // categories don't support attributes.
165 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump11289f42009-09-09 15:08:12 +0000166
Jay Foad7d0479f2009-05-21 09:52:38 +0000167 DeclPtrTy CategoryType =
Mike Stump11289f42009-09-09 15:08:12 +0000168 Actions.ActOnStartCategoryInterface(atLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000169 nameId, nameLoc,
170 categoryId, categoryLoc,
171 ProtocolRefs.data(),
172 ProtocolRefs.size(),
173 EndProtoLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000174
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000175 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000176 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000177 }
178 // Parse a class interface.
179 IdentifierInfo *superClassId = 0;
180 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000181
Chris Lattner0ef13522007-10-09 17:51:17 +0000182 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000183 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +0000184 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000185 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000186 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000187 }
188 superClassId = Tok.getIdentifierInfo();
189 superClassLoc = ConsumeToken();
190 }
191 // Next, we need to check for any protocol references.
Chris Lattner83f095c2009-03-28 19:18:32 +0000192 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000193 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
194 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000195 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000196 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
197 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +0000198 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +0000199
200 DeclPtrTy ClsType =
201 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000202 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000203 ProtocolRefs.data(), ProtocolRefs.size(),
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000204 EndProtoLoc, attrList);
Mike Stump11289f42009-09-09 15:08:12 +0000205
Chris Lattner0ef13522007-10-09 17:51:17 +0000206 if (Tok.is(tok::l_brace))
Steve Naroff33a1e802007-10-29 21:38:07 +0000207 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000208
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000209 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000210 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000211}
212
213/// objc-interface-decl-list:
214/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000215/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000216/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000217/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000218/// objc-interface-decl-list declaration
219/// objc-interface-decl-list ';'
220///
Steve Naroff99264b42007-08-22 16:35:03 +0000221/// objc-method-requirement: [OBJC2]
222/// @required
223/// @optional
224///
Chris Lattner83f095c2009-03-28 19:18:32 +0000225void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattner5e530bc2007-12-27 19:57:00 +0000226 tok::ObjCKeywordKind contextKey) {
Chris Lattner83f095c2009-03-28 19:18:32 +0000227 llvm::SmallVector<DeclPtrTy, 32> allMethods;
228 llvm::SmallVector<DeclPtrTy, 16> allProperties;
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000229 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000230 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000231
Chris Lattnerda9fb152008-10-20 06:10:06 +0000232 SourceLocation AtEndLoc;
233
Steve Naroff99264b42007-08-22 16:35:03 +0000234 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000235 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000236 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Mike Stump11289f42009-09-09 15:08:12 +0000237 DeclPtrTy methodPrototype =
Chris Lattner0ef13522007-10-09 17:51:17 +0000238 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000239 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000240 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
241 // method definitions.
Chris Lattner8510b902009-02-15 22:24:30 +0000242 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
243 "", tok::semi);
Steve Naroff99264b42007-08-22 16:35:03 +0000244 continue;
245 }
Mike Stump11289f42009-09-09 15:08:12 +0000246
Chris Lattner038a3e32008-10-20 05:46:22 +0000247 // Ignore excess semicolons.
248 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000249 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000250 continue;
251 }
Mike Stump11289f42009-09-09 15:08:12 +0000252
Chris Lattnerda9fb152008-10-20 06:10:06 +0000253 // If we got to the end of the file, exit the loop.
Chris Lattner038a3e32008-10-20 05:46:22 +0000254 if (Tok.is(tok::eof))
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000255 break;
Mike Stump11289f42009-09-09 15:08:12 +0000256
Chris Lattner038a3e32008-10-20 05:46:22 +0000257 // If we don't have an @ directive, parse it as a function definition.
258 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000259 // The code below does not consume '}'s because it is afraid of eating the
260 // end of a namespace. Because of the way this code is structured, an
261 // erroneous r_brace would cause an infinite loop if not handled here.
262 if (Tok.is(tok::r_brace))
263 break;
Mike Stump11289f42009-09-09 15:08:12 +0000264
Steve Narofff1bc45b2007-08-22 18:35:33 +0000265 // FIXME: as the name implies, this rule allows function definitions.
266 // We could pass a flag or check for functions during semantic analysis.
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000267 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition());
Chris Lattner038a3e32008-10-20 05:46:22 +0000268 continue;
269 }
Mike Stump11289f42009-09-09 15:08:12 +0000270
Chris Lattner038a3e32008-10-20 05:46:22 +0000271 // Otherwise, we have an @ directive, eat the @.
272 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000273 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000274
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000275 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattner038a3e32008-10-20 05:46:22 +0000276 AtEndLoc = AtLoc;
277 break;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000278 }
Mike Stump11289f42009-09-09 15:08:12 +0000279
Chris Lattnerda9fb152008-10-20 06:10:06 +0000280 // Eat the identifier.
281 ConsumeToken();
282
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000283 switch (DirectiveKind) {
284 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000285 // FIXME: If someone forgets an @end on a protocol, this loop will
286 // continue to eat up tons of stuff and spew lots of nonsense errors. It
287 // would probably be better to bail out if we saw an @class or @interface
288 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000289 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000290 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000291 SkipUntil(tok::r_brace, tok::at);
292 break;
Mike Stump11289f42009-09-09 15:08:12 +0000293
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000294 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000295 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000296 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000297 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000298 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000299 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000300 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000301 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000302 break;
Mike Stump11289f42009-09-09 15:08:12 +0000303
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000304 case tok::objc_property:
Chris Lattner76619232008-10-20 07:22:18 +0000305 if (!getLang().ObjC2)
306 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
307
Chris Lattner038a3e32008-10-20 05:46:22 +0000308 ObjCDeclSpec OCDS;
Mike Stump11289f42009-09-09 15:08:12 +0000309 // Parse property attribute list, if any.
Chris Lattnerbeca7702008-10-20 07:24:39 +0000310 if (Tok.is(tok::l_paren))
Chris Lattner038a3e32008-10-20 05:46:22 +0000311 ParseObjCPropertyAttribute(OCDS);
Mike Stump11289f42009-09-09 15:08:12 +0000312
John McCallcfefb6d2009-11-03 02:38:08 +0000313 struct ObjCPropertyCallback : FieldCallback {
314 Parser &P;
315 DeclPtrTy IDecl;
316 llvm::SmallVectorImpl<DeclPtrTy> &Props;
317 ObjCDeclSpec &OCDS;
318 SourceLocation AtLoc;
319 tok::ObjCKeywordKind MethodImplKind;
320
321 ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
322 llvm::SmallVectorImpl<DeclPtrTy> &Props,
323 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
324 tok::ObjCKeywordKind MethodImplKind) :
325 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
326 MethodImplKind(MethodImplKind) {
327 }
328
329 DeclPtrTy invoke(FieldDeclarator &FD) {
330 if (FD.D.getIdentifier() == 0) {
331 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
332 << FD.D.getSourceRange();
333 return DeclPtrTy();
334 }
335 if (FD.BitfieldSize) {
336 P.Diag(AtLoc, diag::err_objc_property_bitfield)
337 << FD.D.getSourceRange();
338 return DeclPtrTy();
339 }
340
341 // Install the property declarator into interfaceDecl.
342 IdentifierInfo *SelName =
343 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
344
345 Selector GetterSel =
346 P.PP.getSelectorTable().getNullarySelector(SelName);
347 IdentifierInfo *SetterName = OCDS.getSetterName();
348 Selector SetterSel;
349 if (SetterName)
350 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
351 else
352 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
353 P.PP.getSelectorTable(),
354 FD.D.getIdentifier());
355 bool isOverridingProperty = false;
356 DeclPtrTy Property =
357 P.Actions.ActOnProperty(P.CurScope, AtLoc, FD, OCDS,
358 GetterSel, SetterSel, IDecl,
359 &isOverridingProperty,
360 MethodImplKind);
361 if (!isOverridingProperty)
362 Props.push_back(Property);
363
364 return Property;
365 }
366 } Callback(*this, interfaceDecl, allProperties,
367 OCDS, AtLoc, MethodImplKind);
368
Chris Lattner038a3e32008-10-20 05:46:22 +0000369 // Parse all the comma separated declarators.
370 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +0000371 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000372
Chris Lattnere0d3fe92008-10-20 06:15:13 +0000373 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
374 tok::at);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000375 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000376 }
Steve Naroff99264b42007-08-22 16:35:03 +0000377 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000378
379 // We break out of the big loop in two cases: when we see @end or when we see
380 // EOF. In the former case, eat the @end. In the later case, emit an error.
381 if (Tok.isObjCAtKeyword(tok::objc_end))
382 ConsumeToken(); // the "end" identifier
383 else
384 Diag(Tok, diag::err_objc_missing_end);
Mike Stump11289f42009-09-09 15:08:12 +0000385
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000386 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000387 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek09a0d042008-06-06 16:45:15 +0000388 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000389 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000390 allProperties.data(), allProperties.size(),
391 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000392}
393
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000394/// Parse property attribute declarations.
395///
396/// property-attr-decl: '(' property-attrlist ')'
397/// property-attrlist:
398/// property-attribute
399/// property-attrlist ',' property-attribute
400/// property-attribute:
401/// getter '=' identifier
402/// setter '=' identifier ':'
403/// readonly
404/// readwrite
405/// assign
406/// retain
407/// copy
408/// nonatomic
409///
Chris Lattnerd7352d62008-07-21 22:17:28 +0000410void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000411 assert(Tok.getKind() == tok::l_paren);
Chris Lattner43c76c32008-10-20 07:00:43 +0000412 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump11289f42009-09-09 15:08:12 +0000413
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000414 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000415 if (Tok.is(tok::code_completion)) {
416 Actions.CodeCompleteObjCProperty(CurScope, DS);
417 ConsumeToken();
418 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000419 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000420
Chris Lattner76619232008-10-20 07:22:18 +0000421 // If this is not an identifier at all, bail out early.
422 if (II == 0) {
423 MatchRHSPunctuation(tok::r_paren, LHSLoc);
424 return;
425 }
Mike Stump11289f42009-09-09 15:08:12 +0000426
Chris Lattner1db33542008-10-20 07:37:22 +0000427 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000428
Chris Lattner68e48682008-11-20 04:42:34 +0000429 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000430 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000431 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000432 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner68e48682008-11-20 04:42:34 +0000433 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000434 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000435 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000436 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner68e48682008-11-20 04:42:34 +0000437 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000438 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000439 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000440 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner68e48682008-11-20 04:42:34 +0000441 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattner825bca12008-10-20 07:39:53 +0000442 // getter/setter require extra treatment.
Chris Lattner1db33542008-10-20 07:37:22 +0000443 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
444 tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000445 return;
Mike Stump11289f42009-09-09 15:08:12 +0000446
Chris Lattnerbeca7702008-10-20 07:24:39 +0000447 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000448 Diag(Tok, diag::err_expected_ident);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000449 SkipUntil(tok::r_paren);
450 return;
451 }
Mike Stump11289f42009-09-09 15:08:12 +0000452
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000453 if (II->getNameStart()[0] == 's') {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000454 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
455 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000456 ConsumeToken(); // consume method name
Mike Stump11289f42009-09-09 15:08:12 +0000457
Chris Lattner1db33542008-10-20 07:37:22 +0000458 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
459 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000460 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000461 } else {
462 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
463 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000464 ConsumeToken(); // consume method name
Chris Lattnerbeca7702008-10-20 07:24:39 +0000465 }
Chris Lattner825bca12008-10-20 07:39:53 +0000466 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000467 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000468 SkipUntil(tok::r_paren);
469 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000470 }
Mike Stump11289f42009-09-09 15:08:12 +0000471
Chris Lattner1db33542008-10-20 07:37:22 +0000472 if (Tok.isNot(tok::comma))
473 break;
Mike Stump11289f42009-09-09 15:08:12 +0000474
Chris Lattner1db33542008-10-20 07:37:22 +0000475 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000476 }
Mike Stump11289f42009-09-09 15:08:12 +0000477
Chris Lattner1db33542008-10-20 07:37:22 +0000478 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000479}
480
Steve Naroff09bf8152007-09-06 21:24:23 +0000481/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000482/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000483/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000484///
485/// objc-instance-method: '-'
486/// objc-class-method: '+'
487///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000488/// objc-method-attributes: [OBJC2]
489/// __attribute__((deprecated))
490///
Mike Stump11289f42009-09-09 15:08:12 +0000491Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +0000492 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000493 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000494
Mike Stump11289f42009-09-09 15:08:12 +0000495 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000496 SourceLocation mLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000497
Chris Lattner83f095c2009-03-28 19:18:32 +0000498 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff09bf8152007-09-06 21:24:23 +0000499 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000500 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000501 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000502}
503
504/// objc-selector:
505/// identifier
506/// one of
507/// enum struct union if else while do for switch case default
508/// break continue return goto asm sizeof typeof __alignof
509/// unsigned long const short volatile signed restrict _Complex
510/// in out inout bycopy byref oneway int char float double void _Bool
511///
Chris Lattner4f472a32009-04-11 18:13:45 +0000512IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000513 switch (Tok.getKind()) {
514 default:
515 return 0;
516 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000517 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000518 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000519 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000520 case tok::kw_break:
521 case tok::kw_case:
522 case tok::kw_catch:
523 case tok::kw_char:
524 case tok::kw_class:
525 case tok::kw_const:
526 case tok::kw_const_cast:
527 case tok::kw_continue:
528 case tok::kw_default:
529 case tok::kw_delete:
530 case tok::kw_do:
531 case tok::kw_double:
532 case tok::kw_dynamic_cast:
533 case tok::kw_else:
534 case tok::kw_enum:
535 case tok::kw_explicit:
536 case tok::kw_export:
537 case tok::kw_extern:
538 case tok::kw_false:
539 case tok::kw_float:
540 case tok::kw_for:
541 case tok::kw_friend:
542 case tok::kw_goto:
543 case tok::kw_if:
544 case tok::kw_inline:
545 case tok::kw_int:
546 case tok::kw_long:
547 case tok::kw_mutable:
548 case tok::kw_namespace:
549 case tok::kw_new:
550 case tok::kw_operator:
551 case tok::kw_private:
552 case tok::kw_protected:
553 case tok::kw_public:
554 case tok::kw_register:
555 case tok::kw_reinterpret_cast:
556 case tok::kw_restrict:
557 case tok::kw_return:
558 case tok::kw_short:
559 case tok::kw_signed:
560 case tok::kw_sizeof:
561 case tok::kw_static:
562 case tok::kw_static_cast:
563 case tok::kw_struct:
564 case tok::kw_switch:
565 case tok::kw_template:
566 case tok::kw_this:
567 case tok::kw_throw:
568 case tok::kw_true:
569 case tok::kw_try:
570 case tok::kw_typedef:
571 case tok::kw_typeid:
572 case tok::kw_typename:
573 case tok::kw_typeof:
574 case tok::kw_union:
575 case tok::kw_unsigned:
576 case tok::kw_using:
577 case tok::kw_virtual:
578 case tok::kw_void:
579 case tok::kw_volatile:
580 case tok::kw_wchar_t:
581 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000582 case tok::kw__Bool:
583 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000584 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000585 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000586 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000587 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000588 }
Steve Naroff99264b42007-08-22 16:35:03 +0000589}
590
Fariborz Jahanian83615522008-01-02 22:54:34 +0000591/// objc-for-collection-in: 'in'
592///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000593bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000594 // FIXME: May have to do additional look-ahead to only allow for
595 // valid tokens following an 'in'; such as an identifier, unary operators,
596 // '[' etc.
Mike Stump11289f42009-09-09 15:08:12 +0000597 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000598 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000599}
600
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000601/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000602/// qualifier list and builds their bitmask representation in the input
603/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000604///
605/// objc-type-qualifiers:
606/// objc-type-qualifier
607/// objc-type-qualifiers objc-type-qualifier
608///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000609void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner61511e12007-12-12 06:56:32 +0000610 while (1) {
Chris Lattner5e530bc2007-12-27 19:57:00 +0000611 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000612 return;
Mike Stump11289f42009-09-09 15:08:12 +0000613
Chris Lattner61511e12007-12-12 06:56:32 +0000614 const IdentifierInfo *II = Tok.getIdentifierInfo();
615 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000616 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000617 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000618
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000619 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000620 switch (i) {
621 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000622 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
623 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
624 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
625 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
626 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
627 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000628 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000629 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000630 ConsumeToken();
631 II = 0;
632 break;
633 }
Mike Stump11289f42009-09-09 15:08:12 +0000634
Chris Lattner61511e12007-12-12 06:56:32 +0000635 // If this wasn't a recognized qualifier, bail out.
636 if (II) return;
637 }
638}
639
640/// objc-type-name:
641/// '(' objc-type-qualifiers[opt] type-name ')'
642/// '(' objc-type-qualifiers[opt] ')'
643///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000644Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000645 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000646
Chris Lattnerb7954432008-10-22 03:52:06 +0000647 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000648 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000649
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000650 // Parse type qualifiers, in, inout, etc.
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000651 ParseObjCTypeQualifierList(DS);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000652
Chris Lattnerb7954432008-10-22 03:52:06 +0000653 TypeTy *Ty = 0;
Douglas Gregor220cac52009-02-18 17:45:20 +0000654 if (isTypeSpecifierQualifier()) {
655 TypeResult TypeSpec = ParseTypeName();
656 if (!TypeSpec.isInvalid())
657 Ty = TypeSpec.get();
658 }
Mike Stump11289f42009-09-09 15:08:12 +0000659
Steve Naroff90255b42008-10-21 14:15:04 +0000660 if (Tok.is(tok::r_paren))
Chris Lattnerb7954432008-10-22 03:52:06 +0000661 ConsumeParen();
662 else if (Tok.getLocation() == TypeStartLoc) {
663 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000664 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000665 SkipUntil(tok::r_paren);
666 } else {
667 // Otherwise, we found *something*, but didn't get a ')' in the right
668 // place. Emit an error then return what we have as the type.
669 MatchRHSPunctuation(tok::r_paren, LParenLoc);
670 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000671 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000672}
673
674/// objc-method-decl:
675/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000676/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000677/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000678/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000679///
680/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000681/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000682/// objc-keyword-selector objc-keyword-decl
683///
684/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000685/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
686/// objc-selector ':' objc-keyword-attributes[opt] identifier
687/// ':' objc-type-name objc-keyword-attributes[opt] identifier
688/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000689///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000690/// objc-parmlist:
691/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000692///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000693/// objc-parms:
694/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000695///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000696/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000697/// , ...
698///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000699/// objc-keyword-attributes: [OBJC2]
700/// __attribute__((unused))
701///
Chris Lattner83f095c2009-03-28 19:18:32 +0000702Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
703 tok::TokenKind mType,
704 DeclPtrTy IDecl,
705 tok::ObjCKeywordKind MethodImplKind) {
John McCall28a6aea2009-11-04 02:18:39 +0000706 ParsingDeclRAIIObject PD(*this);
707
Chris Lattner2ebb1782008-08-23 01:48:03 +0000708 // Parse the return type if present.
Chris Lattner5700fab2007-10-07 02:00:24 +0000709 TypeTy *ReturnType = 0;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000710 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000711 if (Tok.is(tok::l_paren))
Fariborz Jahanian7a9c4742007-10-31 23:53:01 +0000712 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump11289f42009-09-09 15:08:12 +0000713
Steve Naroff161a92b2007-10-26 20:53:56 +0000714 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000715 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000716
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000717 // An unnamed colon is valid.
718 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +0000719 Diag(Tok, diag::err_expected_selector_for_method)
720 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +0000721 // Skip until we get a ; or {}.
722 SkipUntil(tok::r_brace);
Chris Lattner83f095c2009-03-28 19:18:32 +0000723 return DeclPtrTy();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000724 }
Mike Stump11289f42009-09-09 15:08:12 +0000725
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000726 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattner0ef13522007-10-09 17:51:17 +0000727 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000728 // If attributes exist after the method, parse them.
729 AttributeList *MethodAttrs = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000730 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattner5700fab2007-10-07 02:00:24 +0000731 MethodAttrs = ParseAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000732
Chris Lattner5700fab2007-10-07 02:00:24 +0000733 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall28a6aea2009-11-04 02:18:39 +0000734 DeclPtrTy Result
735 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanianb5605172007-11-09 19:52:12 +0000736 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekbc76c722009-05-04 17:04:30 +0000737 0, CargNames, MethodAttrs,
738 MethodImplKind);
John McCall28a6aea2009-11-04 02:18:39 +0000739 PD.complete(Result);
740 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +0000741 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000742
Steve Narofff73590d2007-09-27 14:38:14 +0000743 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000744 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump11289f42009-09-09 15:08:12 +0000745
Chris Lattner5700fab2007-10-07 02:00:24 +0000746 while (1) {
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000747 Action::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000748
Chris Lattner5700fab2007-10-07 02:00:24 +0000749 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +0000750 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000751 Diag(Tok, diag::err_expected_colon);
752 break;
753 }
754 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +0000755
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000756 ArgInfo.Type = 0;
757 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
758 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
759
Chris Lattner5700fab2007-10-07 02:00:24 +0000760 // If attributes exist before the argument name, parse them.
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000761 ArgInfo.ArgAttrs = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +0000762 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000763 ArgInfo.ArgAttrs = ParseAttributes();
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000764
Chris Lattner0ef13522007-10-09 17:51:17 +0000765 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000766 Diag(Tok, diag::err_expected_ident); // missing argument name.
767 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +0000768 }
Mike Stump11289f42009-09-09 15:08:12 +0000769
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000770 ArgInfo.Name = Tok.getIdentifierInfo();
771 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +0000772 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000773
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000774 ArgInfos.push_back(ArgInfo);
775 KeyIdents.push_back(SelIdent);
776
Chris Lattner5700fab2007-10-07 02:00:24 +0000777 // Check for another keyword selector.
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000778 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000779 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +0000780 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +0000781 break;
782 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +0000783 }
Mike Stump11289f42009-09-09 15:08:12 +0000784
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000785 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +0000786
Chris Lattner5700fab2007-10-07 02:00:24 +0000787 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +0000788 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000789 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +0000790 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000791 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +0000792 ConsumeToken();
793 break;
794 }
Chris Lattner5700fab2007-10-07 02:00:24 +0000795 DeclSpec DS;
796 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000797 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +0000798 Declarator ParmDecl(DS, Declarator::PrototypeContext);
799 ParseDeclarator(ParmDecl);
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000800 CargNames.push_back(ParmDecl);
Chris Lattner5700fab2007-10-07 02:00:24 +0000801 }
Mike Stump11289f42009-09-09 15:08:12 +0000802
Chris Lattner5700fab2007-10-07 02:00:24 +0000803 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000804 // If attributes exist after the method, parse them.
Chris Lattner5700fab2007-10-07 02:00:24 +0000805 AttributeList *MethodAttrs = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000806 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattner5700fab2007-10-07 02:00:24 +0000807 MethodAttrs = ParseAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000808
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000809 if (KeyIdents.size() == 0)
810 return DeclPtrTy();
Chris Lattner5700fab2007-10-07 02:00:24 +0000811 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
812 &KeyIdents[0]);
John McCall28a6aea2009-11-04 02:18:39 +0000813 DeclPtrTy Result
814 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000815 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekbc76c722009-05-04 17:04:30 +0000816 &ArgInfos[0], CargNames, MethodAttrs,
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000817 MethodImplKind, isVariadic);
John McCall28a6aea2009-11-04 02:18:39 +0000818 PD.complete(Result);
819 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +0000820}
821
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000822/// objc-protocol-refs:
823/// '<' identifier-list '>'
824///
Chris Lattnerd7352d62008-07-21 22:17:28 +0000825bool Parser::
Chris Lattner83f095c2009-03-28 19:18:32 +0000826ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000827 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
828 bool WarnOnDeclarations,
829 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +0000830 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +0000831
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000832 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +0000833
Chris Lattner3bbae002008-07-26 04:03:38 +0000834 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +0000835
Chris Lattner3bbae002008-07-26 04:03:38 +0000836 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +0000837 if (Tok.is(tok::code_completion)) {
838 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
839 ProtocolIdents.size());
840 ConsumeToken();
841 }
842
Chris Lattner3bbae002008-07-26 04:03:38 +0000843 if (Tok.isNot(tok::identifier)) {
844 Diag(Tok, diag::err_expected_ident);
845 SkipUntil(tok::greater);
846 return true;
847 }
848 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
849 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000850 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +0000851 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000852
Chris Lattner3bbae002008-07-26 04:03:38 +0000853 if (Tok.isNot(tok::comma))
854 break;
855 ConsumeToken();
856 }
Mike Stump11289f42009-09-09 15:08:12 +0000857
Chris Lattner3bbae002008-07-26 04:03:38 +0000858 // Consume the '>'.
859 if (Tok.isNot(tok::greater)) {
860 Diag(Tok, diag::err_expected_greater);
861 return true;
862 }
Mike Stump11289f42009-09-09 15:08:12 +0000863
Chris Lattner3bbae002008-07-26 04:03:38 +0000864 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +0000865
Chris Lattner3bbae002008-07-26 04:03:38 +0000866 // Convert the list of protocols identifiers into a list of protocol decls.
867 Actions.FindProtocolDeclaration(WarnOnDeclarations,
868 &ProtocolIdents[0], ProtocolIdents.size(),
869 Protocols);
870 return false;
871}
872
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000873/// objc-class-instance-variables:
874/// '{' objc-instance-variable-decl-list[opt] '}'
875///
876/// objc-instance-variable-decl-list:
877/// objc-visibility-spec
878/// objc-instance-variable-decl ';'
879/// ';'
880/// objc-instance-variable-decl-list objc-visibility-spec
881/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
882/// objc-instance-variable-decl-list ';'
883///
884/// objc-visibility-spec:
885/// @private
886/// @protected
887/// @public
Steve Naroff00433d32007-08-21 21:17:12 +0000888/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000889///
890/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +0000891/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000892///
Chris Lattner83f095c2009-03-28 19:18:32 +0000893void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff33a1e802007-10-29 21:38:07 +0000894 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000895 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner83f095c2009-03-28 19:18:32 +0000896 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +0000897
Douglas Gregor45a33ec2009-01-12 18:45:55 +0000898 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +0000899
Steve Naroff00433d32007-08-21 21:17:12 +0000900 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump11289f42009-09-09 15:08:12 +0000901
Fariborz Jahanian2dfcec12008-04-29 23:03:51 +0000902 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroff00433d32007-08-21 21:17:12 +0000903 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +0000904 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +0000905 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +0000906
Steve Naroff00433d32007-08-21 21:17:12 +0000907 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +0000908 if (Tok.is(tok::semi)) {
Chris Lattner3e4fac72009-11-06 06:40:12 +0000909 Diag(Tok, diag::ext_extra_struct_semi)
910 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Steve Naroff00433d32007-08-21 21:17:12 +0000911 ConsumeToken();
912 continue;
913 }
Mike Stump11289f42009-09-09 15:08:12 +0000914
Steve Naroff00433d32007-08-21 21:17:12 +0000915 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +0000916 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +0000917 ConsumeToken(); // eat the @ sign
Steve Naroff7c348172007-08-23 18:16:40 +0000918 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +0000919 case tok::objc_private:
920 case tok::objc_public:
921 case tok::objc_protected:
922 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +0000923 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +0000924 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000925 continue;
Steve Naroff00433d32007-08-21 21:17:12 +0000926 default:
927 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +0000928 continue;
929 }
930 }
Mike Stump11289f42009-09-09 15:08:12 +0000931
John McCallcfefb6d2009-11-03 02:38:08 +0000932 struct ObjCIvarCallback : FieldCallback {
933 Parser &P;
934 DeclPtrTy IDecl;
935 tok::ObjCKeywordKind visibility;
936 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
937
938 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
939 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
940 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
941 }
942
943 DeclPtrTy invoke(FieldDeclarator &FD) {
944 // Install the declarator into the interface decl.
945 DeclPtrTy Field
946 = P.Actions.ActOnIvar(P.CurScope,
947 FD.D.getDeclSpec().getSourceRange().getBegin(),
948 IDecl, FD.D, FD.BitfieldSize, visibility);
949 AllIvarDecls.push_back(Field);
950 return Field;
951 }
952 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
953
Chris Lattnera12405b2008-04-10 06:46:29 +0000954 // Parse all the comma separated declarators.
955 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +0000956 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000957
Chris Lattner0ef13522007-10-09 17:51:17 +0000958 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +0000959 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +0000960 } else {
961 Diag(Tok, diag::err_expected_semi_decl_list);
962 // Skip to end of block or statement
963 SkipUntil(tok::r_brace, true, true);
964 }
965 }
Steve Naroff33a1e802007-10-29 21:38:07 +0000966 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff9c4fddd2007-10-31 22:11:35 +0000967 // Call ActOnFields() even if we don't have any decls. This is useful
968 // for code rewriting tools that need to be aware of the empty list.
969 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foad7d0479f2009-05-21 09:52:38 +0000970 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +0000971 LBraceLoc, RBraceLoc, 0);
Steve Naroff00433d32007-08-21 21:17:12 +0000972 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000973}
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000974
975/// objc-protocol-declaration:
976/// objc-protocol-definition
977/// objc-protocol-forward-reference
978///
979/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +0000980/// @protocol identifier
981/// objc-protocol-refs[opt]
982/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000983/// @end
984///
985/// objc-protocol-forward-reference:
986/// @protocol identifier-list ';'
987///
988/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +0000989/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000990/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattner83f095c2009-03-28 19:18:32 +0000991Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
992 AttributeList *attrList) {
Steve Naroff7c348172007-08-23 18:16:40 +0000993 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000994 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
995 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000996
Douglas Gregor5b4671c2009-11-18 04:49:41 +0000997 if (Tok.is(tok::code_completion)) {
998 Actions.CodeCompleteObjCProtocolDecl(CurScope);
999 ConsumeToken();
1000 }
1001
Chris Lattner0ef13522007-10-09 17:51:17 +00001002 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001003 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001004 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001005 }
1006 // Save the protocol name, then consume it.
1007 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1008 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001009
Chris Lattner0ef13522007-10-09 17:51:17 +00001010 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001011 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001012 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001013 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001014 attrList);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001015 }
Mike Stump11289f42009-09-09 15:08:12 +00001016
Chris Lattner0ef13522007-10-09 17:51:17 +00001017 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001018 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1019 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1020
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001021 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001022 while (1) {
1023 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001024 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001025 Diag(Tok, diag::err_expected_ident);
1026 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +00001027 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001028 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001029 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1030 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001031 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001032
Chris Lattner0ef13522007-10-09 17:51:17 +00001033 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001034 break;
1035 }
1036 // Consume the ';'.
1037 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattner83f095c2009-03-28 19:18:32 +00001038 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001039
Steve Naroff93eb5f12007-10-10 17:32:04 +00001040 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001041 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001042 ProtocolRefs.size(),
1043 attrList);
Chris Lattnerd7352d62008-07-21 22:17:28 +00001044 }
Mike Stump11289f42009-09-09 15:08:12 +00001045
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001046 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001047 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001048
Chris Lattner83f095c2009-03-28 19:18:32 +00001049 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001050 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001051 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001052 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1053 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +00001054 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001055
Chris Lattner83f095c2009-03-28 19:18:32 +00001056 DeclPtrTy ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001057 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001058 ProtocolRefs.data(),
1059 ProtocolRefs.size(),
Daniel Dunbar26e2ab42008-09-26 04:48:09 +00001060 EndProtoLoc, attrList);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +00001061 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerda9fb152008-10-20 06:10:06 +00001062 return ProtoType;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001063}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001064
1065/// objc-implementation:
1066/// objc-class-implementation-prologue
1067/// objc-category-implementation-prologue
1068///
1069/// objc-class-implementation-prologue:
1070/// @implementation identifier objc-superclass[opt]
1071/// objc-class-instance-variables[opt]
1072///
1073/// objc-category-implementation-prologue:
1074/// @implementation identifier ( identifier )
Chris Lattner83f095c2009-03-28 19:18:32 +00001075Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001076 SourceLocation atLoc) {
1077 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1078 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1079 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001080
Chris Lattner0ef13522007-10-09 17:51:17 +00001081 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001082 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001083 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001084 }
1085 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001086 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001087 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump11289f42009-09-09 15:08:12 +00001088
1089 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001090 // we have a category implementation.
1091 SourceLocation lparenLoc = ConsumeParen();
1092 SourceLocation categoryLoc, rparenLoc;
1093 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001094
Chris Lattner0ef13522007-10-09 17:51:17 +00001095 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001096 categoryId = Tok.getIdentifierInfo();
1097 categoryLoc = ConsumeToken();
1098 } else {
1099 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001100 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001101 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001102 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001103 Diag(Tok, diag::err_expected_rparen);
1104 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001105 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001106 }
1107 rparenLoc = ConsumeParen();
Chris Lattner83f095c2009-03-28 19:18:32 +00001108 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump11289f42009-09-09 15:08:12 +00001109 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001110 categoryLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001111 ObjCImpDecl = ImplCatType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001112 PendingObjCImpDecl.push_back(ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001113 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001114 }
1115 // We have a class implementation
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001116 SourceLocation superClassLoc;
1117 IdentifierInfo *superClassId = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +00001118 if (Tok.is(tok::colon)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001119 // We have a super class
1120 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001121 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001122 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001123 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001124 }
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001125 superClassId = Tok.getIdentifierInfo();
1126 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001127 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001128 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner5e530bc2007-12-27 19:57:00 +00001129 atLoc, nameId, nameLoc,
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001130 superClassId, superClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001131
Steve Naroff33a1e802007-10-29 21:38:07 +00001132 if (Tok.is(tok::l_brace)) // we have ivars
1133 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001134 ObjCImpDecl = ImplClsType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001135 PendingObjCImpDecl.push_back(ObjCImpDecl);
1136
Chris Lattner83f095c2009-03-28 19:18:32 +00001137 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001138}
Steve Naroff33a1e802007-10-29 21:38:07 +00001139
Chris Lattner83f095c2009-03-28 19:18:32 +00001140Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001141 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1142 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattner83f095c2009-03-28 19:18:32 +00001143 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001144 ConsumeToken(); // the "end" identifier
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001145 if (ObjCImpDecl) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001146 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001147 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001148 PendingObjCImpDecl.pop_back();
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001149 }
Fariborz Jahanianc644ee42008-01-10 17:58:07 +00001150 else
1151 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001152 return Result;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001153}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001154
Fariborz Jahanianbc02a102009-11-17 17:15:16 +00001155Parser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001156 if (PendingObjCImpDecl.empty())
1157 return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
1158 DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
1159 Actions.ActOnAtEnd(SourceLocation(), ImpDecl);
1160 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1161}
1162
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001163/// compatibility-alias-decl:
1164/// @compatibility_alias alias-name class-name ';'
1165///
Chris Lattner83f095c2009-03-28 19:18:32 +00001166Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001167 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1168 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1169 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001170 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001171 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001172 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001173 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001174 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1175 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001176 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001177 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001178 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001179 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001180 IdentifierInfo *classId = Tok.getIdentifierInfo();
1181 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1182 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001183 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattner83f095c2009-03-28 19:18:32 +00001184 return DeclPtrTy();
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001185 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001186 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1187 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001188}
1189
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001190/// property-synthesis:
1191/// @synthesize property-ivar-list ';'
1192///
1193/// property-ivar-list:
1194/// property-ivar
1195/// property-ivar-list ',' property-ivar
1196///
1197/// property-ivar:
1198/// identifier
1199/// identifier '=' identifier
1200///
Chris Lattner83f095c2009-03-28 19:18:32 +00001201Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001202 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1203 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001204 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattner0ef13522007-10-09 17:51:17 +00001205 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001206 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001207 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001208 }
Mike Stump11289f42009-09-09 15:08:12 +00001209
Chris Lattner0ef13522007-10-09 17:51:17 +00001210 while (Tok.is(tok::identifier)) {
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001211 IdentifierInfo *propertyIvar = 0;
1212 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1213 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattner0ef13522007-10-09 17:51:17 +00001214 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001215 // property '=' ivar-name
1216 ConsumeToken(); // consume '='
Chris Lattner0ef13522007-10-09 17:51:17 +00001217 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001218 Diag(Tok, diag::err_expected_ident);
1219 break;
1220 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001221 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001222 ConsumeToken(); // consume ivar-name
1223 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001224 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1225 propertyId, propertyIvar);
Chris Lattner0ef13522007-10-09 17:51:17 +00001226 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001227 break;
1228 ConsumeToken(); // consume ','
1229 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001230 if (Tok.isNot(tok::semi))
Chris Lattner6d29c102008-11-18 07:48:38 +00001231 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Fariborz Jahanian95239112009-11-06 21:48:47 +00001232 else
1233 ConsumeToken(); // consume ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001234 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001235}
1236
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001237/// property-dynamic:
1238/// @dynamic property-list
1239///
1240/// property-list:
1241/// identifier
1242/// property-list ',' identifier
1243///
Chris Lattner83f095c2009-03-28 19:18:32 +00001244Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001245 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1246 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1247 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattner0ef13522007-10-09 17:51:17 +00001248 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001249 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001250 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001251 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001252 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001253 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1254 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1255 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1256 propertyId, 0);
1257
Chris Lattner0ef13522007-10-09 17:51:17 +00001258 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001259 break;
1260 ConsumeToken(); // consume ','
1261 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001262 if (Tok.isNot(tok::semi))
Chris Lattner6d29c102008-11-18 07:48:38 +00001263 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattner83f095c2009-03-28 19:18:32 +00001264 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001265}
Mike Stump11289f42009-09-09 15:08:12 +00001266
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001267/// objc-throw-statement:
1268/// throw expression[opt];
1269///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001270Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redlc13f2682008-12-09 20:22:58 +00001271 OwningExprResult Res(Actions);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001272 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001273 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001274 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001275 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001276 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001277 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001278 }
1279 }
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001280 ConsumeToken(); // consume ';'
Steve Naroff5ee2c022009-02-11 20:05:44 +00001281 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001282}
1283
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001284/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001285/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001286///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001287Parser::OwningStmtResult
1288Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001289 ConsumeToken(); // consume synchronized
1290 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001291 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001292 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001293 }
1294 ConsumeParen(); // '('
Sebastian Redl59b5e512008-12-11 21:36:32 +00001295 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001296 if (Res.isInvalid()) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001297 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001298 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001299 }
1300 if (Tok.isNot(tok::r_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001301 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001302 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001303 }
1304 ConsumeParen(); // ')'
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001305 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001306 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001307 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001308 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001309 // Enter a scope to hold everything within the compound stmt. Compound
1310 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001311 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroffd9c26072008-06-04 20:36:13 +00001312
Sebastian Redl042ad952008-12-11 19:30:53 +00001313 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001314
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001315 BodyScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001316 if (SynchBody.isInvalid())
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001317 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl726a0d92009-02-05 15:02:23 +00001318 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001319}
1320
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001321/// objc-try-catch-statement:
1322/// @try compound-statement objc-catch-list[opt]
1323/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1324///
1325/// objc-catch-list:
1326/// @catch ( parameter-declaration ) compound-statement
1327/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1328/// catch-parameter-declaration:
1329/// parameter-declaration
1330/// '...' [OBJC2]
1331///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001332Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001333 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001334
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001335 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001336 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001337 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001338 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001339 }
Sebastian Redlc13f2682008-12-09 20:22:58 +00001340 OwningStmtResult CatchStmts(Actions);
1341 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001342 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl042ad952008-12-11 19:30:53 +00001343 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001344 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001345 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001346 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001347
Chris Lattner0ef13522007-10-09 17:51:17 +00001348 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001349 // At this point, we need to lookahead to determine if this @ is the start
1350 // of an @catch or @finally. We don't want to consume the @ token if this
1351 // is an @try or @encode or something else.
1352 Token AfterAt = GetLookAheadToken(1);
1353 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1354 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1355 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001356
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001357 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001358 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001359 DeclPtrTy FirstPart;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001360 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001361 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001362 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001363 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001364 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001365 DeclSpec DS;
1366 ParseDeclarationSpecifiers(DS);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001367 // For some odd reason, the name of the exception variable is
Mike Stump11289f42009-09-09 15:08:12 +00001368 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff371b8fb2009-03-03 19:52:17 +00001369 // we must accept either 'declarator' or 'abstract-declarator' here.
1370 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1371 ParseDeclarator(ParmDecl);
1372
1373 // Inform the actions module about the parameter declarator, so it
1374 // gets added to the current scope.
1375 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001376 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001377 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001378
Steve Naroff65a00892009-04-07 22:56:58 +00001379 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001380
Steve Naroff65a00892009-04-07 22:56:58 +00001381 if (Tok.is(tok::r_paren))
1382 RParenLoc = ConsumeParen();
1383 else // Skip over garbage, until we get to ')'. Eat the ')'.
1384 SkipUntil(tok::r_paren, true, false);
1385
Sebastian Redlc13f2682008-12-09 20:22:58 +00001386 OwningStmtResult CatchBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001387 if (Tok.is(tok::l_brace))
1388 CatchBody = ParseCompoundStatementBody();
1389 else
1390 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001391 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001392 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001393 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff371b8fb2009-03-03 19:52:17 +00001394 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001395 move(CatchStmts));
Steve Naroffe6016792008-02-05 21:27:35 +00001396 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001397 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1398 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001399 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001400 }
1401 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001402 } else {
1403 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001404 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001405 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001406
Sebastian Redlc13f2682008-12-09 20:22:58 +00001407 OwningStmtResult FinallyBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001408 if (Tok.is(tok::l_brace))
1409 FinallyBody = ParseCompoundStatementBody();
1410 else
1411 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001412 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001413 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001414 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl726a0d92009-02-05 15:02:23 +00001415 move(FinallyBody));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001416 catch_or_finally_seen = true;
1417 break;
1418 }
1419 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001420 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001421 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001422 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001423 }
Sebastian Redl726a0d92009-02-05 15:02:23 +00001424 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1425 move(FinallyStmt));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001426}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001427
Steve Naroff09bf8152007-09-06 21:24:23 +00001428/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001429///
Chris Lattner83f095c2009-03-28 19:18:32 +00001430Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1431 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001432
Chris Lattnereae6cb62009-03-05 08:00:35 +00001433 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1434 PP.getSourceManager(),
1435 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001436
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001437 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001438 if (Tok.is(tok::semi)) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00001439 if (ObjCImpDecl) {
1440 Diag(Tok, diag::warn_semicolon_before_method_body)
1441 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
1442 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001443 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001444 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001445
Steve Naroffbb875722007-11-11 19:54:21 +00001446 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001447 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001448 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001449
Steve Naroffbb875722007-11-11 19:54:21 +00001450 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1451 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001452
Steve Naroffbb875722007-11-11 19:54:21 +00001453 // If we didn't find the '{', bail out.
1454 if (Tok.isNot(tok::l_brace))
Chris Lattner83f095c2009-03-28 19:18:32 +00001455 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001456 }
Steve Naroffbb875722007-11-11 19:54:21 +00001457 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001458
Steve Naroffbb875722007-11-11 19:54:21 +00001459 // Enter a scope for the method body.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001460 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00001461
Steve Naroffbb875722007-11-11 19:54:21 +00001462 // Tell the actions module that we have entered a method definition with the
Steve Naroff542cd5d2008-07-25 17:57:26 +00001463 // specified Declarator for the method.
Steve Naroff70f41d62009-02-28 16:59:13 +00001464 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl042ad952008-12-11 19:30:53 +00001465
1466 OwningStmtResult FnBody(ParseCompoundStatementBody());
1467
Steve Naroffbb875722007-11-11 19:54:21 +00001468 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001469 if (FnBody.isInvalid())
Sebastian Redl52f03ba2008-12-21 12:04:03 +00001470 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1471 MultiStmtArg(Actions), false);
Sebastian Redlc675bab2008-12-13 16:23:55 +00001472
Steve Naroffb94d7f62009-03-02 22:00:56 +00001473 // TODO: Pass argument information.
1474 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump11289f42009-09-09 15:08:12 +00001475
Steve Naroffbb875722007-11-11 19:54:21 +00001476 // Leave the function body scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001477 BodyScope.Exit();
Sebastian Redlc675bab2008-12-13 16:23:55 +00001478
Steve Naroff7b8fa472007-11-13 23:01:27 +00001479 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001480}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001481
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001482Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroffe6016792008-02-05 21:27:35 +00001483 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001484 return ParseObjCTryStmt(AtLoc);
Steve Naroffe6016792008-02-05 21:27:35 +00001485 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1486 return ParseObjCThrowStmt(AtLoc);
1487 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1488 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redl90893182008-12-11 22:33:27 +00001489 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001490 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001491 // If the expression is invalid, skip ahead to the next semicolon. Not
1492 // doing this opens us up to the possibility of infinite loops if
1493 // ParseExpression does not consume any tokens.
1494 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001495 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001496 }
1497 // Otherwise, eat the semicolon.
1498 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson24824e52009-05-17 21:11:30 +00001499 return Actions.ActOnExprStmt(Actions.FullExpr(Res));
Steve Naroffe6016792008-02-05 21:27:35 +00001500}
1501
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001502Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001503 switch (Tok.getKind()) {
Chris Lattnere002fbe2007-12-12 01:04:12 +00001504 case tok::string_literal: // primary-expression: string-literal
1505 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001506 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001507 default:
Chris Lattner197a3012008-08-05 06:19:09 +00001508 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001509 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00001510
Chris Lattner197a3012008-08-05 06:19:09 +00001511 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1512 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001513 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001514 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001515 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001516 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001517 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001518 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001519 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00001520 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001521 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001522}
1523
Mike Stump11289f42009-09-09 15:08:12 +00001524/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001525/// '[' objc-receiver objc-message-args ']'
1526///
1527/// objc-receiver:
1528/// expression
1529/// class-name
1530/// type-name
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001531Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00001532 assert(Tok.is(tok::l_square) && "'[' expected");
1533 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1534
1535 // Parse receiver
Chris Lattnere10c6da2008-01-25 19:25:00 +00001536 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner8f697062008-01-25 18:59:06 +00001537 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00001538 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1539 SourceLocation NameLoc = ConsumeToken();
1540 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1541 ExprArg(Actions));
1542 }
Chris Lattner8f697062008-01-25 18:59:06 +00001543 }
1544
Sebastian Redl59b5e512008-12-11 21:36:32 +00001545 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001546 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00001547 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001548 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00001549 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001550
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001551 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001552 0, move(Res));
Chris Lattner8f697062008-01-25 18:59:06 +00001553}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001554
Chris Lattner8f697062008-01-25 18:59:06 +00001555/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1556/// the rest of a message expression.
Mike Stump11289f42009-09-09 15:08:12 +00001557///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001558/// objc-message-args:
1559/// objc-selector
1560/// objc-keywordarg-list
1561///
1562/// objc-keywordarg-list:
1563/// objc-keywordarg
1564/// objc-keywordarg-list objc-keywordarg
1565///
Mike Stump11289f42009-09-09 15:08:12 +00001566/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001567/// selector-name[opt] ':' objc-keywordexpr
1568///
1569/// objc-keywordexpr:
1570/// nonempty-expr-list
1571///
1572/// nonempty-expr-list:
1573/// assignment-expression
1574/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001575///
1576Parser::OwningExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00001577Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff9e4ac112008-11-19 15:54:23 +00001578 SourceLocation NameLoc,
Chris Lattner8f697062008-01-25 18:59:06 +00001579 IdentifierInfo *ReceiverName,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001580 ExprArg ReceiverExpr) {
Steve Naroffeae65032009-11-07 02:08:14 +00001581 if (Tok.is(tok::code_completion)) {
1582 if (ReceiverName)
Douglas Gregor090dd182009-11-17 23:31:36 +00001583 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc);
Steve Naroffeae65032009-11-07 02:08:14 +00001584 else
Douglas Gregor090dd182009-11-17 23:31:36 +00001585 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get());
Steve Naroffeae65032009-11-07 02:08:14 +00001586 ConsumeToken();
1587 }
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001588 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001589 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001590 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00001591
Anders Carlsson978f08d2009-02-14 18:21:46 +00001592 SourceLocation SelectorLoc = Loc;
Mike Stump11289f42009-09-09 15:08:12 +00001593
Steve Narofff73590d2007-09-27 14:38:14 +00001594 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl511ed552008-11-25 22:21:31 +00001595 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00001596
Chris Lattner0ef13522007-10-09 17:51:17 +00001597 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001598 while (1) {
1599 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00001600 KeyIdents.push_back(selIdent);
Steve Naroff486760a2007-09-17 20:25:27 +00001601
Chris Lattner0ef13522007-10-09 17:51:17 +00001602 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001603 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00001604 // We must manually skip to a ']', otherwise the expression skipper will
1605 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1606 // the enclosing expression.
1607 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001608 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001609 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001610
Steve Narofff73590d2007-09-27 14:38:14 +00001611 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00001612 /// Parse the expression after ':'
Sebastian Redl59b5e512008-12-11 21:36:32 +00001613 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001614 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001615 // We must manually skip to a ']', otherwise the expression skipper will
1616 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1617 // the enclosing expression.
1618 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001619 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00001620 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001621
Steve Naroff486760a2007-09-17 20:25:27 +00001622 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001623 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001624
Steve Naroff486760a2007-09-17 20:25:27 +00001625 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00001626 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001627 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001628 break;
1629 // We have a selector or a colon, continue parsing.
1630 }
1631 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00001632 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001633 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00001634 /// Parse the expression after ','
Sebastian Redl59b5e512008-12-11 21:36:32 +00001635 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001636 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001637 // We must manually skip to a ']', otherwise the expression skipper will
1638 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1639 // the enclosing expression.
1640 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001641 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001642 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001643
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001644 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001645 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001646 }
1647 } else if (!selIdent) {
1648 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001649
Chris Lattner197a3012008-08-05 06:19:09 +00001650 // We must manually skip to a ']', otherwise the expression skipper will
1651 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1652 // the enclosing expression.
1653 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001654 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001655 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001656
Chris Lattner0ef13522007-10-09 17:51:17 +00001657 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001658 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00001659 // We must manually skip to a ']', otherwise the expression skipper will
1660 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1661 // the enclosing expression.
1662 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001663 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001664 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001665
Chris Lattner8f697062008-01-25 18:59:06 +00001666 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001667
Steve Naroffe61bfa82007-10-05 18:42:47 +00001668 unsigned nKeys = KeyIdents.size();
Chris Lattner5700fab2007-10-07 02:00:24 +00001669 if (nKeys == 0)
1670 KeyIdents.push_back(selIdent);
1671 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001672
Chris Lattner5700fab2007-10-07 02:00:24 +00001673 // We've just parsed a keyword message.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001674 if (ReceiverName)
1675 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump11289f42009-09-09 15:08:12 +00001676 LBracLoc, NameLoc, SelectorLoc,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001677 RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001678 KeyExprs.take(), KeyExprs.size()));
1679 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001680 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001681 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001682}
1683
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001684Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redld65cea82008-12-11 22:51:44 +00001685 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001686 if (Res.isInvalid()) return move(Res);
1687
Chris Lattnere002fbe2007-12-12 01:04:12 +00001688 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1689 // expressions. At this point, we know that the only valid thing that starts
1690 // with '@' is an @"".
1691 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00001692 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00001693 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001694 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001695
Chris Lattnere002fbe2007-12-12 01:04:12 +00001696 while (Tok.is(tok::at)) {
1697 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00001698
Sebastian Redlc13f2682008-12-09 20:22:58 +00001699 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001700 if (!isTokenStringLiteral())
1701 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001702
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001703 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001704 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001705 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001706
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001707 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00001708 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001709
1710 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1711 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00001712}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001713
1714/// objc-encode-expression:
1715/// @encode ( type-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001716Parser::OwningExprResult
1717Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00001718 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001719
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001720 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001721
Chris Lattner197a3012008-08-05 06:19:09 +00001722 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001723 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1724
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001725 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001726
Douglas Gregor220cac52009-02-18 17:45:20 +00001727 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001728
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001729 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001730
Douglas Gregor220cac52009-02-18 17:45:20 +00001731 if (Ty.isInvalid())
1732 return ExprError();
1733
Mike Stump11289f42009-09-09 15:08:12 +00001734 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +00001735 Ty.get(), RParenLoc));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001736}
Anders Carlssone01493d2007-08-23 15:25:28 +00001737
1738/// objc-protocol-expression
1739/// @protocol ( protocol-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001740Parser::OwningExprResult
1741Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00001742 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001743
Chris Lattner197a3012008-08-05 06:19:09 +00001744 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001745 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1746
Anders Carlssone01493d2007-08-23 15:25:28 +00001747 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001748
Chris Lattner197a3012008-08-05 06:19:09 +00001749 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001750 return ExprError(Diag(Tok, diag::err_expected_ident));
1751
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001752 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00001753 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001754
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001755 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssone01493d2007-08-23 15:25:28 +00001756
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001757 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1758 LParenLoc, RParenLoc));
Anders Carlssone01493d2007-08-23 15:25:28 +00001759}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001760
1761/// objc-selector-expression
1762/// @selector '(' objc-keyword-selector ')'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001763Parser::OwningExprResult
1764Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001765 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001766
Chris Lattner197a3012008-08-05 06:19:09 +00001767 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001768 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1769
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001770 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001771 SourceLocation LParenLoc = ConsumeParen();
1772 SourceLocation sLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001773 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001774 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1775 return ExprError(Diag(Tok, diag::err_expected_ident));
1776
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001777 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00001778 unsigned nColons = 0;
1779 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001780 while (1) {
Chris Lattner197a3012008-08-05 06:19:09 +00001781 if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001782 return ExprError(Diag(Tok, diag::err_expected_colon));
1783
Chris Lattner5e530bc2007-12-27 19:57:00 +00001784 nColons++;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001785 ConsumeToken(); // Eat the ':'.
1786 if (Tok.is(tok::r_paren))
1787 break;
1788 // Check for another keyword selector.
1789 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001790 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001791 KeyIdents.push_back(SelIdent);
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001792 if (!SelIdent && Tok.isNot(tok::colon))
1793 break;
1794 }
Steve Naroff152dd812007-12-05 22:21:29 +00001795 }
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001796 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff152dd812007-12-05 22:21:29 +00001797 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001798 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1799 LParenLoc, RParenLoc));
Gabor Greif24032f12007-10-19 15:38:32 +00001800 }