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