blob: 803d820b21b49bbb8b8035aeb355676784b9d873 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseObjc.cpp - Objective C Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Steve Naroff and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/Basic/Diagnostic.h"
17#include "llvm/ADT/SmallVector.h"
18using namespace clang;
19
20
21/// ParseExternalDeclaration:
22/// external-declaration: [C99 6.9]
23/// [OBJC] objc-class-definition
24/// [OBJC] objc-class-declaration [TODO]
25/// [OBJC] objc-alias-declaration [TODO]
26/// [OBJC] objc-protocol-definition [TODO]
27/// [OBJC] objc-method-definition [TODO]
28/// [OBJC] '@' 'end' [TODO]
Steve Narofffb367882007-08-20 21:31:48 +000029Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Chris Lattner4b009652007-07-25 00:24:17 +000030 SourceLocation AtLoc = ConsumeToken(); // the "@"
31
Steve Naroff87c329f2007-08-23 18:16:40 +000032 switch (Tok.getObjCKeywordID()) {
Chris Lattner4b009652007-07-25 00:24:17 +000033 case tok::objc_class:
34 return ParseObjCAtClassDeclaration(AtLoc);
35 case tok::objc_interface:
Steve Narofffb367882007-08-20 21:31:48 +000036 return ParseObjCAtInterfaceDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000037 case tok::objc_protocol:
Steve Naroff72f17fb2007-08-22 22:17:26 +000038 return ParseObjCAtProtocolDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000039 case tok::objc_implementation:
40 return ParseObjCAtImplementationDeclaration();
41 case tok::objc_end:
42 return ParseObjCAtEndDeclaration();
43 case tok::objc_compatibility_alias:
44 return ParseObjCAtAliasDeclaration();
45 default:
46 Diag(AtLoc, diag::err_unexpected_at);
47 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000048 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000049 }
50}
51
52///
53/// objc-class-declaration:
54/// '@' 'class' identifier-list ';'
55///
Steve Narofffb367882007-08-20 21:31:48 +000056Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattner4b009652007-07-25 00:24:17 +000057 ConsumeToken(); // the identifier "class"
58 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
59
60 while (1) {
61 if (Tok.getKind() != tok::identifier) {
62 Diag(Tok, diag::err_expected_ident);
63 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000064 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000065 }
Chris Lattner4b009652007-07-25 00:24:17 +000066 ClassNames.push_back(Tok.getIdentifierInfo());
67 ConsumeToken();
68
69 if (Tok.getKind() != tok::comma)
70 break;
71
72 ConsumeToken();
73 }
74
75 // Consume the ';'.
76 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Steve Narofffb367882007-08-20 21:31:48 +000077 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000078
Steve Narofffb367882007-08-20 21:31:48 +000079 return Actions.ParsedObjcClassDeclaration(CurScope,
80 &ClassNames[0], ClassNames.size());
Chris Lattner4b009652007-07-25 00:24:17 +000081}
82
Steve Narofffb367882007-08-20 21:31:48 +000083///
84/// objc-interface:
85/// objc-class-interface-attributes[opt] objc-class-interface
86/// objc-category-interface
87///
88/// objc-class-interface:
89/// '@' 'interface' identifier objc-superclass[opt]
90/// objc-protocol-refs[opt]
91/// objc-class-instance-variables[opt]
92/// objc-interface-decl-list
93/// @end
94///
95/// objc-category-interface:
96/// '@' 'interface' identifier '(' identifier[opt] ')'
97/// objc-protocol-refs[opt]
98/// objc-interface-decl-list
99/// @end
100///
101/// objc-superclass:
102/// ':' identifier
103///
104/// objc-class-interface-attributes:
105/// __attribute__((visibility("default")))
106/// __attribute__((visibility("hidden")))
107/// __attribute__((deprecated))
108/// __attribute__((unavailable))
109/// __attribute__((objc_exception)) - used by NSException on 64-bit
110///
111Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration(
112 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000113 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Narofffb367882007-08-20 21:31:48 +0000114 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
115 ConsumeToken(); // the "interface" identifier
116
117 if (Tok.getKind() != tok::identifier) {
118 Diag(Tok, diag::err_expected_ident); // missing class or category name.
119 return 0;
120 }
121 // We have a class or category name - consume it.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000122 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Narofffb367882007-08-20 21:31:48 +0000123 SourceLocation nameLoc = ConsumeToken();
124
125 if (Tok.getKind() == tok::l_paren) { // we have a category
126 SourceLocation lparenLoc = ConsumeParen();
127 SourceLocation categoryLoc, rparenLoc;
128 IdentifierInfo *categoryId = 0;
129
130 // OBJC2: The cateogry name is optional (not an error).
131 if (Tok.getKind() == tok::identifier) {
132 categoryId = Tok.getIdentifierInfo();
133 categoryLoc = ConsumeToken();
134 }
135 if (Tok.getKind() != tok::r_paren) {
136 Diag(Tok, diag::err_expected_rparen);
137 SkipUntil(tok::r_paren, false); // don't stop at ';'
138 return 0;
139 }
140 rparenLoc = ConsumeParen();
141 // Next, we need to check for any protocol references.
142 if (Tok.getKind() == tok::less) {
143 if (ParseObjCProtocolReferences())
144 return 0;
145 }
146 if (attrList) // categories don't support attributes.
147 Diag(Tok, diag::err_objc_no_attributes_on_category);
148
Steve Naroff0bbffd82007-08-22 16:35:03 +0000149 ParseObjCInterfaceDeclList(0/*FIXME*/);
Steve Narofffb367882007-08-20 21:31:48 +0000150
Steve Naroff0bbffd82007-08-22 16:35:03 +0000151 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000152 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000153 ConsumeToken(); // the "end" identifier
Steve Narofffb367882007-08-20 21:31:48 +0000154 return 0;
155 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000156 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000157 return 0;
158 }
159 // Parse a class interface.
160 IdentifierInfo *superClassId = 0;
161 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000162
163 // FIXME: temporary hack to grok class names (until we have sema support).
164 llvm::SmallVector<IdentifierInfo *, 1> ClassName;
165 ClassName.push_back(nameId);
166 Actions.ParsedObjcClassDeclaration(CurScope, &ClassName[0], 1);
Steve Narofffb367882007-08-20 21:31:48 +0000167
168 if (Tok.getKind() == tok::colon) { // a super class is specified.
169 ConsumeToken();
170 if (Tok.getKind() != tok::identifier) {
171 Diag(Tok, diag::err_expected_ident); // missing super class name.
172 return 0;
173 }
174 superClassId = Tok.getIdentifierInfo();
175 superClassLoc = ConsumeToken();
176 }
177 // Next, we need to check for any protocol references.
178 if (Tok.getKind() == tok::less) {
179 if (ParseObjCProtocolReferences())
180 return 0;
181 }
Steve Naroffc4474992007-08-21 21:17:12 +0000182 // FIXME: add Actions.StartObjCClassInterface(nameId, superClassId, ...)
Steve Narofffb367882007-08-20 21:31:48 +0000183 if (Tok.getKind() == tok::l_brace)
Steve Naroffc4474992007-08-21 21:17:12 +0000184 ParseObjCClassInstanceVariables(0/*FIXME*/);
Steve Narofffb367882007-08-20 21:31:48 +0000185
Steve Naroff0bbffd82007-08-22 16:35:03 +0000186 ParseObjCInterfaceDeclList(0/*FIXME*/);
187
188 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000189 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000190 ConsumeToken(); // the "end" identifier
Steve Narofffb367882007-08-20 21:31:48 +0000191 return 0;
192 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000193 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000194 return 0;
195}
196
197/// objc-interface-decl-list:
198/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000199/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000200/// objc-interface-decl-list objc-method-requirement [OBJC2]
201/// objc-interface-decl-list objc-method-proto
Steve Narofffb367882007-08-20 21:31:48 +0000202/// objc-interface-decl-list declaration
203/// objc-interface-decl-list ';'
204///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000205/// objc-method-requirement: [OBJC2]
206/// @required
207/// @optional
208///
209void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl) {
210 while (1) {
211 if (Tok.getKind() == tok::at) {
212 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff87c329f2007-08-23 18:16:40 +0000213 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000214
215 if (ocKind == tok::objc_end) { // terminate list
216 return;
217 } else if (ocKind == tok::objc_required) { // protocols only
218 ConsumeToken();
219 continue;
220 } else if (ocKind == tok::objc_optional) { // protocols only
221 ConsumeToken();
222 continue;
223 } else if (ocKind == tok::objc_property) {
224 ParseObjCPropertyDecl(AtLoc);
225 continue;
226 } else {
227 Diag(Tok, diag::err_objc_illegal_interface_qual);
228 ConsumeToken();
229 }
230 }
231 if (Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) {
232 ParseObjCMethodPrototype();
233 continue;
234 }
235 if (Tok.getKind() == tok::semi)
236 ConsumeToken();
237 else if (Tok.getKind() == tok::eof)
238 return;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000239 else
240 // FIXME: as the name implies, this rule allows function definitions.
241 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000242 ParseDeclarationOrFunctionDefinition();
243 }
244}
245
246void Parser::ParseObjCPropertyDecl(SourceLocation atLoc) {
Chris Lattner4b009652007-07-25 00:24:17 +0000247 assert(0 && "Unimp");
248}
Steve Narofffb367882007-08-20 21:31:48 +0000249
Steve Naroff0bbffd82007-08-22 16:35:03 +0000250/// objc-methodproto:
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000251/// objc-instance-method objc-method-decl objc-method-attributes[opt] ';'
252/// objc-class-method objc-method-decl objc-method-attributes[opt] ';'
Steve Naroff0bbffd82007-08-22 16:35:03 +0000253///
254/// objc-instance-method: '-'
255/// objc-class-method: '+'
256///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000257/// objc-method-attributes: [OBJC2]
258/// __attribute__((deprecated))
259///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000260void Parser::ParseObjCMethodPrototype() {
261 assert((Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) &&
262 "expected +/-");
263
264 tok::TokenKind methodType = Tok.getKind();
265 SourceLocation methodLoc = ConsumeToken();
266
267 // FIXME: deal with "context sensitive" protocol qualifiers in prototypes
268 ParseObjCMethodDecl(methodType, methodLoc);
269
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000270 // If attributes exist after the method, parse them.
271 if (Tok.getKind() == tok::kw___attribute)
272 ParseAttributes();
273
Steve Naroff0bbffd82007-08-22 16:35:03 +0000274 // Consume the ';'.
275 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "method proto");
276}
277
278/// objc-selector:
279/// identifier
280/// one of
281/// enum struct union if else while do for switch case default
282/// break continue return goto asm sizeof typeof __alignof
283/// unsigned long const short volatile signed restrict _Complex
284/// in out inout bycopy byref oneway int char float double void _Bool
285///
286IdentifierInfo *Parser::ParseObjCSelector() {
287 tok::TokenKind tKind = Tok.getKind();
288 IdentifierInfo *II = 0;
289
290 if (tKind == tok::identifier ||
291 (tKind >= tok::kw_auto && tKind <= tok::kw__Complex)) {
292 // FIXME: make sure the list of keywords jives with gcc. For example,
293 // the above test does not include in/out/inout/bycopy/byref/oneway.
294 II = Tok.getIdentifierInfo();
295 ConsumeToken();
296 }
297 return II;
298}
299
Steve Naroffa8ee2262007-08-22 23:18:22 +0000300/// objc-type-qualifier: one of
301/// in out inout bycopy byref oneway
302///
303/// FIXME: remove the string compares...
304bool Parser::isObjCTypeQualifier() {
305 if (Tok.getKind() == tok::identifier) {
306 const char *qual = Tok.getIdentifierInfo()->getName();
307 return (strcmp(qual, "in") == 0) || (strcmp(qual, "out") == 0) ||
308 (strcmp(qual, "inout") == 0) || (strcmp(qual, "oneway") == 0) ||
309 (strcmp(qual, "bycopy") == 0) || (strcmp(qual, "byref") == 0);
310 }
311 return false;
312}
313
Steve Naroff0bbffd82007-08-22 16:35:03 +0000314/// objc-type-name:
315/// '(' objc-type-qualifiers[opt] type-name ')'
316/// '(' objc-type-qualifiers[opt] ')'
317///
318/// objc-type-qualifiers:
319/// objc-type-qualifier
320/// objc-type-qualifiers objc-type-qualifier
321///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000322void Parser::ParseObjCTypeName() {
323 assert(Tok.getKind() == tok::l_paren && "expected (");
324
325 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
326
Steve Naroffa8ee2262007-08-22 23:18:22 +0000327 while (isObjCTypeQualifier())
328 ConsumeToken();
329
Steve Naroff0bbffd82007-08-22 16:35:03 +0000330 if (isTypeSpecifierQualifier()) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000331 //TypeTy *Ty = ParseTypeName();
332 //assert(Ty && "Parser::ParseObjCTypeName(): missing type");
333 ParseTypeName(); // FIXME: when sema support is added.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000334 }
335 if (Tok.getKind() != tok::r_paren) {
336 MatchRHSPunctuation(tok::r_paren, LParenLoc);
337 return;
338 }
339 RParenLoc = ConsumeParen();
340}
341
342/// objc-method-decl:
343/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000344/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000345/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000346/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000347///
348/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000349/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000350/// objc-keyword-selector objc-keyword-decl
351///
352/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000353/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
354/// objc-selector ':' objc-keyword-attributes[opt] identifier
355/// ':' objc-type-name objc-keyword-attributes[opt] identifier
356/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000357///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000358/// objc-parmlist:
359/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000360///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000361/// objc-parms:
362/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000363///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000364/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000365/// , ...
366///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000367/// objc-keyword-attributes: [OBJC2]
368/// __attribute__((unused))
369///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000370void Parser::ParseObjCMethodDecl(tok::TokenKind mType, SourceLocation mLoc) {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000371
372 // Parse the return type.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000373 if (Tok.getKind() == tok::l_paren)
374 ParseObjCTypeName();
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000375 IdentifierInfo *selIdent = ParseObjCSelector();
376
377 if (Tok.getKind() == tok::colon) {
378 IdentifierInfo *keywordSelector = selIdent;
379 while (1) {
380 // Each iteration parses a single keyword argument.
381 if (Tok.getKind() != tok::colon) {
382 Diag(Tok, diag::err_expected_colon);
383 break;
384 }
385 ConsumeToken(); // Eat the ':'.
386 if (Tok.getKind() == tok::l_paren) // Parse the argument type.
387 ParseObjCTypeName();
Steve Naroff72f17fb2007-08-22 22:17:26 +0000388
389 // If attributes exist before the argument name, parse them.
390 if (Tok.getKind() == tok::kw___attribute)
391 ParseAttributes();
392
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000393 if (Tok.getKind() != tok::identifier) {
394 Diag(Tok, diag::err_expected_ident); // missing argument name.
395 break;
396 }
397 ConsumeToken(); // Eat the identifier.
398 // FIXME: add Actions.BuildObjCKeyword()
399
400 keywordSelector = ParseObjCSelector();
401 if (!keywordSelector && Tok.getKind() != tok::colon)
402 break;
403 // We have a selector or a colon, continue parsing.
404 }
405 // Parse the (optional) parameter list.
406 while (Tok.getKind() == tok::comma) {
407 ConsumeToken();
408 if (Tok.getKind() == tok::ellipsis) {
409 ConsumeToken();
410 break;
411 }
412 ParseDeclaration(Declarator::PrototypeContext);
413 }
414 } else if (!selIdent) {
415 Diag(Tok, diag::err_expected_ident); // missing selector name.
416 }
417 // FIXME: add Actions.BuildMethodSignature().
Steve Naroff0bbffd82007-08-22 16:35:03 +0000418}
419
Steve Narofffb367882007-08-20 21:31:48 +0000420/// objc-protocol-refs:
421/// '<' identifier-list '>'
422///
423bool Parser::ParseObjCProtocolReferences() {
424 assert(Tok.getKind() == tok::less && "expected <");
425
426 ConsumeToken(); // the "<"
427 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
428
429 while (1) {
430 if (Tok.getKind() != tok::identifier) {
431 Diag(Tok, diag::err_expected_ident);
432 SkipUntil(tok::greater);
433 return true;
434 }
435 ProtocolRefs.push_back(Tok.getIdentifierInfo());
436 ConsumeToken();
437
438 if (Tok.getKind() != tok::comma)
439 break;
440 ConsumeToken();
441 }
442 // Consume the '>'.
443 return ExpectAndConsume(tok::greater, diag::err_expected_greater);
444}
445
446/// objc-class-instance-variables:
447/// '{' objc-instance-variable-decl-list[opt] '}'
448///
449/// objc-instance-variable-decl-list:
450/// objc-visibility-spec
451/// objc-instance-variable-decl ';'
452/// ';'
453/// objc-instance-variable-decl-list objc-visibility-spec
454/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
455/// objc-instance-variable-decl-list ';'
456///
457/// objc-visibility-spec:
458/// @private
459/// @protected
460/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000461/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000462///
463/// objc-instance-variable-decl:
464/// struct-declaration
465///
Steve Naroffc4474992007-08-21 21:17:12 +0000466void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl) {
467 assert(Tok.getKind() == tok::l_brace && "expected {");
468
469 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
470 llvm::SmallVector<DeclTy*, 32> IvarDecls;
471
472 // While we still have something to read, read the instance variables.
473 while (Tok.getKind() != tok::r_brace &&
474 Tok.getKind() != tok::eof) {
475 // Each iteration of this loop reads one objc-instance-variable-decl.
476
477 // Check for extraneous top-level semicolon.
478 if (Tok.getKind() == tok::semi) {
479 Diag(Tok, diag::ext_extra_struct_semi);
480 ConsumeToken();
481 continue;
482 }
483 // Set the default visibility to private.
484 tok::ObjCKeywordKind visibility = tok::objc_private;
485 if (Tok.getKind() == tok::at) { // parse objc-visibility-spec
486 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000487 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000488 case tok::objc_private:
489 case tok::objc_public:
490 case tok::objc_protected:
491 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000492 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000493 ConsumeToken();
494 continue;
495 default:
496 Diag(Tok, diag::err_objc_illegal_visibility_spec);
497 ConsumeToken();
498 continue;
499 }
500 }
501 ParseStructDeclaration(interfaceDecl, IvarDecls);
502
503 if (Tok.getKind() == tok::semi) {
504 ConsumeToken();
505 } else if (Tok.getKind() == tok::r_brace) {
506 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
507 break;
508 } else {
509 Diag(Tok, diag::err_expected_semi_decl_list);
510 // Skip to end of block or statement
511 SkipUntil(tok::r_brace, true, true);
512 }
513 }
514 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
515 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000516}
Steve Narofffb367882007-08-20 21:31:48 +0000517
518/// objc-protocol-declaration:
519/// objc-protocol-definition
520/// objc-protocol-forward-reference
521///
522/// objc-protocol-definition:
523/// @protocol identifier
524/// objc-protocol-refs[opt]
525/// objc-methodprotolist
526/// @end
527///
528/// objc-protocol-forward-reference:
529/// @protocol identifier-list ';'
530///
531/// "@protocol identifier ;" should be resolved as "@protocol
532/// identifier-list ;": objc-methodprotolist may not start with a
533/// semicolon in the first alternative if objc-protocol-refs are omitted.
534
Steve Naroff72f17fb2007-08-22 22:17:26 +0000535Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000536 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000537 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
538 ConsumeToken(); // the "protocol" identifier
539
540 if (Tok.getKind() != tok::identifier) {
541 Diag(Tok, diag::err_expected_ident); // missing protocol name.
542 return 0;
543 }
544 // Save the protocol name, then consume it.
545 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
546 SourceLocation nameLoc = ConsumeToken();
547
548 if (Tok.getKind() == tok::semi) { // forward declaration.
549 ConsumeToken();
550 return 0; // FIXME: add protocolName
551 }
552 if (Tok.getKind() == tok::comma) { // list of forward declarations.
553 // Parse the list of forward declarations.
554 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
555 ProtocolRefs.push_back(protocolName);
556
557 while (1) {
558 ConsumeToken(); // the ','
559 if (Tok.getKind() != tok::identifier) {
560 Diag(Tok, diag::err_expected_ident);
561 SkipUntil(tok::semi);
562 return 0;
563 }
564 ProtocolRefs.push_back(Tok.getIdentifierInfo());
565 ConsumeToken(); // the identifier
566
567 if (Tok.getKind() != tok::comma)
568 break;
569 }
570 // Consume the ';'.
571 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
572 return 0;
573 return 0; // FIXME
574 }
575 // Last, and definitely not least, parse a protocol declaration.
576 if (Tok.getKind() == tok::less) {
577 if (ParseObjCProtocolReferences())
578 return 0;
579 }
580 ParseObjCInterfaceDeclList(0/*FIXME*/);
581
582 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000583 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000584 ConsumeToken(); // the "end" identifier
585 return 0;
586 }
587 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000588 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000589}
Steve Narofffb367882007-08-20 21:31:48 +0000590
591/// objc-implementation:
592/// objc-class-implementation-prologue
593/// objc-category-implementation-prologue
594///
595/// objc-class-implementation-prologue:
596/// @implementation identifier objc-superclass[opt]
597/// objc-class-instance-variables[opt]
598///
599/// objc-category-implementation-prologue:
600/// @implementation identifier ( identifier )
601
602Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration() {
Chris Lattner4b009652007-07-25 00:24:17 +0000603 assert(0 && "Unimp");
Steve Narofffb367882007-08-20 21:31:48 +0000604 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000605}
Steve Narofffb367882007-08-20 21:31:48 +0000606Parser::DeclTy *Parser::ParseObjCAtEndDeclaration() {
Chris Lattner4b009652007-07-25 00:24:17 +0000607 assert(0 && "Unimp");
Steve Narofffb367882007-08-20 21:31:48 +0000608 return 0;
609}
610Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration() {
611 assert(0 && "Unimp");
612 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000613}
614
Steve Naroff72f17fb2007-08-22 22:17:26 +0000615void Parser::ParseObjCInstanceMethodDefinition() {
616 assert(0 && "Parser::ParseObjCInstanceMethodDefinition():: Unimp");
Chris Lattner4b009652007-07-25 00:24:17 +0000617}
618
Steve Naroff72f17fb2007-08-22 22:17:26 +0000619void Parser::ParseObjCClassMethodDefinition() {
620 assert(0 && "Parser::ParseObjCClassMethodDefinition():: Unimp");
Chris Lattner4b009652007-07-25 00:24:17 +0000621}
Anders Carlssona66cad42007-08-21 17:43:55 +0000622
623Parser::ExprResult Parser::ParseObjCExpression() {
624 SourceLocation AtLoc = ConsumeToken(); // the "@"
625
626 switch (Tok.getKind()) {
627 case tok::string_literal: // primary-expression: string-literal
628 case tok::wide_string_literal:
629 return ParseObjCStringLiteral();
Anders Carlsson2996b4e2007-08-23 15:25:28 +0000630 default:
631 break;
632 }
633
634 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Anders Carlsson8be1d402007-08-22 15:14:15 +0000635 case tok::objc_encode:
636 return ParseObjCEncodeExpression();
Anders Carlsson2996b4e2007-08-23 15:25:28 +0000637 case tok::objc_protocol:
638 return ParseObjCProtocolExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +0000639 default:
640 Diag(AtLoc, diag::err_unexpected_at);
641 SkipUntil(tok::semi);
642 break;
643 }
644
645 return 0;
646}
647
648Parser::ExprResult Parser::ParseObjCStringLiteral() {
649 ExprResult Res = ParseStringLiteralExpression();
650
651 if (Res.isInvalid) return Res;
652
653 return Actions.ParseObjCStringLiteral(Res.Val);
654}
Anders Carlsson8be1d402007-08-22 15:14:15 +0000655
656/// objc-encode-expression:
657/// @encode ( type-name )
658Parser::ExprResult Parser::ParseObjCEncodeExpression() {
Steve Naroff87c329f2007-08-23 18:16:40 +0000659 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +0000660
661 SourceLocation EncLoc = ConsumeToken();
662
663 if (Tok.getKind() != tok::l_paren) {
664 Diag(Tok, diag::err_expected_lparen_after, "@encode");
665 return true;
666 }
667
668 SourceLocation LParenLoc = ConsumeParen();
669
670 TypeTy *Ty = ParseTypeName();
671
Anders Carlsson92faeb82007-08-23 15:31:37 +0000672 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +0000673
674 return Actions.ParseObjCEncodeExpression(EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +0000675 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +0000676}
Anders Carlsson2996b4e2007-08-23 15:25:28 +0000677
678/// objc-protocol-expression
679/// @protocol ( protocol-name )
680
681Parser::ExprResult Parser::ParseObjCProtocolExpression()
682{
683 SourceLocation ProtoLoc = ConsumeToken();
684
685 if (Tok.getKind() != tok::l_paren) {
686 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
687 return true;
688 }
689
690 SourceLocation LParenLoc = ConsumeParen();
691
692 if (Tok.getKind() != tok::identifier) {
693 Diag(Tok, diag::err_expected_ident);
694 return true;
695 }
696
697 // FIXME: Do something with the protocol name
698 ConsumeToken();
699
Anders Carlsson92faeb82007-08-23 15:31:37 +0000700 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +0000701
702 // FIXME
703 return 0;
704}