Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- 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 Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 15 | #include "clang/Parse/DeclSpec.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Diagnostic.h" |
| 17 | #include "llvm/ADT/SmallVector.h" |
| 18 | using 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 Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 29 | Parser::DeclTy *Parser::ParseObjCAtDirectives() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | SourceLocation AtLoc = ConsumeToken(); // the "@" |
| 31 | |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 32 | switch (Tok.getObjCKeywordID()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | case tok::objc_class: |
| 34 | return ParseObjCAtClassDeclaration(AtLoc); |
| 35 | case tok::objc_interface: |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 36 | return ParseObjCAtInterfaceDeclaration(AtLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 37 | case tok::objc_protocol: |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 38 | return ParseObjCAtProtocolDeclaration(AtLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | case tok::objc_implementation: |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 40 | return ObjcImpDecl = ParseObjCAtImplementationDeclaration(AtLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | case tok::objc_end: |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 42 | return ParseObjCAtEndDeclaration(AtLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | case tok::objc_compatibility_alias: |
Fariborz Jahanian | e992af0 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 44 | return ParseObjCAtAliasDeclaration(AtLoc); |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 45 | case tok::objc_synthesize: |
| 46 | return ParseObjCPropertySynthesize(AtLoc); |
| 47 | case tok::objc_dynamic: |
| 48 | return ParseObjCPropertyDynamic(AtLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 49 | default: |
| 50 | Diag(AtLoc, diag::err_unexpected_at); |
| 51 | SkipUntil(tok::semi); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 52 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | /// |
| 57 | /// objc-class-declaration: |
| 58 | /// '@' 'class' identifier-list ';' |
| 59 | /// |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 60 | Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 61 | ConsumeToken(); // the identifier "class" |
| 62 | llvm::SmallVector<IdentifierInfo *, 8> ClassNames; |
| 63 | |
| 64 | while (1) { |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 65 | if (Tok.isNot(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 66 | Diag(Tok, diag::err_expected_ident); |
| 67 | SkipUntil(tok::semi); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 68 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 69 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 70 | ClassNames.push_back(Tok.getIdentifierInfo()); |
| 71 | ConsumeToken(); |
| 72 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 73 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 74 | break; |
| 75 | |
| 76 | ConsumeToken(); |
| 77 | } |
| 78 | |
| 79 | // Consume the ';'. |
| 80 | if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class")) |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 81 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 82 | |
Steve Naroff | e440eb8 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 83 | return Actions.ActOnForwardClassDeclaration(atLoc, |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 84 | &ClassNames[0], ClassNames.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 87 | /// |
| 88 | /// objc-interface: |
| 89 | /// objc-class-interface-attributes[opt] objc-class-interface |
| 90 | /// objc-category-interface |
| 91 | /// |
| 92 | /// objc-class-interface: |
| 93 | /// '@' 'interface' identifier objc-superclass[opt] |
| 94 | /// objc-protocol-refs[opt] |
| 95 | /// objc-class-instance-variables[opt] |
| 96 | /// objc-interface-decl-list |
| 97 | /// @end |
| 98 | /// |
| 99 | /// objc-category-interface: |
| 100 | /// '@' 'interface' identifier '(' identifier[opt] ')' |
| 101 | /// objc-protocol-refs[opt] |
| 102 | /// objc-interface-decl-list |
| 103 | /// @end |
| 104 | /// |
| 105 | /// objc-superclass: |
| 106 | /// ':' identifier |
| 107 | /// |
| 108 | /// objc-class-interface-attributes: |
| 109 | /// __attribute__((visibility("default"))) |
| 110 | /// __attribute__((visibility("hidden"))) |
| 111 | /// __attribute__((deprecated)) |
| 112 | /// __attribute__((unavailable)) |
| 113 | /// __attribute__((objc_exception)) - used by NSException on 64-bit |
| 114 | /// |
| 115 | Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration( |
| 116 | SourceLocation atLoc, AttributeList *attrList) { |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 117 | assert(Tok.isObjCAtKeyword(tok::objc_interface) && |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 118 | "ParseObjCAtInterfaceDeclaration(): Expected @interface"); |
| 119 | ConsumeToken(); // the "interface" identifier |
| 120 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 121 | if (Tok.isNot(tok::identifier)) { |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 122 | Diag(Tok, diag::err_expected_ident); // missing class or category name. |
| 123 | return 0; |
| 124 | } |
| 125 | // We have a class or category name - consume it. |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 126 | IdentifierInfo *nameId = Tok.getIdentifierInfo(); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 127 | SourceLocation nameLoc = ConsumeToken(); |
| 128 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 129 | if (Tok.is(tok::l_paren)) { // we have a category. |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 130 | SourceLocation lparenLoc = ConsumeParen(); |
| 131 | SourceLocation categoryLoc, rparenLoc; |
| 132 | IdentifierInfo *categoryId = 0; |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 133 | llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs; |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 134 | |
Steve Naroff | 527fe23 | 2007-08-23 19:56:30 +0000 | [diff] [blame] | 135 | // For ObjC2, the category name is optional (not an error). |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 136 | if (Tok.is(tok::identifier)) { |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 137 | categoryId = Tok.getIdentifierInfo(); |
| 138 | categoryLoc = ConsumeToken(); |
Steve Naroff | 527fe23 | 2007-08-23 19:56:30 +0000 | [diff] [blame] | 139 | } else if (!getLang().ObjC2) { |
| 140 | Diag(Tok, diag::err_expected_ident); // missing category name. |
| 141 | return 0; |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 142 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 143 | if (Tok.isNot(tok::r_paren)) { |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 144 | Diag(Tok, diag::err_expected_rparen); |
| 145 | SkipUntil(tok::r_paren, false); // don't stop at ';' |
| 146 | return 0; |
| 147 | } |
| 148 | rparenLoc = ConsumeParen(); |
| 149 | // Next, we need to check for any protocol references. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 150 | if (Tok.is(tok::less)) { |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 151 | if (ParseObjCProtocolReferences(ProtocolRefs)) |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | if (attrList) // categories don't support attributes. |
| 155 | Diag(Tok, diag::err_objc_no_attributes_on_category); |
| 156 | |
Steve Naroff | e440eb8 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 157 | DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc, |
Steve Naroff | 3a165b0 | 2007-10-03 21:00:46 +0000 | [diff] [blame] | 158 | nameId, nameLoc, categoryId, categoryLoc, |
| 159 | &ProtocolRefs[0], ProtocolRefs.size()); |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 160 | |
| 161 | ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 162 | |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 163 | // The @ sign was already consumed by ParseObjCInterfaceDeclList(). |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 164 | if (Tok.isObjCAtKeyword(tok::objc_end)) { |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 165 | ConsumeToken(); // the "end" identifier |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 166 | return CategoryType; |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 167 | } |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 168 | Diag(Tok, diag::err_objc_missing_end); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | // Parse a class interface. |
| 172 | IdentifierInfo *superClassId = 0; |
| 173 | SourceLocation superClassLoc; |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 174 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 175 | if (Tok.is(tok::colon)) { // a super class is specified. |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 176 | ConsumeToken(); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 177 | if (Tok.isNot(tok::identifier)) { |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 178 | Diag(Tok, diag::err_expected_ident); // missing super class name. |
| 179 | return 0; |
| 180 | } |
| 181 | superClassId = Tok.getIdentifierInfo(); |
| 182 | superClassLoc = ConsumeToken(); |
| 183 | } |
| 184 | // Next, we need to check for any protocol references. |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 185 | llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs; |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 186 | if (Tok.is(tok::less)) { |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 187 | if (ParseObjCProtocolReferences(ProtocolRefs)) |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 188 | return 0; |
| 189 | } |
Steve Naroff | e440eb8 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 190 | DeclTy *ClsType = Actions.ActOnStartClassInterface( |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 191 | atLoc, nameId, nameLoc, |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 192 | superClassId, superClassLoc, &ProtocolRefs[0], |
| 193 | ProtocolRefs.size(), attrList); |
| 194 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 195 | if (Tok.is(tok::l_brace)) |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 196 | ParseObjCClassInstanceVariables(ClsType); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 197 | |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 198 | ParseObjCInterfaceDeclList(ClsType, tok::objc_interface); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 199 | |
| 200 | // The @ sign was already consumed by ParseObjCInterfaceDeclList(). |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 201 | if (Tok.isObjCAtKeyword(tok::objc_end)) { |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 202 | ConsumeToken(); // the "end" identifier |
Steve Naroff | 2bd42fa | 2007-09-10 20:51:04 +0000 | [diff] [blame] | 203 | return ClsType; |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 204 | } |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 205 | Diag(Tok, diag::err_objc_missing_end); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | /// objc-interface-decl-list: |
| 210 | /// empty |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 211 | /// objc-interface-decl-list objc-property-decl [OBJC2] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 212 | /// objc-interface-decl-list objc-method-requirement [OBJC2] |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 213 | /// objc-interface-decl-list objc-method-proto ';' |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 214 | /// objc-interface-decl-list declaration |
| 215 | /// objc-interface-decl-list ';' |
| 216 | /// |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 217 | /// objc-method-requirement: [OBJC2] |
| 218 | /// @required |
| 219 | /// @optional |
| 220 | /// |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 221 | void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl, |
| 222 | tok::ObjCKeywordKind contextKey) { |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 223 | llvm::SmallVector<DeclTy*, 32> allMethods; |
Fariborz Jahanian | 0093359 | 2007-09-18 00:25:23 +0000 | [diff] [blame] | 224 | tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword; |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 225 | while (1) { |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 226 | if (Tok.is(tok::at)) { |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 227 | SourceLocation AtLoc = ConsumeToken(); // the "@" |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 228 | tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID(); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 229 | |
| 230 | if (ocKind == tok::objc_end) { // terminate list |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 231 | break; |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 232 | } else if (ocKind == tok::objc_required) { // protocols only |
| 233 | ConsumeToken(); |
Fariborz Jahanian | 0093359 | 2007-09-18 00:25:23 +0000 | [diff] [blame] | 234 | MethodImplKind = ocKind; |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 235 | if (contextKey != tok::objc_protocol) |
| 236 | Diag(AtLoc, diag::err_objc_protocol_required); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 237 | } else if (ocKind == tok::objc_optional) { // protocols only |
| 238 | ConsumeToken(); |
Fariborz Jahanian | 0093359 | 2007-09-18 00:25:23 +0000 | [diff] [blame] | 239 | MethodImplKind = ocKind; |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 240 | if (contextKey != tok::objc_protocol) |
| 241 | Diag(AtLoc, diag::err_objc_protocol_optional); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 242 | } else if (ocKind == tok::objc_property) { |
Fariborz Jahanian | e55cd00 | 2007-09-12 18:23:47 +0000 | [diff] [blame] | 243 | ParseObjCPropertyDecl(interfaceDecl); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 244 | continue; |
| 245 | } else { |
| 246 | Diag(Tok, diag::err_objc_illegal_interface_qual); |
| 247 | ConsumeToken(); |
| 248 | } |
| 249 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 250 | if (Tok.is(tok::minus) || Tok.is(tok::plus)) { |
| 251 | DeclTy *methodPrototype = |
| 252 | ParseObjCMethodPrototype(interfaceDecl, MethodImplKind); |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 253 | allMethods.push_back(methodPrototype); |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 254 | // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for |
| 255 | // method definitions. |
Steve Naroff | d16245b | 2007-09-17 15:07:43 +0000 | [diff] [blame] | 256 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto"); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 257 | continue; |
| 258 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 259 | if (Tok.is(tok::semi)) |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 260 | ConsumeToken(); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 261 | else if (Tok.is(tok::eof)) |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 262 | break; |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 263 | else { |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 264 | // FIXME: as the name implies, this rule allows function definitions. |
| 265 | // We could pass a flag or check for functions during semantic analysis. |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 266 | ParseDeclarationOrFunctionDefinition(); |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 267 | } |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 268 | } |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 269 | /// Insert collected methods declarations into the @interface object. |
Steve Naroff | 3a165b0 | 2007-10-03 21:00:46 +0000 | [diff] [blame] | 270 | Actions.ActOnAddMethodsToObjcDecl(CurScope, interfaceDecl, |
| 271 | &allMethods[0], allMethods.size()); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 274 | /// Parse property attribute declarations. |
| 275 | /// |
| 276 | /// property-attr-decl: '(' property-attrlist ')' |
| 277 | /// property-attrlist: |
| 278 | /// property-attribute |
| 279 | /// property-attrlist ',' property-attribute |
| 280 | /// property-attribute: |
| 281 | /// getter '=' identifier |
| 282 | /// setter '=' identifier ':' |
| 283 | /// readonly |
| 284 | /// readwrite |
| 285 | /// assign |
| 286 | /// retain |
| 287 | /// copy |
| 288 | /// nonatomic |
| 289 | /// |
| 290 | void Parser::ParseObjCPropertyAttribute (DeclTy *interfaceDecl) { |
| 291 | SourceLocation loc = ConsumeParen(); // consume '(' |
| 292 | while (isObjCPropertyAttribute()) { |
| 293 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 294 | // getter/setter require extra treatment. |
| 295 | if (II == ObjcPropertyAttrs[objc_getter] || |
| 296 | II == ObjcPropertyAttrs[objc_setter]) { |
| 297 | // skip getter/setter part. |
| 298 | SourceLocation loc = ConsumeToken(); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 299 | if (Tok.is(tok::equal)) { |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 300 | loc = ConsumeToken(); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 301 | if (Tok.is(tok::identifier)) { |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 302 | if (II == ObjcPropertyAttrs[objc_setter]) { |
| 303 | loc = ConsumeToken(); // consume method name |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 304 | if (Tok.isNot(tok::colon)) { |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 305 | Diag(loc, diag::err_expected_colon); |
| 306 | SkipUntil(tok::r_paren,true,true); |
| 307 | break; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | else { |
| 312 | Diag(loc, diag::err_expected_ident); |
| 313 | SkipUntil(tok::r_paren,true,true); |
| 314 | break; |
| 315 | } |
| 316 | } |
| 317 | else { |
| 318 | Diag(loc, diag::err_objc_expected_equal); |
| 319 | SkipUntil(tok::r_paren,true,true); |
| 320 | break; |
| 321 | } |
| 322 | } |
| 323 | ConsumeToken(); // consume last attribute token |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 324 | if (Tok.is(tok::comma)) { |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 325 | loc = ConsumeToken(); |
| 326 | continue; |
| 327 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 328 | if (Tok.is(tok::r_paren)) |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 329 | break; |
| 330 | Diag(loc, diag::err_expected_rparen); |
| 331 | SkipUntil(tok::semi); |
| 332 | return; |
| 333 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 334 | if (Tok.is(tok::r_paren)) |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 335 | ConsumeParen(); |
| 336 | else { |
| 337 | Diag(loc, diag::err_objc_expected_property_attr); |
| 338 | SkipUntil(tok::r_paren); // recover from error inside attribute list |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /// Main routine to parse property declaration. |
| 343 | /// |
| 344 | /// @property property-attr-decl[opt] property-component-decl ';' |
| 345 | /// |
| 346 | void Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl) { |
| 347 | assert(Tok.isObjCAtKeyword(tok::objc_property) && |
| 348 | "ParseObjCPropertyDecl(): Expected @property"); |
| 349 | ConsumeToken(); // the "property" identifier |
| 350 | // Parse property attribute list, if any. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 351 | if (Tok.is(tok::l_paren)) { |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 352 | // property has attribute list. |
| 353 | ParseObjCPropertyAttribute(0/*FIXME*/); |
| 354 | } |
| 355 | // Parse declaration portion of @property. |
| 356 | llvm::SmallVector<DeclTy*, 32> PropertyDecls; |
| 357 | ParseStructDeclaration(interfaceDecl, PropertyDecls); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 358 | if (Tok.is(tok::semi)) |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 359 | ConsumeToken(); |
| 360 | else { |
| 361 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 362 | SkipUntil(tok::r_brace, true, true); |
| 363 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 364 | } |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 365 | |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 366 | /// objc-method-proto: |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 367 | /// objc-instance-method objc-method-decl objc-method-attributes[opt] |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 368 | /// objc-class-method objc-method-decl objc-method-attributes[opt] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 369 | /// |
| 370 | /// objc-instance-method: '-' |
| 371 | /// objc-class-method: '+' |
| 372 | /// |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 373 | /// objc-method-attributes: [OBJC2] |
| 374 | /// __attribute__((deprecated)) |
| 375 | /// |
Fariborz Jahanian | 0093359 | 2007-09-18 00:25:23 +0000 | [diff] [blame] | 376 | Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl, |
| 377 | tok::ObjCKeywordKind MethodImplKind) { |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 378 | assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-"); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 379 | |
| 380 | tok::TokenKind methodType = Tok.getKind(); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 381 | ConsumeToken(); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 382 | |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 383 | DeclTy *MDecl = ParseObjCMethodDecl(methodType, MethodImplKind); |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 384 | // Since this rule is used for both method declarations and definitions, |
Steve Naroff | 2bd42fa | 2007-09-10 20:51:04 +0000 | [diff] [blame] | 385 | // the caller is (optionally) responsible for consuming the ';'. |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 386 | return MDecl; |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /// objc-selector: |
| 390 | /// identifier |
| 391 | /// one of |
| 392 | /// enum struct union if else while do for switch case default |
| 393 | /// break continue return goto asm sizeof typeof __alignof |
| 394 | /// unsigned long const short volatile signed restrict _Complex |
| 395 | /// in out inout bycopy byref oneway int char float double void _Bool |
| 396 | /// |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 397 | IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) { |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 398 | switch (Tok.getKind()) { |
| 399 | default: |
| 400 | return 0; |
| 401 | case tok::identifier: |
| 402 | case tok::kw_typeof: |
| 403 | case tok::kw___alignof: |
| 404 | case tok::kw_auto: |
| 405 | case tok::kw_break: |
| 406 | case tok::kw_case: |
| 407 | case tok::kw_char: |
| 408 | case tok::kw_const: |
| 409 | case tok::kw_continue: |
| 410 | case tok::kw_default: |
| 411 | case tok::kw_do: |
| 412 | case tok::kw_double: |
| 413 | case tok::kw_else: |
| 414 | case tok::kw_enum: |
| 415 | case tok::kw_extern: |
| 416 | case tok::kw_float: |
| 417 | case tok::kw_for: |
| 418 | case tok::kw_goto: |
| 419 | case tok::kw_if: |
| 420 | case tok::kw_inline: |
| 421 | case tok::kw_int: |
| 422 | case tok::kw_long: |
| 423 | case tok::kw_register: |
| 424 | case tok::kw_restrict: |
| 425 | case tok::kw_return: |
| 426 | case tok::kw_short: |
| 427 | case tok::kw_signed: |
| 428 | case tok::kw_sizeof: |
| 429 | case tok::kw_static: |
| 430 | case tok::kw_struct: |
| 431 | case tok::kw_switch: |
| 432 | case tok::kw_typedef: |
| 433 | case tok::kw_union: |
| 434 | case tok::kw_unsigned: |
| 435 | case tok::kw_void: |
| 436 | case tok::kw_volatile: |
| 437 | case tok::kw_while: |
| 438 | case tok::kw__Bool: |
| 439 | case tok::kw__Complex: |
| 440 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 441 | SelectorLoc = ConsumeToken(); |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 442 | return II; |
Fariborz Jahanian | d064951 | 2007-09-27 19:52:15 +0000 | [diff] [blame] | 443 | } |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Steve Naroff | 4fa7afd | 2007-08-22 23:18:22 +0000 | [diff] [blame] | 446 | /// objc-type-qualifier: one of |
| 447 | /// in out inout bycopy byref oneway |
| 448 | /// |
Steve Naroff | 4fa7afd | 2007-08-22 23:18:22 +0000 | [diff] [blame] | 449 | bool Parser::isObjCTypeQualifier() { |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 450 | if (Tok.is(tok::identifier)) { |
Chris Lattner | 34870da | 2007-08-29 22:54:08 +0000 | [diff] [blame] | 451 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 452 | for (unsigned i = 0; i < objc_NumQuals; ++i) |
| 453 | if (II == ObjcTypeQuals[i]) return true; |
Steve Naroff | 4fa7afd | 2007-08-22 23:18:22 +0000 | [diff] [blame] | 454 | } |
| 455 | return false; |
| 456 | } |
| 457 | |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 458 | /// property-attrlist: one of |
| 459 | /// readonly getter setter assign retain copy nonatomic |
| 460 | /// |
| 461 | bool Parser::isObjCPropertyAttribute() { |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 462 | if (Tok.is(tok::identifier)) { |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 463 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 464 | for (unsigned i = 0; i < objc_NumAttrs; ++i) |
| 465 | if (II == ObjcPropertyAttrs[i]) return true; |
| 466 | } |
| 467 | return false; |
| 468 | } |
| 469 | |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 470 | /// objc-type-name: |
| 471 | /// '(' objc-type-qualifiers[opt] type-name ')' |
| 472 | /// '(' objc-type-qualifiers[opt] ')' |
| 473 | /// |
| 474 | /// objc-type-qualifiers: |
| 475 | /// objc-type-qualifier |
| 476 | /// objc-type-qualifiers objc-type-qualifier |
| 477 | /// |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 478 | Parser::TypeTy *Parser::ParseObjCTypeName() { |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 479 | assert(Tok.is(tok::l_paren) && "expected ("); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 480 | |
| 481 | SourceLocation LParenLoc = ConsumeParen(), RParenLoc; |
Chris Lattner | 271f1a6 | 2007-09-27 15:15:46 +0000 | [diff] [blame] | 482 | TypeTy *Ty = 0; |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 483 | |
Steve Naroff | 4fa7afd | 2007-08-22 23:18:22 +0000 | [diff] [blame] | 484 | while (isObjCTypeQualifier()) |
| 485 | ConsumeToken(); |
| 486 | |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 487 | if (isTypeSpecifierQualifier()) { |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 488 | Ty = ParseTypeName(); |
| 489 | // FIXME: back when Sema support is in place... |
| 490 | // assert(Ty && "Parser::ParseObjCTypeName(): missing type"); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 491 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 492 | if (Tok.isNot(tok::r_paren)) { |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 493 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 494 | return 0; // FIXME: decide how we want to handle this error... |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 495 | } |
| 496 | RParenLoc = ConsumeParen(); |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 497 | return Ty; |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | /// objc-method-decl: |
| 501 | /// objc-selector |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 502 | /// objc-keyword-selector objc-parmlist[opt] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 503 | /// objc-type-name objc-selector |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 504 | /// objc-type-name objc-keyword-selector objc-parmlist[opt] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 505 | /// |
| 506 | /// objc-keyword-selector: |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 507 | /// objc-keyword-decl |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 508 | /// objc-keyword-selector objc-keyword-decl |
| 509 | /// |
| 510 | /// objc-keyword-decl: |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 511 | /// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier |
| 512 | /// objc-selector ':' objc-keyword-attributes[opt] identifier |
| 513 | /// ':' objc-type-name objc-keyword-attributes[opt] identifier |
| 514 | /// ':' objc-keyword-attributes[opt] identifier |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 515 | /// |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 516 | /// objc-parmlist: |
| 517 | /// objc-parms objc-ellipsis[opt] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 518 | /// |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 519 | /// objc-parms: |
| 520 | /// objc-parms , parameter-declaration |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 521 | /// |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 522 | /// objc-ellipsis: |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 523 | /// , ... |
| 524 | /// |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 525 | /// objc-keyword-attributes: [OBJC2] |
| 526 | /// __attribute__((unused)) |
| 527 | /// |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 528 | Parser::DeclTy *Parser::ParseObjCMethodDecl(tok::TokenKind mType, |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 529 | tok::ObjCKeywordKind MethodImplKind) |
| 530 | { |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 531 | // Parse the return type. |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 532 | TypeTy *ReturnType = 0; |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 533 | if (Tok.is(tok::l_paren)) |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 534 | ReturnType = ParseObjCTypeName(); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 535 | SourceLocation mLoc; |
| 536 | IdentifierInfo *SelIdent = ParseObjCSelector(mLoc); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 537 | if (Tok.isNot(tok::colon)) { |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 538 | if (!SelIdent) { |
| 539 | Diag(Tok, diag::err_expected_ident); // missing selector name. |
| 540 | // FIXME: this creates a unary selector with a null identifier, is this |
| 541 | // ok?? Maybe we should skip to the next semicolon or something. |
| 542 | } |
| 543 | |
| 544 | // If attributes exist after the method, parse them. |
| 545 | AttributeList *MethodAttrs = 0; |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 546 | if (getLang().ObjC2 && Tok.is(tok::kw___attribute)) |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 547 | MethodAttrs = ParseAttributes(); |
| 548 | |
| 549 | Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent); |
| 550 | return Actions.ActOnMethodDeclaration(mLoc, mType, ReturnType, Sel, |
| 551 | 0, 0, MethodAttrs, MethodImplKind); |
| 552 | } |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 553 | |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 554 | llvm::SmallVector<IdentifierInfo *, 12> KeyIdents; |
| 555 | llvm::SmallVector<Action::TypeTy *, 12> KeyTypes; |
| 556 | llvm::SmallVector<IdentifierInfo *, 12> ArgNames; |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 557 | |
| 558 | Action::TypeTy *TypeInfo; |
| 559 | while (1) { |
| 560 | KeyIdents.push_back(SelIdent); |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 561 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 562 | // Each iteration parses a single keyword argument. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 563 | if (Tok.isNot(tok::colon)) { |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 564 | Diag(Tok, diag::err_expected_colon); |
| 565 | break; |
| 566 | } |
| 567 | ConsumeToken(); // Eat the ':'. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 568 | if (Tok.is(tok::l_paren)) // Parse the argument type. |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 569 | TypeInfo = ParseObjCTypeName(); |
| 570 | else |
| 571 | TypeInfo = 0; |
| 572 | KeyTypes.push_back(TypeInfo); |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 573 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 574 | // If attributes exist before the argument name, parse them. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 575 | if (getLang().ObjC2 && Tok.is(tok::kw___attribute)) |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 576 | ParseAttributes(); // FIXME: pass attributes through. |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 577 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 578 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 579 | Diag(Tok, diag::err_expected_ident); // missing argument name. |
| 580 | break; |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 581 | } |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 582 | ArgNames.push_back(Tok.getIdentifierInfo()); |
| 583 | ConsumeToken(); // Eat the identifier. |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 584 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 585 | // Check for another keyword selector. |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 586 | SourceLocation Loc; |
| 587 | SelIdent = ParseObjCSelector(Loc); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 588 | if (!SelIdent && Tok.isNot(tok::colon)) |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 589 | break; |
| 590 | // We have a selector or a colon, continue parsing. |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 591 | } |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 592 | |
| 593 | // Parse the (optional) parameter list. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 594 | while (Tok.is(tok::comma)) { |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 595 | ConsumeToken(); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 596 | if (Tok.is(tok::ellipsis)) { |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 597 | ConsumeToken(); |
| 598 | break; |
| 599 | } |
| 600 | // Parse the c-style argument declaration-specifier. |
| 601 | DeclSpec DS; |
| 602 | ParseDeclarationSpecifiers(DS); |
| 603 | // Parse the declarator. |
| 604 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 605 | ParseDeclarator(ParmDecl); |
| 606 | } |
| 607 | |
| 608 | // FIXME: Add support for optional parmameter list... |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 609 | // If attributes exist after the method, parse them. |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 610 | AttributeList *MethodAttrs = 0; |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 611 | if (getLang().ObjC2 && Tok.is(tok::kw___attribute)) |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 612 | MethodAttrs = ParseAttributes(); |
| 613 | |
| 614 | Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(), |
| 615 | &KeyIdents[0]); |
Steve Naroff | 37e58d1 | 2007-10-02 22:39:18 +0000 | [diff] [blame] | 616 | return Actions.ActOnMethodDeclaration(mLoc, mType, ReturnType, Sel, |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 617 | &KeyTypes[0], &ArgNames[0], |
| 618 | MethodAttrs, MethodImplKind); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 621 | /// CmpProtocolVals - Comparison predicate for sorting protocols. |
| 622 | static bool CmpProtocolVals(const IdentifierInfo* const& lhs, |
| 623 | const IdentifierInfo* const& rhs) { |
| 624 | return strcmp(lhs->getName(), rhs->getName()) < 0; |
| 625 | } |
| 626 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 627 | /// objc-protocol-refs: |
| 628 | /// '<' identifier-list '>' |
| 629 | /// |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 630 | bool Parser::ParseObjCProtocolReferences( |
| 631 | llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs) { |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 632 | assert(Tok.is(tok::less) && "expected <"); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 633 | |
| 634 | ConsumeToken(); // the "<" |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 635 | |
| 636 | while (1) { |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 637 | if (Tok.isNot(tok::identifier)) { |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 638 | Diag(Tok, diag::err_expected_ident); |
| 639 | SkipUntil(tok::greater); |
| 640 | return true; |
| 641 | } |
| 642 | ProtocolRefs.push_back(Tok.getIdentifierInfo()); |
| 643 | ConsumeToken(); |
| 644 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 645 | if (Tok.isNot(tok::comma)) |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 646 | break; |
| 647 | ConsumeToken(); |
| 648 | } |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 649 | |
| 650 | // Sort protocols, keyed by name. |
| 651 | // Later on, we remove duplicates. |
| 652 | std::stable_sort(ProtocolRefs.begin(), ProtocolRefs.end(), CmpProtocolVals); |
| 653 | |
| 654 | // Make protocol names unique. |
| 655 | ProtocolRefs.erase(std::unique(ProtocolRefs.begin(), ProtocolRefs.end()), |
| 656 | ProtocolRefs.end()); |
| 657 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 658 | // Consume the '>'. |
| 659 | return ExpectAndConsume(tok::greater, diag::err_expected_greater); |
| 660 | } |
| 661 | |
| 662 | /// objc-class-instance-variables: |
| 663 | /// '{' objc-instance-variable-decl-list[opt] '}' |
| 664 | /// |
| 665 | /// objc-instance-variable-decl-list: |
| 666 | /// objc-visibility-spec |
| 667 | /// objc-instance-variable-decl ';' |
| 668 | /// ';' |
| 669 | /// objc-instance-variable-decl-list objc-visibility-spec |
| 670 | /// objc-instance-variable-decl-list objc-instance-variable-decl ';' |
| 671 | /// objc-instance-variable-decl-list ';' |
| 672 | /// |
| 673 | /// objc-visibility-spec: |
| 674 | /// @private |
| 675 | /// @protected |
| 676 | /// @public |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 677 | /// @package [OBJC2] |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 678 | /// |
| 679 | /// objc-instance-variable-decl: |
| 680 | /// struct-declaration |
| 681 | /// |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 682 | void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl) { |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 683 | assert(Tok.is(tok::l_brace) && "expected {"); |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 684 | llvm::SmallVector<DeclTy*, 16> IvarDecls; |
Fariborz Jahanian | 7d6402f | 2007-09-13 20:56:13 +0000 | [diff] [blame] | 685 | llvm::SmallVector<DeclTy*, 32> AllIvarDecls; |
| 686 | llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities; |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 687 | |
| 688 | SourceLocation LBraceLoc = ConsumeBrace(); // the "{" |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 689 | |
Fariborz Jahanian | 7d6402f | 2007-09-13 20:56:13 +0000 | [diff] [blame] | 690 | tok::ObjCKeywordKind visibility = tok::objc_private; |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 691 | // While we still have something to read, read the instance variables. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 692 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 693 | // Each iteration of this loop reads one objc-instance-variable-decl. |
| 694 | |
| 695 | // Check for extraneous top-level semicolon. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 696 | if (Tok.is(tok::semi)) { |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 697 | Diag(Tok, diag::ext_extra_struct_semi); |
| 698 | ConsumeToken(); |
| 699 | continue; |
| 700 | } |
| 701 | // Set the default visibility to private. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 702 | if (Tok.is(tok::at)) { // parse objc-visibility-spec |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 703 | ConsumeToken(); // eat the @ sign |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 704 | switch (Tok.getObjCKeywordID()) { |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 705 | case tok::objc_private: |
| 706 | case tok::objc_public: |
| 707 | case tok::objc_protected: |
| 708 | case tok::objc_package: |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 709 | visibility = Tok.getObjCKeywordID(); |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 710 | ConsumeToken(); |
| 711 | continue; |
| 712 | default: |
| 713 | Diag(Tok, diag::err_objc_illegal_visibility_spec); |
| 714 | ConsumeToken(); |
| 715 | continue; |
| 716 | } |
| 717 | } |
| 718 | ParseStructDeclaration(interfaceDecl, IvarDecls); |
Fariborz Jahanian | 7d6402f | 2007-09-13 20:56:13 +0000 | [diff] [blame] | 719 | for (unsigned i = 0; i < IvarDecls.size(); i++) { |
| 720 | AllIvarDecls.push_back(IvarDecls[i]); |
| 721 | AllVisibilities.push_back(visibility); |
| 722 | } |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 723 | IvarDecls.clear(); |
| 724 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 725 | if (Tok.is(tok::semi)) { |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 726 | ConsumeToken(); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 727 | } else if (Tok.is(tok::r_brace)) { |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 728 | Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list); |
| 729 | break; |
| 730 | } else { |
| 731 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 732 | // Skip to end of block or statement |
| 733 | SkipUntil(tok::r_brace, true, true); |
| 734 | } |
| 735 | } |
Fariborz Jahanian | 7d6402f | 2007-09-13 20:56:13 +0000 | [diff] [blame] | 736 | if (AllIvarDecls.size()) { // Check for {} - no ivars in braces |
Fariborz Jahanian | 9d048ff | 2007-09-29 00:54:24 +0000 | [diff] [blame] | 737 | Actions.ActOnFields(CurScope, LBraceLoc, interfaceDecl, |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 738 | &AllIvarDecls[0], AllIvarDecls.size(), |
| 739 | &AllVisibilities[0]); |
Fariborz Jahanian | 7d6402f | 2007-09-13 20:56:13 +0000 | [diff] [blame] | 740 | } |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 741 | MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
| 742 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 743 | } |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 744 | |
| 745 | /// objc-protocol-declaration: |
| 746 | /// objc-protocol-definition |
| 747 | /// objc-protocol-forward-reference |
| 748 | /// |
| 749 | /// objc-protocol-definition: |
| 750 | /// @protocol identifier |
| 751 | /// objc-protocol-refs[opt] |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 752 | /// objc-interface-decl-list |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 753 | /// @end |
| 754 | /// |
| 755 | /// objc-protocol-forward-reference: |
| 756 | /// @protocol identifier-list ';' |
| 757 | /// |
| 758 | /// "@protocol identifier ;" should be resolved as "@protocol |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 759 | /// identifier-list ;": objc-interface-decl-list may not start with a |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 760 | /// semicolon in the first alternative if objc-protocol-refs are omitted. |
| 761 | |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 762 | Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) { |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 763 | assert(Tok.isObjCAtKeyword(tok::objc_protocol) && |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 764 | "ParseObjCAtProtocolDeclaration(): Expected @protocol"); |
| 765 | ConsumeToken(); // the "protocol" identifier |
| 766 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 767 | if (Tok.isNot(tok::identifier)) { |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 768 | Diag(Tok, diag::err_expected_ident); // missing protocol name. |
| 769 | return 0; |
| 770 | } |
| 771 | // Save the protocol name, then consume it. |
| 772 | IdentifierInfo *protocolName = Tok.getIdentifierInfo(); |
| 773 | SourceLocation nameLoc = ConsumeToken(); |
| 774 | |
Fariborz Jahanian | 894c57f | 2007-09-21 15:40:54 +0000 | [diff] [blame] | 775 | llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs; |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 776 | if (Tok.is(tok::semi)) { // forward declaration of one protocol. |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 777 | ConsumeToken(); |
Fariborz Jahanian | 894c57f | 2007-09-21 15:40:54 +0000 | [diff] [blame] | 778 | ProtocolRefs.push_back(protocolName); |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 779 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 780 | if (Tok.is(tok::comma)) { // list of forward declarations. |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 781 | // Parse the list of forward declarations. |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 782 | ProtocolRefs.push_back(protocolName); |
| 783 | |
| 784 | while (1) { |
| 785 | ConsumeToken(); // the ',' |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 786 | if (Tok.isNot(tok::identifier)) { |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 787 | Diag(Tok, diag::err_expected_ident); |
| 788 | SkipUntil(tok::semi); |
| 789 | return 0; |
| 790 | } |
| 791 | ProtocolRefs.push_back(Tok.getIdentifierInfo()); |
| 792 | ConsumeToken(); // the identifier |
| 793 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 794 | if (Tok.isNot(tok::comma)) |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 795 | break; |
| 796 | } |
| 797 | // Consume the ';'. |
| 798 | if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol")) |
| 799 | return 0; |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 800 | } |
Fariborz Jahanian | 894c57f | 2007-09-21 15:40:54 +0000 | [diff] [blame] | 801 | if (ProtocolRefs.size() > 0) |
Steve Naroff | e440eb8 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 802 | return Actions.ActOnForwardProtocolDeclaration(AtLoc, |
Steve Naroff | 37e58d1 | 2007-10-02 22:39:18 +0000 | [diff] [blame] | 803 | &ProtocolRefs[0], |
| 804 | ProtocolRefs.size()); |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 805 | // Last, and definitely not least, parse a protocol declaration. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 806 | if (Tok.is(tok::less)) { |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 807 | if (ParseObjCProtocolReferences(ProtocolRefs)) |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 808 | return 0; |
| 809 | } |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 810 | |
Steve Naroff | e440eb8 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 811 | DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc, |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 812 | protocolName, nameLoc, |
| 813 | &ProtocolRefs[0], |
| 814 | ProtocolRefs.size()); |
| 815 | ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol); |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 816 | |
| 817 | // The @ sign was already consumed by ParseObjCInterfaceDeclList(). |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 818 | if (Tok.isObjCAtKeyword(tok::objc_end)) { |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 819 | ConsumeToken(); // the "end" identifier |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 820 | return ProtoType; |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 821 | } |
| 822 | Diag(Tok, diag::err_objc_missing_end); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 823 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 824 | } |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 825 | |
| 826 | /// objc-implementation: |
| 827 | /// objc-class-implementation-prologue |
| 828 | /// objc-category-implementation-prologue |
| 829 | /// |
| 830 | /// objc-class-implementation-prologue: |
| 831 | /// @implementation identifier objc-superclass[opt] |
| 832 | /// objc-class-instance-variables[opt] |
| 833 | /// |
| 834 | /// objc-category-implementation-prologue: |
| 835 | /// @implementation identifier ( identifier ) |
| 836 | |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 837 | Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration( |
| 838 | SourceLocation atLoc) { |
| 839 | assert(Tok.isObjCAtKeyword(tok::objc_implementation) && |
| 840 | "ParseObjCAtImplementationDeclaration(): Expected @implementation"); |
| 841 | ConsumeToken(); // the "implementation" identifier |
| 842 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 843 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 844 | Diag(Tok, diag::err_expected_ident); // missing class or category name. |
| 845 | return 0; |
| 846 | } |
| 847 | // We have a class or category name - consume it. |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 848 | IdentifierInfo *nameId = Tok.getIdentifierInfo(); |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 849 | SourceLocation nameLoc = ConsumeToken(); // consume class or category name |
| 850 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 851 | if (Tok.is(tok::l_paren)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 852 | // we have a category implementation. |
| 853 | SourceLocation lparenLoc = ConsumeParen(); |
| 854 | SourceLocation categoryLoc, rparenLoc; |
| 855 | IdentifierInfo *categoryId = 0; |
| 856 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 857 | if (Tok.is(tok::identifier)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 858 | categoryId = Tok.getIdentifierInfo(); |
| 859 | categoryLoc = ConsumeToken(); |
| 860 | } else { |
| 861 | Diag(Tok, diag::err_expected_ident); // missing category name. |
| 862 | return 0; |
| 863 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 864 | if (Tok.isNot(tok::r_paren)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 865 | Diag(Tok, diag::err_expected_rparen); |
| 866 | SkipUntil(tok::r_paren, false); // don't stop at ';' |
| 867 | return 0; |
| 868 | } |
| 869 | rparenLoc = ConsumeParen(); |
Steve Naroff | e440eb8 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 870 | DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation( |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 871 | atLoc, nameId, nameLoc, categoryId, |
| 872 | categoryLoc); |
| 873 | return ImplCatType; |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 874 | } |
| 875 | // We have a class implementation |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 876 | SourceLocation superClassLoc; |
| 877 | IdentifierInfo *superClassId = 0; |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 878 | if (Tok.is(tok::colon)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 879 | // We have a super class |
| 880 | ConsumeToken(); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 881 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 882 | Diag(Tok, diag::err_expected_ident); // missing super class name. |
| 883 | return 0; |
| 884 | } |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 885 | superClassId = Tok.getIdentifierInfo(); |
| 886 | superClassLoc = ConsumeToken(); // Consume super class name |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 887 | } |
Steve Naroff | e440eb8 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 888 | DeclTy *ImplClsType = Actions.ActOnStartClassImplementation( |
Steve Naroff | 3a165b0 | 2007-10-03 21:00:46 +0000 | [diff] [blame] | 889 | atLoc, nameId, nameLoc, |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 890 | superClassId, superClassLoc); |
| 891 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 892 | if (Tok.is(tok::l_brace)) |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 893 | ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/); // we have ivars |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 894 | |
Fariborz Jahanian | d0b0154 | 2007-09-27 18:57:03 +0000 | [diff] [blame] | 895 | return ImplClsType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 896 | } |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 897 | Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) { |
| 898 | assert(Tok.isObjCAtKeyword(tok::objc_end) && |
| 899 | "ParseObjCAtEndDeclaration(): Expected @end"); |
| 900 | ConsumeToken(); // the "end" identifier |
Fariborz Jahanian | d0b0154 | 2007-09-27 18:57:03 +0000 | [diff] [blame] | 901 | if (ObjcImpDecl) { |
| 902 | // Checking is not necessary except that a parse error might have caused |
| 903 | // @implementation not to have been parsed to completion and ObjcImpDecl |
| 904 | // could be 0. |
| 905 | /// Insert collected methods declarations into the @interface object. |
Steve Naroff | 3a165b0 | 2007-10-03 21:00:46 +0000 | [diff] [blame] | 906 | Actions.ActOnAddMethodsToObjcDecl(CurScope, ObjcImpDecl, |
| 907 | &AllImplMethods[0],AllImplMethods.size()); |
Fariborz Jahanian | d0b0154 | 2007-09-27 18:57:03 +0000 | [diff] [blame] | 908 | ObjcImpDecl = 0; |
| 909 | AllImplMethods.clear(); |
| 910 | } |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 911 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 912 | return 0; |
| 913 | } |
Fariborz Jahanian | e992af0 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 914 | |
| 915 | /// compatibility-alias-decl: |
| 916 | /// @compatibility_alias alias-name class-name ';' |
| 917 | /// |
| 918 | Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) { |
| 919 | assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) && |
| 920 | "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias"); |
| 921 | ConsumeToken(); // consume compatibility_alias |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 922 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | e992af0 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 923 | Diag(Tok, diag::err_expected_ident); |
| 924 | return 0; |
| 925 | } |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 926 | IdentifierInfo *aliasId = Tok.getIdentifierInfo(); |
| 927 | SourceLocation aliasLoc = ConsumeToken(); // consume alias-name |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 928 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | e992af0 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 929 | Diag(Tok, diag::err_expected_ident); |
| 930 | return 0; |
| 931 | } |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 932 | IdentifierInfo *classId = Tok.getIdentifierInfo(); |
| 933 | SourceLocation classLoc = ConsumeToken(); // consume class-name; |
| 934 | if (Tok.isNot(tok::semi)) { |
Fariborz Jahanian | 8cd8c66 | 2007-09-04 21:42:12 +0000 | [diff] [blame] | 935 | Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias"); |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 936 | return 0; |
| 937 | } |
| 938 | DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc, |
| 939 | aliasId, aliasLoc, |
| 940 | classId, classLoc); |
| 941 | return ClsType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 944 | /// property-synthesis: |
| 945 | /// @synthesize property-ivar-list ';' |
| 946 | /// |
| 947 | /// property-ivar-list: |
| 948 | /// property-ivar |
| 949 | /// property-ivar-list ',' property-ivar |
| 950 | /// |
| 951 | /// property-ivar: |
| 952 | /// identifier |
| 953 | /// identifier '=' identifier |
| 954 | /// |
| 955 | Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) { |
| 956 | assert(Tok.isObjCAtKeyword(tok::objc_synthesize) && |
| 957 | "ParseObjCPropertyDynamic(): Expected '@synthesize'"); |
| 958 | SourceLocation loc = ConsumeToken(); // consume dynamic |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 959 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 960 | Diag(Tok, diag::err_expected_ident); |
| 961 | return 0; |
| 962 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 963 | while (Tok.is(tok::identifier)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 964 | ConsumeToken(); // consume property name |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 965 | if (Tok.is(tok::equal)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 966 | // property '=' ivar-name |
| 967 | ConsumeToken(); // consume '=' |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 968 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 969 | Diag(Tok, diag::err_expected_ident); |
| 970 | break; |
| 971 | } |
| 972 | ConsumeToken(); // consume ivar-name |
| 973 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 974 | if (Tok.isNot(tok::comma)) |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 975 | break; |
| 976 | ConsumeToken(); // consume ',' |
| 977 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 978 | if (Tok.isNot(tok::semi)) |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 979 | Diag(Tok, diag::err_expected_semi_after, "@synthesize"); |
| 980 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 983 | /// property-dynamic: |
| 984 | /// @dynamic property-list |
| 985 | /// |
| 986 | /// property-list: |
| 987 | /// identifier |
| 988 | /// property-list ',' identifier |
| 989 | /// |
| 990 | Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) { |
| 991 | assert(Tok.isObjCAtKeyword(tok::objc_dynamic) && |
| 992 | "ParseObjCPropertyDynamic(): Expected '@dynamic'"); |
| 993 | SourceLocation loc = ConsumeToken(); // consume dynamic |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 994 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 995 | Diag(Tok, diag::err_expected_ident); |
| 996 | return 0; |
| 997 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 998 | while (Tok.is(tok::identifier)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 999 | ConsumeToken(); // consume property name |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1000 | if (Tok.isNot(tok::comma)) |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1001 | break; |
| 1002 | ConsumeToken(); // consume ',' |
| 1003 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1004 | if (Tok.isNot(tok::semi)) |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1005 | Diag(Tok, diag::err_expected_semi_after, "@dynamic"); |
| 1006 | return 0; |
| 1007 | } |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1008 | |
| 1009 | /// objc-throw-statement: |
| 1010 | /// throw expression[opt]; |
| 1011 | /// |
Fariborz Jahanian | b384d32 | 2007-10-04 20:19:06 +0000 | [diff] [blame] | 1012 | Parser::DeclTy *Parser::ParseObjCThrowStmt(SourceLocation atLoc) { |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1013 | ConsumeToken(); // consume throw |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1014 | if (Tok.isNot(tok::semi)) { |
Fariborz Jahanian | b384d32 | 2007-10-04 20:19:06 +0000 | [diff] [blame] | 1015 | ExprResult Res = ParseExpression(); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1016 | if (Res.isInvalid) { |
| 1017 | SkipUntil(tok::semi); |
| 1018 | return 0; |
| 1019 | } |
| 1020 | } |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | /// objc-try-catch-statement: |
| 1025 | /// @try compound-statement objc-catch-list[opt] |
| 1026 | /// @try compound-statement objc-catch-list[opt] @finally compound-statement |
| 1027 | /// |
| 1028 | /// objc-catch-list: |
| 1029 | /// @catch ( parameter-declaration ) compound-statement |
| 1030 | /// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement |
| 1031 | /// catch-parameter-declaration: |
| 1032 | /// parameter-declaration |
| 1033 | /// '...' [OBJC2] |
| 1034 | /// |
Fariborz Jahanian | b384d32 | 2007-10-04 20:19:06 +0000 | [diff] [blame] | 1035 | Parser::DeclTy *Parser::ParseObjCTryStmt(SourceLocation atLoc) { |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1036 | bool catch_or_finally_seen = false; |
| 1037 | ConsumeToken(); // consume try |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1038 | if (Tok.isNot(tok::l_brace)) { |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1039 | Diag (Tok, diag::err_expected_lbrace); |
| 1040 | return 0; |
| 1041 | } |
| 1042 | StmtResult TryBody = ParseCompoundStatementBody(); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1043 | while (Tok.is(tok::at)) { |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1044 | ConsumeToken(); |
| 1045 | if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_catch) { |
| 1046 | SourceLocation catchLoc = ConsumeToken(); // consume catch |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1047 | if (Tok.is(tok::l_paren)) { |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1048 | ConsumeParen(); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1049 | if (Tok.isNot(tok::ellipsis)) { |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1050 | DeclSpec DS; |
| 1051 | ParseDeclarationSpecifiers(DS); |
| 1052 | // Parse the parameter-declaration. |
| 1053 | // FIXME: BlockContext may not be the right context! |
| 1054 | Declarator ParmDecl(DS, Declarator::BlockContext); |
| 1055 | ParseDeclarator(ParmDecl); |
| 1056 | } |
| 1057 | else |
| 1058 | ConsumeToken(); // consume '...' |
| 1059 | ConsumeParen(); |
| 1060 | StmtResult CatchMody = ParseCompoundStatementBody(); |
| 1061 | } |
| 1062 | else { |
| 1063 | Diag(catchLoc, diag::err_expected_lparen_after, "@catch clause"); |
| 1064 | return 0; |
| 1065 | } |
| 1066 | catch_or_finally_seen = true; |
| 1067 | } |
| 1068 | else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_finally) { |
| 1069 | ConsumeToken(); // consume finally |
| 1070 | StmtResult FinallyBody = ParseCompoundStatementBody(); |
| 1071 | catch_or_finally_seen = true; |
| 1072 | break; |
| 1073 | } |
| 1074 | } |
| 1075 | if (!catch_or_finally_seen) |
| 1076 | Diag(atLoc, diag::err_missing_catch_finally); |
| 1077 | return 0; |
| 1078 | } |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1079 | |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 1080 | /// objc-method-def: objc-method-proto ';'[opt] '{' body '}' |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1081 | /// |
| 1082 | void Parser::ParseObjCInstanceMethodDefinition() { |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1083 | assert(Tok.is(tok::minus) && "Method definitions should start with '-'"); |
| 1084 | |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 1085 | // FIXME: @optional/@protocol?? |
Fariborz Jahanian | d0b0154 | 2007-09-27 18:57:03 +0000 | [diff] [blame] | 1086 | AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl)); |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1087 | // parse optional ';' |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1088 | if (Tok.is(tok::semi)) |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1089 | ConsumeToken(); |
| 1090 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1091 | if (Tok.isNot(tok::l_brace)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1092 | Diag (Tok, diag::err_expected_lbrace); |
| 1093 | return; |
| 1094 | } |
| 1095 | |
| 1096 | StmtResult FnBody = ParseCompoundStatementBody(); |
| 1097 | } |
| 1098 | |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 1099 | /// objc-method-def: objc-method-proto ';'[opt] '{' body '}' |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1100 | /// |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1101 | void Parser::ParseObjCClassMethodDefinition() { |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1102 | assert(Tok.is(tok::plus) && "Class method definitions should start with '+'"); |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 1103 | // FIXME: @optional/@protocol?? |
Fariborz Jahanian | d0b0154 | 2007-09-27 18:57:03 +0000 | [diff] [blame] | 1104 | AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl)); |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1105 | // parse optional ';' |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1106 | if (Tok.is(tok::semi)) |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1107 | ConsumeToken(); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1108 | if (Tok.isNot(tok::l_brace)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1109 | Diag (Tok, diag::err_expected_lbrace); |
| 1110 | return; |
| 1111 | } |
| 1112 | |
| 1113 | StmtResult FnBody = ParseCompoundStatementBody(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1114 | } |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1115 | |
Fariborz Jahanian | b384d32 | 2007-10-04 20:19:06 +0000 | [diff] [blame] | 1116 | Parser::ExprResult Parser::ParseObjCExpression(SourceLocation AtLoc) { |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1117 | |
| 1118 | switch (Tok.getKind()) { |
| 1119 | case tok::string_literal: // primary-expression: string-literal |
| 1120 | case tok::wide_string_literal: |
Fariborz Jahanian | b384d32 | 2007-10-04 20:19:06 +0000 | [diff] [blame] | 1121 | return ParsePostfixExpressionSuffix(ParseObjCStringLiteral()); |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1122 | default: |
| 1123 | break; |
| 1124 | } |
| 1125 | |
| 1126 | switch (Tok.getIdentifierInfo()->getObjCKeywordID()) { |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1127 | case tok::objc_encode: |
Fariborz Jahanian | b384d32 | 2007-10-04 20:19:06 +0000 | [diff] [blame] | 1128 | return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression()); |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1129 | case tok::objc_protocol: |
Fariborz Jahanian | b384d32 | 2007-10-04 20:19:06 +0000 | [diff] [blame] | 1130 | return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression()); |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1131 | default: |
| 1132 | Diag(AtLoc, diag::err_unexpected_at); |
| 1133 | SkipUntil(tok::semi); |
| 1134 | break; |
| 1135 | } |
| 1136 | |
| 1137 | return 0; |
| 1138 | } |
| 1139 | |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 1140 | /// objc-message-expr: |
| 1141 | /// '[' objc-receiver objc-message-args ']' |
| 1142 | /// |
| 1143 | /// objc-receiver: |
| 1144 | /// expression |
| 1145 | /// class-name |
| 1146 | /// type-name |
| 1147 | /// |
| 1148 | /// objc-message-args: |
| 1149 | /// objc-selector |
| 1150 | /// objc-keywordarg-list |
| 1151 | /// |
| 1152 | /// objc-keywordarg-list: |
| 1153 | /// objc-keywordarg |
| 1154 | /// objc-keywordarg-list objc-keywordarg |
| 1155 | /// |
| 1156 | /// objc-keywordarg: |
| 1157 | /// selector-name[opt] ':' objc-keywordexpr |
| 1158 | /// |
| 1159 | /// objc-keywordexpr: |
| 1160 | /// nonempty-expr-list |
| 1161 | /// |
| 1162 | /// nonempty-expr-list: |
| 1163 | /// assignment-expression |
| 1164 | /// nonempty-expr-list , assignment-expression |
| 1165 | /// |
| 1166 | Parser::ExprResult Parser::ParseObjCMessageExpression() { |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1167 | assert(Tok.is(tok::l_square) && "'[' expected"); |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1168 | SourceLocation LBracloc = ConsumeBracket(); // consume '[' |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1169 | IdentifierInfo *ReceiverName = 0; |
| 1170 | ExprTy *ReceiverExpr = 0; |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1171 | // Parse receiver |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1172 | if (Tok.is(tok::identifier) && |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1173 | Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) { |
| 1174 | ReceiverName = Tok.getIdentifierInfo(); |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 1175 | ConsumeToken(); |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1176 | } else { |
| 1177 | ExprResult Res = ParseAssignmentExpression(); |
| 1178 | if (Res.isInvalid) { |
| 1179 | SkipUntil(tok::identifier); |
| 1180 | return Res; |
| 1181 | } |
| 1182 | ReceiverExpr = Res.Val; |
| 1183 | } |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1184 | // Parse objc-selector |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1185 | SourceLocation Loc; |
| 1186 | IdentifierInfo *selIdent = ParseObjCSelector(Loc); |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1187 | |
| 1188 | llvm::SmallVector<IdentifierInfo *, 12> KeyIdents; |
| 1189 | llvm::SmallVector<Action::ExprTy *, 12> KeyExprs; |
| 1190 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1191 | if (Tok.is(tok::colon)) { |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1192 | while (1) { |
| 1193 | // Each iteration parses a single keyword argument. |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1194 | KeyIdents.push_back(selIdent); |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1195 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1196 | if (Tok.isNot(tok::colon)) { |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1197 | Diag(Tok, diag::err_expected_colon); |
| 1198 | SkipUntil(tok::semi); |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1199 | return true; |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1200 | } |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1201 | ConsumeToken(); // Eat the ':'. |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1202 | /// Parse the expression after ':' |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1203 | ExprResult Res = ParseAssignmentExpression(); |
| 1204 | if (Res.isInvalid) { |
| 1205 | SkipUntil(tok::identifier); |
| 1206 | return Res; |
| 1207 | } |
| 1208 | // We have a valid expression. |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1209 | KeyExprs.push_back(Res.Val); |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1210 | |
| 1211 | // Check for another keyword selector. |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1212 | selIdent = ParseObjCSelector(Loc); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1213 | if (!selIdent && Tok.isNot(tok::colon)) |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1214 | break; |
| 1215 | // We have a selector or a colon, continue parsing. |
| 1216 | } |
| 1217 | // Parse the, optional, argument list, comma separated. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1218 | while (Tok.is(tok::comma)) { |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1219 | ConsumeToken(); |
| 1220 | /// Parse the expression after ',' |
| 1221 | ParseAssignmentExpression(); |
| 1222 | } |
| 1223 | } else if (!selIdent) { |
| 1224 | Diag(Tok, diag::err_expected_ident); // missing selector name. |
| 1225 | SkipUntil(tok::semi); |
| 1226 | return 0; |
| 1227 | } |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1228 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1229 | if (Tok.isNot(tok::r_square)) { |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1230 | Diag(Tok, diag::err_expected_rsquare); |
| 1231 | SkipUntil(tok::semi); |
| 1232 | return 0; |
| 1233 | } |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1234 | SourceLocation RBracloc = ConsumeBracket(); // consume ']' |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1235 | |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 1236 | unsigned nKeys = KeyIdents.size(); |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1237 | if (nKeys == 0) |
| 1238 | KeyIdents.push_back(selIdent); |
| 1239 | Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]); |
| 1240 | |
| 1241 | // We've just parsed a keyword message. |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1242 | if (ReceiverName) |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1243 | return Actions.ActOnClassMessage(ReceiverName, Sel, LBracloc, RBracloc, |
| 1244 | &KeyExprs[0]); |
| 1245 | return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracloc, RBracloc, |
| 1246 | &KeyExprs[0]); |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1249 | Parser::ExprResult Parser::ParseObjCStringLiteral() { |
| 1250 | ExprResult Res = ParseStringLiteralExpression(); |
| 1251 | |
| 1252 | if (Res.isInvalid) return Res; |
| 1253 | |
| 1254 | return Actions.ParseObjCStringLiteral(Res.Val); |
| 1255 | } |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1256 | |
| 1257 | /// objc-encode-expression: |
| 1258 | /// @encode ( type-name ) |
| 1259 | Parser::ExprResult Parser::ParseObjCEncodeExpression() { |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 1260 | assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!"); |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1261 | |
| 1262 | SourceLocation EncLoc = ConsumeToken(); |
| 1263 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1264 | if (Tok.isNot(tok::l_paren)) { |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1265 | Diag(Tok, diag::err_expected_lparen_after, "@encode"); |
| 1266 | return true; |
| 1267 | } |
| 1268 | |
| 1269 | SourceLocation LParenLoc = ConsumeParen(); |
| 1270 | |
| 1271 | TypeTy *Ty = ParseTypeName(); |
| 1272 | |
Anders Carlsson | 4988ae3 | 2007-08-23 15:31:37 +0000 | [diff] [blame] | 1273 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1274 | |
| 1275 | return Actions.ParseObjCEncodeExpression(EncLoc, LParenLoc, Ty, |
Anders Carlsson | 4988ae3 | 2007-08-23 15:31:37 +0000 | [diff] [blame] | 1276 | RParenLoc); |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1277 | } |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1278 | |
| 1279 | /// objc-protocol-expression |
| 1280 | /// @protocol ( protocol-name ) |
| 1281 | |
| 1282 | Parser::ExprResult Parser::ParseObjCProtocolExpression() |
| 1283 | { |
| 1284 | SourceLocation ProtoLoc = ConsumeToken(); |
| 1285 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1286 | if (Tok.isNot(tok::l_paren)) { |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1287 | Diag(Tok, diag::err_expected_lparen_after, "@protocol"); |
| 1288 | return true; |
| 1289 | } |
| 1290 | |
| 1291 | SourceLocation LParenLoc = ConsumeParen(); |
| 1292 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1293 | if (Tok.isNot(tok::identifier)) { |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1294 | Diag(Tok, diag::err_expected_ident); |
| 1295 | return true; |
| 1296 | } |
| 1297 | |
| 1298 | // FIXME: Do something with the protocol name |
| 1299 | ConsumeToken(); |
| 1300 | |
Anders Carlsson | 4988ae3 | 2007-08-23 15:31:37 +0000 | [diff] [blame] | 1301 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1302 | |
| 1303 | // FIXME |
| 1304 | return 0; |
| 1305 | } |