blob: cabb5bf69a53c42784faed4d4a9c9f8ada262e53 [file] [log] [blame]
Ted Kremenek42730c52008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner4b009652007-07-25 00:24:17 +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 Naroff09a0c4c2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Fariborz Jahanian06798362007-11-01 23:59:59 +000016#include "clang/Parse/Scope.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/Basic/Diagnostic.h"
18#include "llvm/ADT/SmallVector.h"
19using namespace clang;
20
21
22/// ParseExternalDeclaration:
23/// external-declaration: [C99 6.9]
24/// [OBJC] objc-class-definition
Steve Naroff5b96e2e2007-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'
Steve Narofffb367882007-08-20 21:31:48 +000030Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Chris Lattner4b009652007-07-25 00:24:17 +000031 SourceLocation AtLoc = ConsumeToken(); // the "@"
32
Steve Naroff87c329f2007-08-23 18:16:40 +000033 switch (Tok.getObjCKeywordID()) {
Chris Lattner4b009652007-07-25 00:24:17 +000034 case tok::objc_class:
35 return ParseObjCAtClassDeclaration(AtLoc);
36 case tok::objc_interface:
Steve Narofffb367882007-08-20 21:31:48 +000037 return ParseObjCAtInterfaceDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000038 case tok::objc_protocol:
Steve Naroff72f17fb2007-08-22 22:17:26 +000039 return ParseObjCAtProtocolDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000040 case tok::objc_implementation:
Fariborz Jahanian83ddf822007-11-10 20:59:13 +000041 return ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000042 case tok::objc_end:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +000043 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000044 case tok::objc_compatibility_alias:
Fariborz Jahanianb62aff32007-09-04 19:26:51 +000045 return ParseObjCAtAliasDeclaration(AtLoc);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +000046 case tok::objc_synthesize:
47 return ParseObjCPropertySynthesize(AtLoc);
48 case tok::objc_dynamic:
49 return ParseObjCPropertyDynamic(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000050 default:
51 Diag(AtLoc, diag::err_unexpected_at);
52 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000053 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000054 }
55}
56
57///
58/// objc-class-declaration:
59/// '@' 'class' identifier-list ';'
60///
Steve Narofffb367882007-08-20 21:31:48 +000061Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattner4b009652007-07-25 00:24:17 +000062 ConsumeToken(); // the identifier "class"
63 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
64
65 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +000066 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +000067 Diag(Tok, diag::err_expected_ident);
68 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000069 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000070 }
Chris Lattner4b009652007-07-25 00:24:17 +000071 ClassNames.push_back(Tok.getIdentifierInfo());
72 ConsumeToken();
73
Chris Lattnera1d2bb72007-10-09 17:51:17 +000074 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +000075 break;
76
77 ConsumeToken();
78 }
79
80 // Consume the ';'.
81 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Steve Narofffb367882007-08-20 21:31:48 +000082 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000083
Steve Naroff415c1832007-10-10 17:32:04 +000084 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff81f1bba2007-09-06 21:24:23 +000085 &ClassNames[0], ClassNames.size());
Chris Lattner4b009652007-07-25 00:24:17 +000086}
87
Steve Narofffb367882007-08-20 21:31:48 +000088///
89/// objc-interface:
90/// objc-class-interface-attributes[opt] objc-class-interface
91/// objc-category-interface
92///
93/// objc-class-interface:
94/// '@' 'interface' identifier objc-superclass[opt]
95/// objc-protocol-refs[opt]
96/// objc-class-instance-variables[opt]
97/// objc-interface-decl-list
98/// @end
99///
100/// objc-category-interface:
101/// '@' 'interface' identifier '(' identifier[opt] ')'
102/// objc-protocol-refs[opt]
103/// objc-interface-decl-list
104/// @end
105///
106/// objc-superclass:
107/// ':' identifier
108///
109/// objc-class-interface-attributes:
110/// __attribute__((visibility("default")))
111/// __attribute__((visibility("hidden")))
112/// __attribute__((deprecated))
113/// __attribute__((unavailable))
114/// __attribute__((objc_exception)) - used by NSException on 64-bit
115///
116Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration(
117 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000118 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Narofffb367882007-08-20 21:31:48 +0000119 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
120 ConsumeToken(); // the "interface" identifier
121
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000122 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000123 Diag(Tok, diag::err_expected_ident); // missing class or category name.
124 return 0;
125 }
126 // We have a class or category name - consume it.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000127 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Narofffb367882007-08-20 21:31:48 +0000128 SourceLocation nameLoc = ConsumeToken();
129
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000130 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Narofffb367882007-08-20 21:31:48 +0000131 SourceLocation lparenLoc = ConsumeParen();
132 SourceLocation categoryLoc, rparenLoc;
133 IdentifierInfo *categoryId = 0;
134
Steve Naroffa7f62782007-08-23 19:56:30 +0000135 // For ObjC2, the category name is optional (not an error).
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000136 if (Tok.is(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000137 categoryId = Tok.getIdentifierInfo();
138 categoryLoc = ConsumeToken();
Steve Naroffa7f62782007-08-23 19:56:30 +0000139 } else if (!getLang().ObjC2) {
140 Diag(Tok, diag::err_expected_ident); // missing category name.
141 return 0;
Steve Narofffb367882007-08-20 21:31:48 +0000142 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000143 if (Tok.isNot(tok::r_paren)) {
Steve Narofffb367882007-08-20 21:31:48 +0000144 Diag(Tok, diag::err_expected_rparen);
145 SkipUntil(tok::r_paren, false); // don't stop at ';'
146 return 0;
147 }
148 rparenLoc = ConsumeParen();
Chris Lattner45142b92008-07-26 04:07:02 +0000149
Steve Narofffb367882007-08-20 21:31:48 +0000150 // Next, we need to check for any protocol references.
Chris Lattner45142b92008-07-26 04:07:02 +0000151 SourceLocation EndProtoLoc;
152 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
153 if (Tok.is(tok::less) &&
154 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
155 return 0;
156
Steve Narofffb367882007-08-20 21:31:48 +0000157 if (attrList) // categories don't support attributes.
158 Diag(Tok, diag::err_objc_no_attributes_on_category);
159
Steve Naroff415c1832007-10-10 17:32:04 +0000160 DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
Steve Naroff25aace82007-10-03 21:00:46 +0000161 nameId, nameLoc, categoryId, categoryLoc,
Steve Naroff667f1682007-10-30 13:30:57 +0000162 &ProtocolRefs[0], ProtocolRefs.size(),
Chris Lattner45142b92008-07-26 04:07:02 +0000163 EndProtoLoc);
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000164
165 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Steve Narofffb367882007-08-20 21:31:48 +0000166
Steve Naroff0bbffd82007-08-22 16:35:03 +0000167 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000168 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000169 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000170 return CategoryType;
Steve Narofffb367882007-08-20 21:31:48 +0000171 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000172 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000173 return 0;
174 }
175 // Parse a class interface.
176 IdentifierInfo *superClassId = 0;
177 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000178
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000179 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Narofffb367882007-08-20 21:31:48 +0000180 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000181 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000182 Diag(Tok, diag::err_expected_ident); // missing super class name.
183 return 0;
184 }
185 superClassId = Tok.getIdentifierInfo();
186 superClassLoc = ConsumeToken();
187 }
188 // Next, we need to check for any protocol references.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000189 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Steve Naroffef20ed32007-10-30 02:23:23 +0000190 SourceLocation endProtoLoc;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000191 if (Tok.is(tok::less)) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000192 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Narofffb367882007-08-20 21:31:48 +0000193 return 0;
194 }
Steve Naroff415c1832007-10-10 17:32:04 +0000195 DeclTy *ClsType = Actions.ActOnStartClassInterface(
Chris Lattner847f5c12007-12-27 19:57:00 +0000196 atLoc, nameId, nameLoc,
Steve Naroff304ed392007-09-05 23:30:30 +0000197 superClassId, superClassLoc, &ProtocolRefs[0],
Steve Naroffef20ed32007-10-30 02:23:23 +0000198 ProtocolRefs.size(), endProtoLoc, attrList);
Steve Naroff304ed392007-09-05 23:30:30 +0000199
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000200 if (Tok.is(tok::l_brace))
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000201 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Narofffb367882007-08-20 21:31:48 +0000202
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000203 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000204
205 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000206 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000207 ConsumeToken(); // the "end" identifier
Steve Narofffaed3bf2007-09-10 20:51:04 +0000208 return ClsType;
Steve Narofffb367882007-08-20 21:31:48 +0000209 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000210 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000211 return 0;
212}
213
214/// objc-interface-decl-list:
215/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000216/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000217/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000218/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000219/// objc-interface-decl-list declaration
220/// objc-interface-decl-list ';'
221///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000222/// objc-method-requirement: [OBJC2]
223/// @required
224/// @optional
225///
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000226void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000227 tok::ObjCKeywordKind contextKey) {
Ted Kremenek8c945b12008-06-06 16:45:15 +0000228 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000229 llvm::SmallVector<DeclTy*, 16> allProperties;
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000230 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000231 SourceLocation AtEndLoc;
232
Steve Naroff0bbffd82007-08-22 16:35:03 +0000233 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000234 if (Tok.is(tok::at)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000235 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff87c329f2007-08-23 18:16:40 +0000236 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000237
238 if (ocKind == tok::objc_end) { // terminate list
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000239 AtEndLoc = AtLoc;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000240 break;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000241 } else if (ocKind == tok::objc_required) { // protocols only
242 ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +0000243 MethodImplKind = ocKind;
244 if (contextKey != tok::objc_protocol)
245 Diag(AtLoc, diag::err_objc_protocol_required);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000246 } else if (ocKind == tok::objc_optional) { // protocols only
247 ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +0000248 MethodImplKind = ocKind;
249 if (contextKey != tok::objc_protocol)
250 Diag(AtLoc, diag::err_objc_protocol_optional);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000251 } else if (ocKind == tok::objc_property) {
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000252 ObjCDeclSpec OCDS;
253 ConsumeToken(); // the "property" identifier
254 // Parse property attribute list, if any.
255 if (Tok.is(tok::l_paren)) {
256 // property has attribute list.
257 ParseObjCPropertyAttribute(OCDS);
258 }
259 // Parse all the comma separated declarators.
260 DeclSpec DS;
261 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
262 ParseStructDeclaration(DS, FieldDeclarators);
263
264 if (Tok.is(tok::semi))
265 ConsumeToken();
266 else {
267 Diag(Tok, diag::err_expected_semi_decl_list);
268 SkipUntil(tok::r_brace, true, true);
269 }
270 // Convert them all to property declarations.
271 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
272 FieldDeclarator &FD = FieldDeclarators[i];
273 // Install the property declarator into interfaceDecl.
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +0000274 Selector GetterSel =
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000275 PP.getSelectorTable().getNullarySelector(OCDS.getGetterName()
276 ? OCDS.getGetterName()
277 : FD.D.getIdentifier());
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +0000278 Selector SetterSel =
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000279 PP.getSelectorTable().getNullarySelector(OCDS.getSetterName()
280 ? OCDS.getSetterName()
281 // FIXME. This is not right!
282 : FD.D.getIdentifier());
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000283 DeclTy *Property = Actions.ActOnProperty(CurScope,
Steve Naroff638d6a42008-05-22 23:24:08 +0000284 AtLoc, FD, OCDS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +0000285 GetterSel, SetterSel,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +0000286 MethodImplKind);
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000287 allProperties.push_back(Property);
288 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000289 continue;
290 } else {
291 Diag(Tok, diag::err_objc_illegal_interface_qual);
292 ConsumeToken();
293 }
294 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000295 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
296 DeclTy *methodPrototype =
297 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000298 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000299 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
300 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000301 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000302 continue;
303 }
Fariborz Jahanian5d175c32007-12-11 18:34:51 +0000304 else if (Tok.is(tok::at))
305 continue;
306
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000307 if (Tok.is(tok::semi))
Steve Naroff0bbffd82007-08-22 16:35:03 +0000308 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000309 else if (Tok.is(tok::eof))
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000310 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000311 else {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000312 // FIXME: as the name implies, this rule allows function definitions.
313 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000314 ParseDeclarationOrFunctionDefinition();
Steve Naroff304ed392007-09-05 23:30:30 +0000315 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000316 }
Steve Naroff1ccf4632007-10-30 03:43:13 +0000317 /// Insert collected methods declarations into the @interface object.
Ted Kremenek8c945b12008-06-06 16:45:15 +0000318 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
319 allMethods.empty() ? 0 : &allMethods[0],
320 allMethods.size(),
321 allProperties.empty() ? 0 : &allProperties[0],
322 allProperties.size());
Steve Naroff0bbffd82007-08-22 16:35:03 +0000323}
324
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000325/// Parse property attribute declarations.
326///
327/// property-attr-decl: '(' property-attrlist ')'
328/// property-attrlist:
329/// property-attribute
330/// property-attrlist ',' property-attribute
331/// property-attribute:
332/// getter '=' identifier
333/// setter '=' identifier ':'
334/// readonly
335/// readwrite
336/// assign
337/// retain
338/// copy
339/// nonatomic
340///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000341void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000342 SourceLocation loc = ConsumeParen(); // consume '('
343 while (isObjCPropertyAttribute()) {
344 const IdentifierInfo *II = Tok.getIdentifierInfo();
345 // getter/setter require extra treatment.
Ted Kremenek42730c52008-01-07 19:49:32 +0000346 if (II == ObjCPropertyAttrs[objc_getter] ||
347 II == ObjCPropertyAttrs[objc_setter]) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000348 // skip getter/setter part.
349 SourceLocation loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000350 if (Tok.is(tok::equal)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000351 loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000352 if (Tok.is(tok::identifier)) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000353 if (II == ObjCPropertyAttrs[objc_setter]) {
354 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000355 DS.setSetterName(Tok.getIdentifierInfo());
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000356 loc = ConsumeToken(); // consume method name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000357 if (Tok.isNot(tok::colon)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000358 Diag(loc, diag::err_expected_colon);
359 SkipUntil(tok::r_paren,true,true);
360 break;
361 }
362 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000363 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000364 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000365 DS.setGetterName(Tok.getIdentifierInfo());
366 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000367 }
368 else {
369 Diag(loc, diag::err_expected_ident);
Chris Lattner847f5c12007-12-27 19:57:00 +0000370 SkipUntil(tok::r_paren,true,true);
371 break;
372 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000373 }
374 else {
375 Diag(loc, diag::err_objc_expected_equal);
376 SkipUntil(tok::r_paren,true,true);
377 break;
378 }
379 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000380
Ted Kremenek42730c52008-01-07 19:49:32 +0000381 else if (II == ObjCPropertyAttrs[objc_readonly])
382 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
383 else if (II == ObjCPropertyAttrs[objc_assign])
384 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
385 else if (II == ObjCPropertyAttrs[objc_readwrite])
386 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
387 else if (II == ObjCPropertyAttrs[objc_retain])
388 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
389 else if (II == ObjCPropertyAttrs[objc_copy])
390 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
391 else if (II == ObjCPropertyAttrs[objc_nonatomic])
392 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000393
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000394 ConsumeToken(); // consume last attribute token
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000395 if (Tok.is(tok::comma)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000396 loc = ConsumeToken();
397 continue;
398 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000399 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000400 break;
401 Diag(loc, diag::err_expected_rparen);
402 SkipUntil(tok::semi);
403 return;
404 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000405 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000406 ConsumeParen();
407 else {
408 Diag(loc, diag::err_objc_expected_property_attr);
409 SkipUntil(tok::r_paren); // recover from error inside attribute list
410 }
411}
412
Steve Naroff81f1bba2007-09-06 21:24:23 +0000413/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000414/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000415/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000416///
417/// objc-instance-method: '-'
418/// objc-class-method: '+'
419///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000420/// objc-method-attributes: [OBJC2]
421/// __attribute__((deprecated))
422///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000423Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000424 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000425 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000426
427 tok::TokenKind methodType = Tok.getKind();
Steve Naroff3774dd92007-10-26 20:53:56 +0000428 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000429
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000430 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000431 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000432 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000433 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000434}
435
436/// objc-selector:
437/// identifier
438/// one of
439/// enum struct union if else while do for switch case default
440/// break continue return goto asm sizeof typeof __alignof
441/// unsigned long const short volatile signed restrict _Complex
442/// in out inout bycopy byref oneway int char float double void _Bool
443///
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000444IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000445 switch (Tok.getKind()) {
446 default:
447 return 0;
448 case tok::identifier:
449 case tok::kw_typeof:
450 case tok::kw___alignof:
451 case tok::kw_auto:
452 case tok::kw_break:
453 case tok::kw_case:
454 case tok::kw_char:
455 case tok::kw_const:
456 case tok::kw_continue:
457 case tok::kw_default:
458 case tok::kw_do:
459 case tok::kw_double:
460 case tok::kw_else:
461 case tok::kw_enum:
462 case tok::kw_extern:
463 case tok::kw_float:
464 case tok::kw_for:
465 case tok::kw_goto:
466 case tok::kw_if:
467 case tok::kw_inline:
468 case tok::kw_int:
469 case tok::kw_long:
470 case tok::kw_register:
471 case tok::kw_restrict:
472 case tok::kw_return:
473 case tok::kw_short:
474 case tok::kw_signed:
475 case tok::kw_sizeof:
476 case tok::kw_static:
477 case tok::kw_struct:
478 case tok::kw_switch:
479 case tok::kw_typedef:
480 case tok::kw_union:
481 case tok::kw_unsigned:
482 case tok::kw_void:
483 case tok::kw_volatile:
484 case tok::kw_while:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000485 case tok::kw_bool:
Chris Lattnerd031a452007-10-07 02:00:24 +0000486 case tok::kw__Bool:
487 case tok::kw__Complex:
488 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000489 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000490 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000491 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000492}
493
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000494/// property-attrlist: one of
495/// readonly getter setter assign retain copy nonatomic
496///
497bool Parser::isObjCPropertyAttribute() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000498 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000499 const IdentifierInfo *II = Tok.getIdentifierInfo();
500 for (unsigned i = 0; i < objc_NumAttrs; ++i)
Ted Kremenek42730c52008-01-07 19:49:32 +0000501 if (II == ObjCPropertyAttrs[i]) return true;
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000502 }
503 return false;
504}
505
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000506/// objc-for-collection-in: 'in'
507///
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000508bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000509 // FIXME: May have to do additional look-ahead to only allow for
510 // valid tokens following an 'in'; such as an identifier, unary operators,
511 // '[' etc.
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000512 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
513 Tok.getIdentifierInfo() == ObjCForCollectionInKW);
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000514}
515
Ted Kremenek42730c52008-01-07 19:49:32 +0000516/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner2b740db2007-12-12 06:56:32 +0000517/// qualifier list and builds their bitmask representation in the input
518/// argument.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000519///
520/// objc-type-qualifiers:
521/// objc-type-qualifier
522/// objc-type-qualifiers objc-type-qualifier
523///
Ted Kremenek42730c52008-01-07 19:49:32 +0000524void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner2b740db2007-12-12 06:56:32 +0000525 while (1) {
Chris Lattner847f5c12007-12-27 19:57:00 +0000526 if (Tok.isNot(tok::identifier))
Chris Lattner2b740db2007-12-12 06:56:32 +0000527 return;
528
529 const IdentifierInfo *II = Tok.getIdentifierInfo();
530 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000531 if (II != ObjCTypeQuals[i])
Chris Lattner2b740db2007-12-12 06:56:32 +0000532 continue;
533
Ted Kremenek42730c52008-01-07 19:49:32 +0000534 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner2b740db2007-12-12 06:56:32 +0000535 switch (i) {
536 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000537 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
538 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
539 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
540 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
541 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
542 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner2b740db2007-12-12 06:56:32 +0000543 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000544 DS.setObjCDeclQualifier(Qual);
Chris Lattner2b740db2007-12-12 06:56:32 +0000545 ConsumeToken();
546 II = 0;
547 break;
548 }
549
550 // If this wasn't a recognized qualifier, bail out.
551 if (II) return;
552 }
553}
554
555/// objc-type-name:
556/// '(' objc-type-qualifiers[opt] type-name ')'
557/// '(' objc-type-qualifiers[opt] ')'
558///
Ted Kremenek42730c52008-01-07 19:49:32 +0000559Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000560 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000561
562 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner265c8172007-09-27 15:15:46 +0000563 TypeTy *Ty = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000564
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000565 // Parse type qualifiers, in, inout, etc.
Ted Kremenek42730c52008-01-07 19:49:32 +0000566 ParseObjCTypeQualifierList(DS);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000567
Steve Naroff0bbffd82007-08-22 16:35:03 +0000568 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000569 Ty = ParseTypeName();
570 // FIXME: back when Sema support is in place...
571 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000572 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000573 if (Tok.isNot(tok::r_paren)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000574 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000575 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000576 }
577 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000578 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000579}
580
581/// objc-method-decl:
582/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000583/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000584/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000585/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000586///
587/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000588/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000589/// objc-keyword-selector objc-keyword-decl
590///
591/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000592/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
593/// objc-selector ':' objc-keyword-attributes[opt] identifier
594/// ':' objc-type-name objc-keyword-attributes[opt] identifier
595/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000596///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000597/// objc-parmlist:
598/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000599///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000600/// objc-parms:
601/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000602///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000603/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000604/// , ...
605///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000606/// objc-keyword-attributes: [OBJC2]
607/// __attribute__((unused))
608///
Steve Naroff3774dd92007-10-26 20:53:56 +0000609Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000610 tok::TokenKind mType,
611 DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000612 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000613{
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000614 // Parse the return type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000615 TypeTy *ReturnType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000616 ObjCDeclSpec DSRet;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000617 if (Tok.is(tok::l_paren))
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000618 ReturnType = ParseObjCTypeName(DSRet);
Steve Naroff3774dd92007-10-26 20:53:56 +0000619 SourceLocation selLoc;
620 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000621 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000622 if (!SelIdent) {
623 Diag(Tok, diag::err_expected_ident); // missing selector name.
624 // FIXME: this creates a unary selector with a null identifier, is this
625 // ok?? Maybe we should skip to the next semicolon or something.
626 }
627
628 // If attributes exist after the method, parse them.
629 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000630 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000631 MethodAttrs = ParseAttributes();
632
633 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000634 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000635 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000636 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerd031a452007-10-07 02:00:24 +0000637 }
Steve Naroff304ed392007-09-05 23:30:30 +0000638
Steve Naroff4ed9d662007-09-27 14:38:14 +0000639 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
640 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremenek42730c52008-01-07 19:49:32 +0000641 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000642 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000643
644 Action::TypeTy *TypeInfo;
645 while (1) {
646 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000647
Chris Lattnerd031a452007-10-07 02:00:24 +0000648 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000649 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000650 Diag(Tok, diag::err_expected_colon);
651 break;
652 }
653 ConsumeToken(); // Eat the ':'.
Ted Kremenek42730c52008-01-07 19:49:32 +0000654 ObjCDeclSpec DSType;
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000655 if (Tok.is(tok::l_paren)) { // Parse the argument type.
656 TypeInfo = ParseObjCTypeName(DSType);
657 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000658 else
659 TypeInfo = 0;
660 KeyTypes.push_back(TypeInfo);
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000661 ArgTypeQuals.push_back(DSType);
Steve Naroff304ed392007-09-05 23:30:30 +0000662
Chris Lattnerd031a452007-10-07 02:00:24 +0000663 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000664 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000665 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000666
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000667 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000668 Diag(Tok, diag::err_expected_ident); // missing argument name.
669 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000670 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000671 ArgNames.push_back(Tok.getIdentifierInfo());
672 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000673
Chris Lattnerd031a452007-10-07 02:00:24 +0000674 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000675 SourceLocation Loc;
676 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000677 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000678 break;
679 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000680 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000681
Steve Naroff29fe7462007-11-15 12:35:21 +0000682 bool isVariadic = false;
683
Chris Lattnerd031a452007-10-07 02:00:24 +0000684 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000685 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000686 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000687 if (Tok.is(tok::ellipsis)) {
Steve Naroff29fe7462007-11-15 12:35:21 +0000688 isVariadic = true;
Chris Lattnerd031a452007-10-07 02:00:24 +0000689 ConsumeToken();
690 break;
691 }
Steve Naroff29fe7462007-11-15 12:35:21 +0000692 // FIXME: implement this...
Chris Lattnerd031a452007-10-07 02:00:24 +0000693 // Parse the c-style argument declaration-specifier.
694 DeclSpec DS;
695 ParseDeclarationSpecifiers(DS);
696 // Parse the declarator.
697 Declarator ParmDecl(DS, Declarator::PrototypeContext);
698 ParseDeclarator(ParmDecl);
699 }
700
701 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000702 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000703 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000704 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000705 MethodAttrs = ParseAttributes();
706
707 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
708 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000709 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000710 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000711 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff29fe7462007-11-15 12:35:21 +0000712 &ArgNames[0], MethodAttrs,
713 MethodImplKind, isVariadic);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000714}
715
Steve Narofffb367882007-08-20 21:31:48 +0000716/// objc-protocol-refs:
717/// '<' identifier-list '>'
718///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000719bool Parser::
720ParseObjCProtocolReferences(llvm::SmallVectorImpl<IdentifierLocPair> &Protocols,
721 SourceLocation &endLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000722 assert(Tok.is(tok::less) && "expected <");
Steve Narofffb367882007-08-20 21:31:48 +0000723
724 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000725
726 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000727 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000728 Diag(Tok, diag::err_expected_ident);
729 SkipUntil(tok::greater);
730 return true;
731 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000732 Protocols.push_back(std::make_pair(Tok.getIdentifierInfo(),
733 Tok.getLocation()));
Steve Narofffb367882007-08-20 21:31:48 +0000734 ConsumeToken();
735
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000736 if (Tok.isNot(tok::comma))
Steve Narofffb367882007-08-20 21:31:48 +0000737 break;
738 ConsumeToken();
739 }
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000740
Steve Narofffb367882007-08-20 21:31:48 +0000741 // Consume the '>'.
Steve Naroffef20ed32007-10-30 02:23:23 +0000742 if (Tok.is(tok::greater)) {
743 endLoc = ConsumeAnyToken();
744 return false;
745 }
746 Diag(Tok, diag::err_expected_greater);
747 return true;
Steve Narofffb367882007-08-20 21:31:48 +0000748}
749
Chris Lattner2bdedd62008-07-26 04:03:38 +0000750/// objc-protocol-refs:
751/// '<' identifier-list '>'
752///
753bool Parser::
754ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclTy*> &Protocols,
755 bool WarnOnDeclarations, SourceLocation &EndLoc) {
756 assert(Tok.is(tok::less) && "expected <");
757
758 ConsumeToken(); // the "<"
759
760 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
761
762 while (1) {
763 if (Tok.isNot(tok::identifier)) {
764 Diag(Tok, diag::err_expected_ident);
765 SkipUntil(tok::greater);
766 return true;
767 }
768 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
769 Tok.getLocation()));
770 ConsumeToken();
771
772 if (Tok.isNot(tok::comma))
773 break;
774 ConsumeToken();
775 }
776
777 // Consume the '>'.
778 if (Tok.isNot(tok::greater)) {
779 Diag(Tok, diag::err_expected_greater);
780 return true;
781 }
782
783 EndLoc = ConsumeAnyToken();
784
785 // Convert the list of protocols identifiers into a list of protocol decls.
786 Actions.FindProtocolDeclaration(WarnOnDeclarations,
787 &ProtocolIdents[0], ProtocolIdents.size(),
788 Protocols);
789 return false;
790}
791
Steve Narofffb367882007-08-20 21:31:48 +0000792/// objc-class-instance-variables:
793/// '{' objc-instance-variable-decl-list[opt] '}'
794///
795/// objc-instance-variable-decl-list:
796/// objc-visibility-spec
797/// objc-instance-variable-decl ';'
798/// ';'
799/// objc-instance-variable-decl-list objc-visibility-spec
800/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
801/// objc-instance-variable-decl-list ';'
802///
803/// objc-visibility-spec:
804/// @private
805/// @protected
806/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000807/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000808///
809/// objc-instance-variable-decl:
810/// struct-declaration
811///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000812void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
813 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000814 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000815 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000816 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
817
Steve Naroffc4474992007-08-21 21:17:12 +0000818 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000819
Fariborz Jahanian7c420a72008-04-29 23:03:51 +0000820 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffc4474992007-08-21 21:17:12 +0000821 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000822 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000823 // Each iteration of this loop reads one objc-instance-variable-decl.
824
825 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000826 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000827 Diag(Tok, diag::ext_extra_struct_semi);
828 ConsumeToken();
829 continue;
830 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000831
Steve Naroffc4474992007-08-21 21:17:12 +0000832 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000833 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000834 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000835 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000836 case tok::objc_private:
837 case tok::objc_public:
838 case tok::objc_protected:
839 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000840 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000841 ConsumeToken();
842 continue;
843 default:
844 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffc4474992007-08-21 21:17:12 +0000845 continue;
846 }
847 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000848
849 // Parse all the comma separated declarators.
850 DeclSpec DS;
851 FieldDeclarators.clear();
852 ParseStructDeclaration(DS, FieldDeclarators);
853
854 // Convert them all to fields.
855 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
856 FieldDeclarator &FD = FieldDeclarators[i];
857 // Install the declarator into interfaceDecl.
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000858 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattner3dd8d392008-04-10 06:46:29 +0000859 DS.getSourceRange().getBegin(),
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000860 FD.D, FD.BitfieldSize, visibility);
Chris Lattner3dd8d392008-04-10 06:46:29 +0000861 AllIvarDecls.push_back(Field);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000862 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000863
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000864 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000865 ConsumeToken();
Steve Naroffc4474992007-08-21 21:17:12 +0000866 } else {
867 Diag(Tok, diag::err_expected_semi_decl_list);
868 // Skip to end of block or statement
869 SkipUntil(tok::r_brace, true, true);
870 }
871 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000872 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff809b4f02007-10-31 22:11:35 +0000873 // Call ActOnFields() even if we don't have any decls. This is useful
874 // for code rewriting tools that need to be aware of the empty list.
875 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
876 &AllIvarDecls[0], AllIvarDecls.size(),
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000877 LBraceLoc, RBraceLoc);
Steve Naroffc4474992007-08-21 21:17:12 +0000878 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000879}
Steve Narofffb367882007-08-20 21:31:48 +0000880
881/// objc-protocol-declaration:
882/// objc-protocol-definition
883/// objc-protocol-forward-reference
884///
885/// objc-protocol-definition:
886/// @protocol identifier
887/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000888/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000889/// @end
890///
891/// objc-protocol-forward-reference:
892/// @protocol identifier-list ';'
893///
894/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000895/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000896/// semicolon in the first alternative if objc-protocol-refs are omitted.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000897Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000898 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000899 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
900 ConsumeToken(); // the "protocol" identifier
901
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000902 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000903 Diag(Tok, diag::err_expected_ident); // missing protocol name.
904 return 0;
905 }
906 // Save the protocol name, then consume it.
907 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
908 SourceLocation nameLoc = ConsumeToken();
909
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000910 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000911 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000912 ConsumeToken();
Chris Lattnere705e5e2008-07-21 22:17:28 +0000913 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000914 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000915
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000916 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000917 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
918 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
919
Steve Naroff72f17fb2007-08-22 22:17:26 +0000920 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000921 while (1) {
922 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000923 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000924 Diag(Tok, diag::err_expected_ident);
925 SkipUntil(tok::semi);
926 return 0;
927 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000928 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
929 Tok.getLocation()));
Steve Naroff72f17fb2007-08-22 22:17:26 +0000930 ConsumeToken(); // the identifier
931
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000932 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000933 break;
934 }
935 // Consume the ';'.
936 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
937 return 0;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000938
Steve Naroff415c1832007-10-10 17:32:04 +0000939 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000940 &ProtocolRefs[0],
941 ProtocolRefs.size());
Chris Lattnere705e5e2008-07-21 22:17:28 +0000942 }
943
Steve Naroff72f17fb2007-08-22 22:17:26 +0000944 // Last, and definitely not least, parse a protocol declaration.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000945 SourceLocation EndProtoLoc;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000946
Chris Lattner2bdedd62008-07-26 04:03:38 +0000947 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000948 if (Tok.is(tok::less) &&
Chris Lattner2bdedd62008-07-26 04:03:38 +0000949 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattnere705e5e2008-07-21 22:17:28 +0000950 return 0;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000951
Chris Lattner2bdedd62008-07-26 04:03:38 +0000952 DeclTy *ProtoType =
953 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
954 &ProtocolRefs[0], ProtocolRefs.size(),
955 EndProtoLoc);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000956 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000957
958 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000959 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000960 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000961 return ProtoType;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000962 }
963 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000964 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000965}
Steve Narofffb367882007-08-20 21:31:48 +0000966
967/// objc-implementation:
968/// objc-class-implementation-prologue
969/// objc-category-implementation-prologue
970///
971/// objc-class-implementation-prologue:
972/// @implementation identifier objc-superclass[opt]
973/// objc-class-instance-variables[opt]
974///
975/// objc-category-implementation-prologue:
976/// @implementation identifier ( identifier )
977
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000978Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
979 SourceLocation atLoc) {
980 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
981 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
982 ConsumeToken(); // the "implementation" identifier
983
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000984 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000985 Diag(Tok, diag::err_expected_ident); // missing class or category name.
986 return 0;
987 }
988 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000989 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000990 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
991
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000992 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000993 // we have a category implementation.
994 SourceLocation lparenLoc = ConsumeParen();
995 SourceLocation categoryLoc, rparenLoc;
996 IdentifierInfo *categoryId = 0;
997
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000998 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000999 categoryId = Tok.getIdentifierInfo();
1000 categoryLoc = ConsumeToken();
1001 } else {
1002 Diag(Tok, diag::err_expected_ident); // missing category name.
1003 return 0;
1004 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001005 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001006 Diag(Tok, diag::err_expected_rparen);
1007 SkipUntil(tok::r_paren, false); // don't stop at ';'
1008 return 0;
1009 }
1010 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +00001011 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +00001012 atLoc, nameId, nameLoc, categoryId,
1013 categoryLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001014 ObjCImpDecl = ImplCatType;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001015 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001016 }
1017 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001018 SourceLocation superClassLoc;
1019 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001020 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001021 // We have a super class
1022 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001023 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001024 Diag(Tok, diag::err_expected_ident); // missing super class name.
1025 return 0;
1026 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001027 superClassId = Tok.getIdentifierInfo();
1028 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001029 }
Steve Naroff415c1832007-10-10 17:32:04 +00001030 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner847f5c12007-12-27 19:57:00 +00001031 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001032 superClassId, superClassLoc);
1033
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001034 if (Tok.is(tok::l_brace)) // we have ivars
1035 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001036 ObjCImpDecl = ImplClsType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001037
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001038 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001039}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001040
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001041Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1042 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1043 "ParseObjCAtEndDeclaration(): Expected @end");
1044 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001045 if (ObjCImpDecl)
Ted Kremenek42730c52008-01-07 19:49:32 +00001046 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001047 else
1048 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremenek42730c52008-01-07 19:49:32 +00001049 return ObjCImpDecl;
Steve Narofffb367882007-08-20 21:31:48 +00001050}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001051
1052/// compatibility-alias-decl:
1053/// @compatibility_alias alias-name class-name ';'
1054///
1055Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1056 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1057 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1058 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001059 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001060 Diag(Tok, diag::err_expected_ident);
1061 return 0;
1062 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001063 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1064 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001065 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001066 Diag(Tok, diag::err_expected_ident);
1067 return 0;
1068 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001069 IdentifierInfo *classId = Tok.getIdentifierInfo();
1070 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1071 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +00001072 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001073 return 0;
1074 }
1075 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1076 aliasId, aliasLoc,
1077 classId, classLoc);
1078 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +00001079}
1080
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001081/// property-synthesis:
1082/// @synthesize property-ivar-list ';'
1083///
1084/// property-ivar-list:
1085/// property-ivar
1086/// property-ivar-list ',' property-ivar
1087///
1088/// property-ivar:
1089/// identifier
1090/// identifier '=' identifier
1091///
1092Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1093 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1094 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001095 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001096 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001097 Diag(Tok, diag::err_expected_ident);
1098 return 0;
1099 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001100 while (Tok.is(tok::identifier)) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001101 IdentifierInfo *propertyIvar = 0;
1102 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1103 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001104 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001105 // property '=' ivar-name
1106 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001107 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001108 Diag(Tok, diag::err_expected_ident);
1109 break;
1110 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001111 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001112 ConsumeToken(); // consume ivar-name
1113 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001114 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1115 propertyId, propertyIvar);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001116 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001117 break;
1118 ConsumeToken(); // consume ','
1119 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001120 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001121 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1122 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001123}
1124
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001125/// property-dynamic:
1126/// @dynamic property-list
1127///
1128/// property-list:
1129/// identifier
1130/// property-list ',' identifier
1131///
1132Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1133 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1134 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1135 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001136 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001137 Diag(Tok, diag::err_expected_ident);
1138 return 0;
1139 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001140 while (Tok.is(tok::identifier)) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001141 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1142 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1143 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1144 propertyId, 0);
1145
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001146 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001147 break;
1148 ConsumeToken(); // consume ','
1149 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001150 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001151 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1152 return 0;
1153}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001154
1155/// objc-throw-statement:
1156/// throw expression[opt];
1157///
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001158Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1159 ExprResult Res;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001160 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001161 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001162 Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001163 if (Res.isInvalid) {
1164 SkipUntil(tok::semi);
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001165 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001166 }
1167 }
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001168 ConsumeToken(); // consume ';'
Ted Kremenek42730c52008-01-07 19:49:32 +00001169 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001170}
1171
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001172/// objc-synchronized-statement:
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001173/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001174///
1175Parser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001176 ConsumeToken(); // consume synchronized
1177 if (Tok.isNot(tok::l_paren)) {
1178 Diag (Tok, diag::err_expected_lparen_after, "@synchronized");
1179 return true;
1180 }
1181 ConsumeParen(); // '('
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001182 ExprResult Res = ParseExpression();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001183 if (Res.isInvalid) {
1184 SkipUntil(tok::semi);
1185 return true;
1186 }
1187 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001188 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001189 return true;
1190 }
1191 ConsumeParen(); // ')'
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001192 if (Tok.isNot(tok::l_brace)) {
1193 Diag (Tok, diag::err_expected_lbrace);
1194 return true;
1195 }
Steve Naroff70337ac2008-06-04 20:36:13 +00001196 // Enter a scope to hold everything within the compound stmt. Compound
1197 // statements can always hold declarations.
1198 EnterScope(Scope::DeclScope);
1199
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001200 StmtResult SynchBody = ParseCompoundStatementBody();
Steve Naroff70337ac2008-06-04 20:36:13 +00001201
1202 ExitScope();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001203 if (SynchBody.isInvalid)
1204 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
1205 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.Val, SynchBody.Val);
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001206}
1207
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001208/// objc-try-catch-statement:
1209/// @try compound-statement objc-catch-list[opt]
1210/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1211///
1212/// objc-catch-list:
1213/// @catch ( parameter-declaration ) compound-statement
1214/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1215/// catch-parameter-declaration:
1216/// parameter-declaration
1217/// '...' [OBJC2]
1218///
Chris Lattner80712392008-03-10 06:06:04 +00001219Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001220 bool catch_or_finally_seen = false;
Steve Naroffc949a462008-02-05 21:27:35 +00001221
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001222 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001223 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001224 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanian70952482007-11-01 21:12:44 +00001225 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001226 }
Fariborz Jahanian06798362007-11-01 23:59:59 +00001227 StmtResult CatchStmts;
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001228 StmtResult FinallyStmt;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001229 StmtResult TryBody = ParseCompoundStatementBody();
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001230 if (TryBody.isInvalid)
1231 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner80712392008-03-10 06:06:04 +00001232
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001233 while (Tok.is(tok::at)) {
Chris Lattner80712392008-03-10 06:06:04 +00001234 // At this point, we need to lookahead to determine if this @ is the start
1235 // of an @catch or @finally. We don't want to consume the @ token if this
1236 // is an @try or @encode or something else.
1237 Token AfterAt = GetLookAheadToken(1);
1238 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1239 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1240 break;
1241
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001242 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +00001243 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Fariborz Jahanian06798362007-11-01 23:59:59 +00001244 StmtTy *FirstPart = 0;
1245 ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001246 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001247 ConsumeParen();
Fariborz Jahanian06798362007-11-01 23:59:59 +00001248 EnterScope(Scope::DeclScope);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001249 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001250 DeclSpec DS;
1251 ParseDeclarationSpecifiers(DS);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001252 // For some odd reason, the name of the exception variable is
1253 // optional. As a result, we need to use PrototypeContext.
1254 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001255 ParseDeclarator(DeclaratorInfo);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001256 if (DeclaratorInfo.getIdentifier()) {
1257 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
Chris Lattnera4ff4272008-03-13 06:29:04 +00001258 DeclaratorInfo, 0);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001259 StmtResult stmtResult =
1260 Actions.ActOnDeclStmt(aBlockVarDecl,
1261 DS.getSourceRange().getBegin(),
1262 DeclaratorInfo.getSourceRange().getEnd());
1263 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
1264 }
Steve Naroffc949a462008-02-05 21:27:35 +00001265 } else
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001266 ConsumeToken(); // consume '...'
Fariborz Jahanian06798362007-11-01 23:59:59 +00001267 SourceLocation RParenLoc = ConsumeParen();
Chris Lattner8027be62008-02-14 19:27:54 +00001268
1269 StmtResult CatchBody(true);
1270 if (Tok.is(tok::l_brace))
1271 CatchBody = ParseCompoundStatementBody();
1272 else
1273 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001274 if (CatchBody.isInvalid)
1275 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001276 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian06798362007-11-01 23:59:59 +00001277 FirstPart, CatchBody.Val, CatchStmts.Val);
1278 ExitScope();
Steve Naroffc949a462008-02-05 21:27:35 +00001279 } else {
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001280 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1281 "@catch clause");
Fariborz Jahanian70952482007-11-01 21:12:44 +00001282 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001283 }
1284 catch_or_finally_seen = true;
Chris Lattner80712392008-03-10 06:06:04 +00001285 } else {
1286 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffc949a462008-02-05 21:27:35 +00001287 ConsumeToken(); // consume finally
Chris Lattner8027be62008-02-14 19:27:54 +00001288
1289 StmtResult FinallyBody(true);
1290 if (Tok.is(tok::l_brace))
1291 FinallyBody = ParseCompoundStatementBody();
1292 else
1293 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001294 if (FinallyBody.isInvalid)
1295 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001296 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001297 FinallyBody.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001298 catch_or_finally_seen = true;
1299 break;
1300 }
1301 }
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001302 if (!catch_or_finally_seen) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001303 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001304 return true;
1305 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001306 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001307 FinallyStmt.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001308}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001309
Steve Naroff81f1bba2007-09-06 21:24:23 +00001310/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001311///
Steve Naroff18c83382007-11-13 23:01:27 +00001312Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremenek42730c52008-01-07 19:49:32 +00001313 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001314 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001315 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001316 ConsumeToken();
1317
Steve Naroff9191a9e82007-11-11 19:54:21 +00001318 // We should have an opening brace now.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001319 if (Tok.isNot(tok::l_brace)) {
Steve Naroff70f16242008-02-29 21:48:07 +00001320 Diag(Tok, diag::err_expected_method_body);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001321
1322 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1323 SkipUntil(tok::l_brace, true, true);
1324
1325 // If we didn't find the '{', bail out.
1326 if (Tok.isNot(tok::l_brace))
Steve Naroff18c83382007-11-13 23:01:27 +00001327 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001328 }
Steve Naroff9191a9e82007-11-11 19:54:21 +00001329 SourceLocation BraceLoc = Tok.getLocation();
1330
1331 // Enter a scope for the method body.
1332 EnterScope(Scope::FnScope|Scope::DeclScope);
1333
1334 // Tell the actions module that we have entered a method definition with the
Steve Naroff3ac43f92008-07-25 17:57:26 +00001335 // specified Declarator for the method.
1336 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001337
1338 StmtResult FnBody = ParseCompoundStatementBody();
1339
1340 // If the function body could not be parsed, make a bogus compoundstmt.
1341 if (FnBody.isInvalid)
1342 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1343
1344 // Leave the function body scope.
1345 ExitScope();
1346
1347 // TODO: Pass argument information.
Steve Naroff3ac43f92008-07-25 17:57:26 +00001348 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff18c83382007-11-13 23:01:27 +00001349 return MDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001350}
Anders Carlssona66cad42007-08-21 17:43:55 +00001351
Steve Naroffc949a462008-02-05 21:27:35 +00001352Parser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
1353 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner80712392008-03-10 06:06:04 +00001354 return ParseObjCTryStmt(AtLoc);
Steve Naroffc949a462008-02-05 21:27:35 +00001355 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1356 return ParseObjCThrowStmt(AtLoc);
1357 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1358 return ParseObjCSynchronizedStmt(AtLoc);
1359 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
1360 if (Res.isInvalid) {
1361 // If the expression is invalid, skip ahead to the next semicolon. Not
1362 // doing this opens us up to the possibility of infinite loops if
1363 // ParseExpression does not consume any tokens.
1364 SkipUntil(tok::semi);
1365 return true;
1366 }
1367 // Otherwise, eat the semicolon.
1368 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1369 return Actions.ActOnExprStmt(Res.Val);
1370}
1371
Steve Narofffb9dd752007-10-15 20:55:58 +00001372Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001373
1374 switch (Tok.getKind()) {
Chris Lattnerddd3e632007-12-12 01:04:12 +00001375 case tok::string_literal: // primary-expression: string-literal
1376 case tok::wide_string_literal:
1377 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1378 default:
1379 break;
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001380 }
1381
1382 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Chris Lattnercfd61c82007-10-16 22:51:17 +00001383 case tok::objc_encode:
1384 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1385 case tok::objc_protocol:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001386 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001387 case tok::objc_selector:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001388 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001389 default:
1390 Diag(AtLoc, diag::err_unexpected_at);
1391 SkipUntil(tok::semi);
Chris Lattnerfd44db32008-01-30 21:20:25 +00001392 return true;
Anders Carlssona66cad42007-08-21 17:43:55 +00001393 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001394}
1395
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001396/// objc-message-expr:
1397/// '[' objc-receiver objc-message-args ']'
1398///
1399/// objc-receiver:
1400/// expression
1401/// class-name
1402/// type-name
Chris Lattnered27a532008-01-25 18:59:06 +00001403Parser::ExprResult Parser::ParseObjCMessageExpression() {
1404 assert(Tok.is(tok::l_square) && "'[' expected");
1405 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1406
1407 // Parse receiver
Chris Lattnerc0587e12008-01-25 19:25:00 +00001408 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattnered27a532008-01-25 18:59:06 +00001409 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1410 ConsumeToken();
1411 return ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 0);
1412 }
1413
1414 ExprResult Res = ParseAssignmentExpression();
1415 if (Res.isInvalid) {
1416 Diag(Tok, diag::err_invalid_receiver_to_message);
Chris Lattnere69015d2008-01-25 19:43:26 +00001417 SkipUntil(tok::r_square);
Chris Lattnered27a532008-01-25 18:59:06 +00001418 return Res;
1419 }
1420 return ParseObjCMessageExpressionBody(LBracLoc, 0, Res.Val);
1421}
1422
1423/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1424/// the rest of a message expression.
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001425///
1426/// objc-message-args:
1427/// objc-selector
1428/// objc-keywordarg-list
1429///
1430/// objc-keywordarg-list:
1431/// objc-keywordarg
1432/// objc-keywordarg-list objc-keywordarg
1433///
1434/// objc-keywordarg:
1435/// selector-name[opt] ':' objc-keywordexpr
1436///
1437/// objc-keywordexpr:
1438/// nonempty-expr-list
1439///
1440/// nonempty-expr-list:
1441/// assignment-expression
1442/// nonempty-expr-list , assignment-expression
1443///
Chris Lattnered27a532008-01-25 18:59:06 +00001444Parser::ExprResult
1445Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
1446 IdentifierInfo *ReceiverName,
1447 ExprTy *ReceiverExpr) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001448 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001449 SourceLocation Loc;
1450 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001451
1452 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1453 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1454
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001455 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001456 while (1) {
1457 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001458 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001459
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001460 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001461 Diag(Tok, diag::err_expected_colon);
1462 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001463 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001464 }
Steve Naroff4ed9d662007-09-27 14:38:14 +00001465 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001466 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001467 ExprResult Res = ParseAssignmentExpression();
1468 if (Res.isInvalid) {
1469 SkipUntil(tok::identifier);
1470 return Res;
1471 }
1472 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001473 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001474
1475 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001476 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001477 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001478 break;
1479 // We have a selector or a colon, continue parsing.
1480 }
1481 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001482 while (Tok.is(tok::comma)) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001483 ConsumeToken(); // Eat the ','.
1484 /// Parse the expression after ','
1485 ExprResult Res = ParseAssignmentExpression();
1486 if (Res.isInvalid) {
1487 SkipUntil(tok::identifier);
1488 return Res;
1489 }
1490 // We have a valid expression.
1491 KeyExprs.push_back(Res.Val);
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001492 }
1493 } else if (!selIdent) {
1494 Diag(Tok, diag::err_expected_ident); // missing selector name.
1495 SkipUntil(tok::semi);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001496 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001497 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001498
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001499 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001500 Diag(Tok, diag::err_expected_rsquare);
1501 SkipUntil(tok::semi);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001502 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001503 }
Chris Lattnered27a532008-01-25 18:59:06 +00001504 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001505
Steve Narofff9e80db2007-10-05 18:42:47 +00001506 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001507 if (nKeys == 0)
1508 KeyIdents.push_back(selIdent);
1509 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1510
1511 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001512 if (ReceiverName)
Fariborz Jahanian2ce5dc52007-11-12 20:13:27 +00001513 return Actions.ActOnClassMessage(CurScope,
Chris Lattnered27a532008-01-25 18:59:06 +00001514 ReceiverName, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001515 &KeyExprs[0], KeyExprs.size());
Chris Lattnered27a532008-01-25 18:59:06 +00001516 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001517 &KeyExprs[0], KeyExprs.size());
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001518}
1519
Steve Naroff0add5d22007-11-03 11:27:19 +00001520Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001521 ExprResult Res = ParseStringLiteralExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +00001522 if (Res.isInvalid) return Res;
Chris Lattnerddd3e632007-12-12 01:04:12 +00001523
1524 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1525 // expressions. At this point, we know that the only valid thing that starts
1526 // with '@' is an @"".
1527 llvm::SmallVector<SourceLocation, 4> AtLocs;
1528 llvm::SmallVector<ExprTy*, 4> AtStrings;
1529 AtLocs.push_back(AtLoc);
1530 AtStrings.push_back(Res.Val);
1531
1532 while (Tok.is(tok::at)) {
1533 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlssona66cad42007-08-21 17:43:55 +00001534
Chris Lattnerddd3e632007-12-12 01:04:12 +00001535 ExprResult Res(true); // Invalid unless there is a string literal.
1536 if (isTokenStringLiteral())
1537 Res = ParseStringLiteralExpression();
1538 else
1539 Diag(Tok, diag::err_objc_concat_string);
1540
1541 if (Res.isInvalid) {
1542 while (!AtStrings.empty()) {
1543 Actions.DeleteExpr(AtStrings.back());
1544 AtStrings.pop_back();
1545 }
1546 return Res;
1547 }
1548
1549 AtStrings.push_back(Res.Val);
1550 }
Fariborz Jahanian1a442d32007-12-12 23:55:49 +00001551
Chris Lattnerddd3e632007-12-12 01:04:12 +00001552 return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1553 AtStrings.size());
Anders Carlssona66cad42007-08-21 17:43:55 +00001554}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001555
1556/// objc-encode-expression:
1557/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001558Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001559 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001560
1561 SourceLocation EncLoc = ConsumeToken();
1562
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001563 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson8be1d402007-08-22 15:14:15 +00001564 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1565 return true;
1566 }
1567
1568 SourceLocation LParenLoc = ConsumeParen();
1569
1570 TypeTy *Ty = ParseTypeName();
1571
Anders Carlsson92faeb82007-08-23 15:31:37 +00001572 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001573
Chris Lattnercfd61c82007-10-16 22:51:17 +00001574 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001575 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001576}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001577
1578/// objc-protocol-expression
1579/// @protocol ( protocol-name )
1580
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001581Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001582{
1583 SourceLocation ProtoLoc = ConsumeToken();
1584
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001585 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001586 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1587 return true;
1588 }
1589
1590 SourceLocation LParenLoc = ConsumeParen();
1591
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001592 if (Tok.isNot(tok::identifier)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001593 Diag(Tok, diag::err_expected_ident);
1594 return true;
1595 }
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001596 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001597 ConsumeToken();
1598
Anders Carlsson92faeb82007-08-23 15:31:37 +00001599 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001600
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001601 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1602 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001603}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001604
1605/// objc-selector-expression
1606/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001607Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001608{
1609 SourceLocation SelectorLoc = ConsumeToken();
1610
1611 if (Tok.isNot(tok::l_paren)) {
1612 Diag(Tok, diag::err_expected_lparen_after, "@selector");
1613 return 0;
1614 }
1615
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001616 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001617 SourceLocation LParenLoc = ConsumeParen();
1618 SourceLocation sLoc;
1619 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
1620 if (!SelIdent && Tok.isNot(tok::colon)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001621 Diag(Tok, diag::err_expected_ident); // missing selector name.
1622 return 0;
1623 }
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001624 KeyIdents.push_back(SelIdent);
Steve Naroff6fd89272007-12-05 22:21:29 +00001625 unsigned nColons = 0;
1626 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001627 while (1) {
1628 if (Tok.isNot(tok::colon)) {
1629 Diag(Tok, diag::err_expected_colon);
1630 break;
1631 }
Chris Lattner847f5c12007-12-27 19:57:00 +00001632 nColons++;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001633 ConsumeToken(); // Eat the ':'.
1634 if (Tok.is(tok::r_paren))
1635 break;
1636 // Check for another keyword selector.
1637 SourceLocation Loc;
1638 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001639 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001640 if (!SelIdent && Tok.isNot(tok::colon))
1641 break;
1642 }
Steve Naroff6fd89272007-12-05 22:21:29 +00001643 }
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001644 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff6fd89272007-12-05 22:21:29 +00001645 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001646 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001647 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001648 }