Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1 | //===--- ParseObjC.cpp - Objective C Parsing ------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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()) { |
Chris Lattner | 5ffb14b | 2008-08-23 02:02:23 +0000 | [diff] [blame] | 34 | case tok::objc_class: |
| 35 | return ParseObjCAtClassDeclaration(AtLoc); |
| 36 | case tok::objc_interface: |
| 37 | return ParseObjCAtInterfaceDeclaration(AtLoc); |
| 38 | case tok::objc_protocol: |
| 39 | return ParseObjCAtProtocolDeclaration(AtLoc); |
| 40 | case tok::objc_implementation: |
| 41 | return ParseObjCAtImplementationDeclaration(AtLoc); |
| 42 | case tok::objc_end: |
| 43 | return ParseObjCAtEndDeclaration(AtLoc); |
| 44 | case tok::objc_compatibility_alias: |
| 45 | return ParseObjCAtAliasDeclaration(AtLoc); |
| 46 | case tok::objc_synthesize: |
| 47 | return ParseObjCPropertySynthesize(AtLoc); |
| 48 | case tok::objc_dynamic: |
| 49 | return ParseObjCPropertyDynamic(AtLoc); |
| 50 | default: |
| 51 | Diag(AtLoc, diag::err_unexpected_at); |
| 52 | SkipUntil(tok::semi); |
| 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; |
| 134 | |
Steve Naroff | 527fe23 | 2007-08-23 19:56:30 +0000 | [diff] [blame] | 135 | // For ObjC2, the category name is optional (not an error). |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 136 | if (Tok.is(tok::identifier)) { |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 137 | categoryId = Tok.getIdentifierInfo(); |
| 138 | categoryLoc = ConsumeToken(); |
Steve Naroff | 527fe23 | 2007-08-23 19:56:30 +0000 | [diff] [blame] | 139 | } else if (!getLang().ObjC2) { |
| 140 | Diag(Tok, diag::err_expected_ident); // missing category name. |
| 141 | return 0; |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 142 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 143 | if (Tok.isNot(tok::r_paren)) { |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 144 | Diag(Tok, diag::err_expected_rparen); |
| 145 | SkipUntil(tok::r_paren, false); // don't stop at ';' |
| 146 | return 0; |
| 147 | } |
| 148 | rparenLoc = ConsumeParen(); |
Chris Lattner | 6bd6d0b | 2008-07-26 04:07:02 +0000 | [diff] [blame] | 149 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 150 | // Next, we need to check for any protocol references. |
Chris Lattner | 6bd6d0b | 2008-07-26 04:07:02 +0000 | [diff] [blame] | 151 | SourceLocation EndProtoLoc; |
| 152 | llvm::SmallVector<DeclTy *, 8> ProtocolRefs; |
| 153 | if (Tok.is(tok::less) && |
| 154 | ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc)) |
| 155 | return 0; |
| 156 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 157 | if (attrList) // categories don't support attributes. |
| 158 | Diag(Tok, diag::err_objc_no_attributes_on_category); |
| 159 | |
Steve Naroff | e440eb8 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 160 | DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc, |
Steve Naroff | 3a165b0 | 2007-10-03 21:00:46 +0000 | [diff] [blame] | 161 | nameId, nameLoc, categoryId, categoryLoc, |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 162 | &ProtocolRefs[0], ProtocolRefs.size(), |
Chris Lattner | 6bd6d0b | 2008-07-26 04:07:02 +0000 | [diff] [blame] | 163 | EndProtoLoc); |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 164 | |
| 165 | ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 166 | |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 167 | // The @ sign was already consumed by ParseObjCInterfaceDeclList(). |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 168 | if (Tok.isObjCAtKeyword(tok::objc_end)) { |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 169 | ConsumeToken(); // the "end" identifier |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 170 | return CategoryType; |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 171 | } |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 172 | Diag(Tok, diag::err_objc_missing_end); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 173 | return 0; |
| 174 | } |
| 175 | // Parse a class interface. |
| 176 | IdentifierInfo *superClassId = 0; |
| 177 | SourceLocation superClassLoc; |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 178 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 179 | if (Tok.is(tok::colon)) { // a super class is specified. |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 180 | ConsumeToken(); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 181 | if (Tok.isNot(tok::identifier)) { |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 182 | Diag(Tok, diag::err_expected_ident); // missing super class name. |
| 183 | return 0; |
| 184 | } |
| 185 | superClassId = Tok.getIdentifierInfo(); |
| 186 | superClassLoc = ConsumeToken(); |
| 187 | } |
| 188 | // Next, we need to check for any protocol references. |
Chris Lattner | 06036d3 | 2008-07-26 04:13:19 +0000 | [diff] [blame] | 189 | llvm::SmallVector<Action::DeclTy*, 8> ProtocolRefs; |
| 190 | SourceLocation EndProtoLoc; |
| 191 | if (Tok.is(tok::less) && |
| 192 | ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc)) |
| 193 | return 0; |
| 194 | |
| 195 | DeclTy *ClsType = |
| 196 | Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc, |
| 197 | superClassId, superClassLoc, |
| 198 | &ProtocolRefs[0], ProtocolRefs.size(), |
| 199 | EndProtoLoc, attrList); |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 200 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 201 | if (Tok.is(tok::l_brace)) |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 202 | ParseObjCClassInstanceVariables(ClsType, atLoc); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 203 | |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 204 | ParseObjCInterfaceDeclList(ClsType, tok::objc_interface); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 205 | |
| 206 | // The @ sign was already consumed by ParseObjCInterfaceDeclList(). |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 207 | if (Tok.isObjCAtKeyword(tok::objc_end)) { |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 208 | ConsumeToken(); // the "end" identifier |
Steve Naroff | 2bd42fa | 2007-09-10 20:51:04 +0000 | [diff] [blame] | 209 | return ClsType; |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 210 | } |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 211 | Diag(Tok, diag::err_objc_missing_end); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 212 | return 0; |
| 213 | } |
| 214 | |
Daniel Dunbar | 4d7da2f | 2008-08-26 02:32:45 +0000 | [diff] [blame] | 215 | /// constructSetterName - Return the setter name for the given |
| 216 | /// identifier, i.e. "set" + Name where the initial character of Name |
| 217 | /// has been capitalized. |
| 218 | static IdentifierInfo *constructSetterName(IdentifierTable &Idents, |
| 219 | const IdentifierInfo *Name) { |
| 220 | unsigned N = Name->getLength(); |
| 221 | char *SelectorName = new char[3 + N]; |
| 222 | memcpy(SelectorName, "set", 3); |
| 223 | memcpy(&SelectorName[3], Name->getName(), N); |
| 224 | SelectorName[3] = toupper(SelectorName[3]); |
| 225 | |
| 226 | IdentifierInfo *Setter = |
| 227 | &Idents.get(SelectorName, &SelectorName[3 + N]); |
| 228 | delete[] SelectorName; |
| 229 | return Setter; |
| 230 | } |
| 231 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 232 | /// objc-interface-decl-list: |
| 233 | /// empty |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 234 | /// objc-interface-decl-list objc-property-decl [OBJC2] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 235 | /// objc-interface-decl-list objc-method-requirement [OBJC2] |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 236 | /// objc-interface-decl-list objc-method-proto ';' |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 237 | /// objc-interface-decl-list declaration |
| 238 | /// objc-interface-decl-list ';' |
| 239 | /// |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 240 | /// objc-method-requirement: [OBJC2] |
| 241 | /// @required |
| 242 | /// @optional |
| 243 | /// |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 244 | void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl, |
Chris Lattner | cb53b36 | 2007-12-27 19:57:00 +0000 | [diff] [blame] | 245 | tok::ObjCKeywordKind contextKey) { |
Ted Kremenek | 8a77931 | 2008-06-06 16:45:15 +0000 | [diff] [blame] | 246 | llvm::SmallVector<DeclTy*, 32> allMethods; |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 247 | llvm::SmallVector<DeclTy*, 16> allProperties; |
Fariborz Jahanian | 0093359 | 2007-09-18 00:25:23 +0000 | [diff] [blame] | 248 | tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword; |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 249 | SourceLocation AtEndLoc; |
| 250 | |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 251 | while (1) { |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 252 | if (Tok.is(tok::at)) { |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 253 | SourceLocation AtLoc = ConsumeToken(); // the "@" |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 254 | tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID(); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 255 | |
| 256 | if (ocKind == tok::objc_end) { // terminate list |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 257 | AtEndLoc = AtLoc; |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 258 | break; |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 259 | } else if (ocKind == tok::objc_required) { // protocols only |
| 260 | ConsumeToken(); |
Chris Lattner | cb53b36 | 2007-12-27 19:57:00 +0000 | [diff] [blame] | 261 | MethodImplKind = ocKind; |
| 262 | if (contextKey != tok::objc_protocol) |
| 263 | Diag(AtLoc, diag::err_objc_protocol_required); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 264 | } else if (ocKind == tok::objc_optional) { // protocols only |
| 265 | ConsumeToken(); |
Chris Lattner | cb53b36 | 2007-12-27 19:57:00 +0000 | [diff] [blame] | 266 | MethodImplKind = ocKind; |
| 267 | if (contextKey != tok::objc_protocol) |
| 268 | Diag(AtLoc, diag::err_objc_protocol_optional); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 269 | } else if (ocKind == tok::objc_property) { |
Fariborz Jahanian | 1de1e74 | 2008-04-14 23:36:35 +0000 | [diff] [blame] | 270 | ObjCDeclSpec OCDS; |
| 271 | ConsumeToken(); // the "property" identifier |
| 272 | // Parse property attribute list, if any. |
| 273 | if (Tok.is(tok::l_paren)) { |
| 274 | // property has attribute list. |
| 275 | ParseObjCPropertyAttribute(OCDS); |
| 276 | } |
| 277 | // Parse all the comma separated declarators. |
| 278 | DeclSpec DS; |
| 279 | llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators; |
| 280 | ParseStructDeclaration(DS, FieldDeclarators); |
| 281 | |
| 282 | if (Tok.is(tok::semi)) |
| 283 | ConsumeToken(); |
| 284 | else { |
| 285 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 286 | SkipUntil(tok::r_brace, true, true); |
| 287 | } |
| 288 | // Convert them all to property declarations. |
| 289 | for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) { |
| 290 | FieldDeclarator &FD = FieldDeclarators[i]; |
| 291 | // Install the property declarator into interfaceDecl. |
Fariborz Jahanian | 5251e13 | 2008-05-06 18:09:04 +0000 | [diff] [blame] | 292 | Selector GetterSel = |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 293 | PP.getSelectorTable().getNullarySelector(OCDS.getGetterName() |
| 294 | ? OCDS.getGetterName() |
| 295 | : FD.D.getIdentifier()); |
Daniel Dunbar | 4d7da2f | 2008-08-26 02:32:45 +0000 | [diff] [blame] | 296 | IdentifierInfo *SetterName = OCDS.getSetterName(); |
| 297 | if (!SetterName) |
| 298 | SetterName = constructSetterName(PP.getIdentifierTable(), |
| 299 | FD.D.getIdentifier()); |
Fariborz Jahanian | 5251e13 | 2008-05-06 18:09:04 +0000 | [diff] [blame] | 300 | Selector SetterSel = |
Daniel Dunbar | 4d7da2f | 2008-08-26 02:32:45 +0000 | [diff] [blame] | 301 | PP.getSelectorTable().getUnarySelector(SetterName); |
Fariborz Jahanian | 1de1e74 | 2008-04-14 23:36:35 +0000 | [diff] [blame] | 302 | DeclTy *Property = Actions.ActOnProperty(CurScope, |
Steve Naroff | 8442b5c | 2008-05-22 23:24:08 +0000 | [diff] [blame] | 303 | AtLoc, FD, OCDS, |
Fariborz Jahanian | 5251e13 | 2008-05-06 18:09:04 +0000 | [diff] [blame] | 304 | GetterSel, SetterSel, |
Fariborz Jahanian | 46b55e5 | 2008-05-05 18:51:55 +0000 | [diff] [blame] | 305 | MethodImplKind); |
Fariborz Jahanian | 1de1e74 | 2008-04-14 23:36:35 +0000 | [diff] [blame] | 306 | allProperties.push_back(Property); |
| 307 | } |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 308 | continue; |
| 309 | } else { |
| 310 | Diag(Tok, diag::err_objc_illegal_interface_qual); |
| 311 | ConsumeToken(); |
| 312 | } |
| 313 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 314 | if (Tok.is(tok::minus) || Tok.is(tok::plus)) { |
| 315 | DeclTy *methodPrototype = |
| 316 | ParseObjCMethodPrototype(interfaceDecl, MethodImplKind); |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 317 | allMethods.push_back(methodPrototype); |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 318 | // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for |
| 319 | // method definitions. |
Steve Naroff | d16245b | 2007-09-17 15:07:43 +0000 | [diff] [blame] | 320 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto"); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 321 | continue; |
| 322 | } |
Fariborz Jahanian | f366b4c | 2007-12-11 18:34:51 +0000 | [diff] [blame] | 323 | else if (Tok.is(tok::at)) |
| 324 | continue; |
| 325 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 326 | if (Tok.is(tok::semi)) |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 327 | ConsumeToken(); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 328 | else if (Tok.is(tok::eof)) |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 329 | break; |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 330 | else { |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 331 | // FIXME: as the name implies, this rule allows function definitions. |
| 332 | // We could pass a flag or check for functions during semantic analysis. |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 333 | ParseDeclarationOrFunctionDefinition(); |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 334 | } |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 335 | } |
Steve Naroff | 2feac5e | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 336 | /// Insert collected methods declarations into the @interface object. |
Ted Kremenek | 8a77931 | 2008-06-06 16:45:15 +0000 | [diff] [blame] | 337 | Actions.ActOnAtEnd(AtEndLoc, interfaceDecl, |
| 338 | allMethods.empty() ? 0 : &allMethods[0], |
| 339 | allMethods.size(), |
| 340 | allProperties.empty() ? 0 : &allProperties[0], |
| 341 | allProperties.size()); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 344 | /// Parse property attribute declarations. |
| 345 | /// |
| 346 | /// property-attr-decl: '(' property-attrlist ')' |
| 347 | /// property-attrlist: |
| 348 | /// property-attribute |
| 349 | /// property-attrlist ',' property-attribute |
| 350 | /// property-attribute: |
| 351 | /// getter '=' identifier |
| 352 | /// setter '=' identifier ':' |
| 353 | /// readonly |
| 354 | /// readwrite |
| 355 | /// assign |
| 356 | /// retain |
| 357 | /// copy |
| 358 | /// nonatomic |
| 359 | /// |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 360 | void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) { |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 361 | SourceLocation loc = ConsumeParen(); // consume '(' |
| 362 | while (isObjCPropertyAttribute()) { |
| 363 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 364 | // getter/setter require extra treatment. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 365 | if (II == ObjCPropertyAttrs[objc_getter] || |
| 366 | II == ObjCPropertyAttrs[objc_setter]) { |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 367 | // skip getter/setter part. |
| 368 | SourceLocation loc = ConsumeToken(); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 369 | if (Tok.is(tok::equal)) { |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 370 | loc = ConsumeToken(); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 371 | if (Tok.is(tok::identifier)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 372 | if (II == ObjCPropertyAttrs[objc_setter]) { |
| 373 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter); |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 374 | DS.setSetterName(Tok.getIdentifierInfo()); |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 375 | loc = ConsumeToken(); // consume method name |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 376 | if (Tok.isNot(tok::colon)) { |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 377 | Diag(loc, diag::err_expected_colon); |
| 378 | SkipUntil(tok::r_paren,true,true); |
| 379 | break; |
| 380 | } |
| 381 | } |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 382 | else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 383 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter); |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 384 | DS.setGetterName(Tok.getIdentifierInfo()); |
| 385 | } |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 386 | } |
| 387 | else { |
| 388 | Diag(loc, diag::err_expected_ident); |
Chris Lattner | cb53b36 | 2007-12-27 19:57:00 +0000 | [diff] [blame] | 389 | SkipUntil(tok::r_paren,true,true); |
| 390 | break; |
| 391 | } |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 392 | } |
| 393 | else { |
| 394 | Diag(loc, diag::err_objc_expected_equal); |
| 395 | SkipUntil(tok::r_paren,true,true); |
| 396 | break; |
| 397 | } |
| 398 | } |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 399 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 400 | else if (II == ObjCPropertyAttrs[objc_readonly]) |
| 401 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly); |
| 402 | else if (II == ObjCPropertyAttrs[objc_assign]) |
| 403 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign); |
| 404 | else if (II == ObjCPropertyAttrs[objc_readwrite]) |
| 405 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite); |
| 406 | else if (II == ObjCPropertyAttrs[objc_retain]) |
| 407 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain); |
| 408 | else if (II == ObjCPropertyAttrs[objc_copy]) |
| 409 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy); |
| 410 | else if (II == ObjCPropertyAttrs[objc_nonatomic]) |
| 411 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic); |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 412 | |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 413 | ConsumeToken(); // consume last attribute token |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 414 | if (Tok.is(tok::comma)) { |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 415 | loc = ConsumeToken(); |
| 416 | continue; |
| 417 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 418 | if (Tok.is(tok::r_paren)) |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 419 | break; |
| 420 | Diag(loc, diag::err_expected_rparen); |
| 421 | SkipUntil(tok::semi); |
| 422 | return; |
| 423 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 424 | if (Tok.is(tok::r_paren)) |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 425 | ConsumeParen(); |
| 426 | else { |
| 427 | Diag(loc, diag::err_objc_expected_property_attr); |
| 428 | SkipUntil(tok::r_paren); // recover from error inside attribute list |
| 429 | } |
| 430 | } |
| 431 | |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 432 | /// objc-method-proto: |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 433 | /// objc-instance-method objc-method-decl objc-method-attributes[opt] |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 434 | /// objc-class-method objc-method-decl objc-method-attributes[opt] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 435 | /// |
| 436 | /// objc-instance-method: '-' |
| 437 | /// objc-class-method: '+' |
| 438 | /// |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 439 | /// objc-method-attributes: [OBJC2] |
| 440 | /// __attribute__((deprecated)) |
| 441 | /// |
Fariborz Jahanian | 0093359 | 2007-09-18 00:25:23 +0000 | [diff] [blame] | 442 | Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl, |
Chris Lattner | cb53b36 | 2007-12-27 19:57:00 +0000 | [diff] [blame] | 443 | tok::ObjCKeywordKind MethodImplKind) { |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 444 | assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-"); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 445 | |
| 446 | tok::TokenKind methodType = Tok.getKind(); |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 447 | SourceLocation mLoc = ConsumeToken(); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 448 | |
Fariborz Jahanian | 1f7b6f8 | 2007-11-09 19:52:12 +0000 | [diff] [blame] | 449 | DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind); |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 450 | // Since this rule is used for both method declarations and definitions, |
Steve Naroff | 2bd42fa | 2007-09-10 20:51:04 +0000 | [diff] [blame] | 451 | // the caller is (optionally) responsible for consuming the ';'. |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 452 | return MDecl; |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /// objc-selector: |
| 456 | /// identifier |
| 457 | /// one of |
| 458 | /// enum struct union if else while do for switch case default |
| 459 | /// break continue return goto asm sizeof typeof __alignof |
| 460 | /// unsigned long const short volatile signed restrict _Complex |
| 461 | /// in out inout bycopy byref oneway int char float double void _Bool |
| 462 | /// |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 463 | IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) { |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 464 | switch (Tok.getKind()) { |
| 465 | default: |
| 466 | return 0; |
| 467 | case tok::identifier: |
Anders Carlsson | ef048ef | 2008-08-23 21:00:01 +0000 | [diff] [blame] | 468 | case tok::kw_asm: |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 469 | case tok::kw_auto: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 470 | case tok::kw_bool: |
Anders Carlsson | ef048ef | 2008-08-23 21:00:01 +0000 | [diff] [blame] | 471 | case tok::kw_break: |
| 472 | case tok::kw_case: |
| 473 | case tok::kw_catch: |
| 474 | case tok::kw_char: |
| 475 | case tok::kw_class: |
| 476 | case tok::kw_const: |
| 477 | case tok::kw_const_cast: |
| 478 | case tok::kw_continue: |
| 479 | case tok::kw_default: |
| 480 | case tok::kw_delete: |
| 481 | case tok::kw_do: |
| 482 | case tok::kw_double: |
| 483 | case tok::kw_dynamic_cast: |
| 484 | case tok::kw_else: |
| 485 | case tok::kw_enum: |
| 486 | case tok::kw_explicit: |
| 487 | case tok::kw_export: |
| 488 | case tok::kw_extern: |
| 489 | case tok::kw_false: |
| 490 | case tok::kw_float: |
| 491 | case tok::kw_for: |
| 492 | case tok::kw_friend: |
| 493 | case tok::kw_goto: |
| 494 | case tok::kw_if: |
| 495 | case tok::kw_inline: |
| 496 | case tok::kw_int: |
| 497 | case tok::kw_long: |
| 498 | case tok::kw_mutable: |
| 499 | case tok::kw_namespace: |
| 500 | case tok::kw_new: |
| 501 | case tok::kw_operator: |
| 502 | case tok::kw_private: |
| 503 | case tok::kw_protected: |
| 504 | case tok::kw_public: |
| 505 | case tok::kw_register: |
| 506 | case tok::kw_reinterpret_cast: |
| 507 | case tok::kw_restrict: |
| 508 | case tok::kw_return: |
| 509 | case tok::kw_short: |
| 510 | case tok::kw_signed: |
| 511 | case tok::kw_sizeof: |
| 512 | case tok::kw_static: |
| 513 | case tok::kw_static_cast: |
| 514 | case tok::kw_struct: |
| 515 | case tok::kw_switch: |
| 516 | case tok::kw_template: |
| 517 | case tok::kw_this: |
| 518 | case tok::kw_throw: |
| 519 | case tok::kw_true: |
| 520 | case tok::kw_try: |
| 521 | case tok::kw_typedef: |
| 522 | case tok::kw_typeid: |
| 523 | case tok::kw_typename: |
| 524 | case tok::kw_typeof: |
| 525 | case tok::kw_union: |
| 526 | case tok::kw_unsigned: |
| 527 | case tok::kw_using: |
| 528 | case tok::kw_virtual: |
| 529 | case tok::kw_void: |
| 530 | case tok::kw_volatile: |
| 531 | case tok::kw_wchar_t: |
| 532 | case tok::kw_while: |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 533 | case tok::kw__Bool: |
| 534 | case tok::kw__Complex: |
Anders Carlsson | ef048ef | 2008-08-23 21:00:01 +0000 | [diff] [blame] | 535 | case tok::kw___alignof: |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 536 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 537 | SelectorLoc = ConsumeToken(); |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 538 | return II; |
Fariborz Jahanian | d064951 | 2007-09-27 19:52:15 +0000 | [diff] [blame] | 539 | } |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 542 | /// property-attrlist: one of |
| 543 | /// readonly getter setter assign retain copy nonatomic |
| 544 | /// |
| 545 | bool Parser::isObjCPropertyAttribute() { |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 546 | if (Tok.is(tok::identifier)) { |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 547 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 548 | for (unsigned i = 0; i < objc_NumAttrs; ++i) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 549 | if (II == ObjCPropertyAttrs[i]) return true; |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 550 | } |
| 551 | return false; |
| 552 | } |
| 553 | |
Fariborz Jahanian | 0196cab | 2008-01-02 22:54:34 +0000 | [diff] [blame] | 554 | /// objc-for-collection-in: 'in' |
| 555 | /// |
Fariborz Jahanian | 335a2d4 | 2008-01-04 23:04:08 +0000 | [diff] [blame] | 556 | bool Parser::isTokIdentifier_in() const { |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 557 | // FIXME: May have to do additional look-ahead to only allow for |
| 558 | // valid tokens following an 'in'; such as an identifier, unary operators, |
| 559 | // '[' etc. |
Fariborz Jahanian | 335a2d4 | 2008-01-04 23:04:08 +0000 | [diff] [blame] | 560 | return (getLang().ObjC2 && Tok.is(tok::identifier) && |
Chris Lattner | 5ffb14b | 2008-08-23 02:02:23 +0000 | [diff] [blame] | 561 | Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]); |
Fariborz Jahanian | 0196cab | 2008-01-02 22:54:34 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 564 | /// ParseObjCTypeQualifierList - This routine parses the objective-c's type |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 565 | /// qualifier list and builds their bitmask representation in the input |
| 566 | /// argument. |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 567 | /// |
| 568 | /// objc-type-qualifiers: |
| 569 | /// objc-type-qualifier |
| 570 | /// objc-type-qualifiers objc-type-qualifier |
| 571 | /// |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 572 | void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) { |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 573 | while (1) { |
Chris Lattner | cb53b36 | 2007-12-27 19:57:00 +0000 | [diff] [blame] | 574 | if (Tok.isNot(tok::identifier)) |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 575 | return; |
| 576 | |
| 577 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 578 | for (unsigned i = 0; i != objc_NumQuals; ++i) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 579 | if (II != ObjCTypeQuals[i]) |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 580 | continue; |
| 581 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 582 | ObjCDeclSpec::ObjCDeclQualifier Qual; |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 583 | switch (i) { |
| 584 | default: assert(0 && "Unknown decl qualifier"); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 585 | case objc_in: Qual = ObjCDeclSpec::DQ_In; break; |
| 586 | case objc_out: Qual = ObjCDeclSpec::DQ_Out; break; |
| 587 | case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break; |
| 588 | case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break; |
| 589 | case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break; |
| 590 | case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break; |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 591 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 592 | DS.setObjCDeclQualifier(Qual); |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 593 | ConsumeToken(); |
| 594 | II = 0; |
| 595 | break; |
| 596 | } |
| 597 | |
| 598 | // If this wasn't a recognized qualifier, bail out. |
| 599 | if (II) return; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | /// objc-type-name: |
| 604 | /// '(' objc-type-qualifiers[opt] type-name ')' |
| 605 | /// '(' objc-type-qualifiers[opt] ')' |
| 606 | /// |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 607 | Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) { |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 608 | assert(Tok.is(tok::l_paren) && "expected ("); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 609 | |
| 610 | SourceLocation LParenLoc = ConsumeParen(), RParenLoc; |
Chris Lattner | e8904e9 | 2008-08-23 01:48:03 +0000 | [diff] [blame] | 611 | SourceLocation TypeStartLoc = Tok.getLocation(); |
Chris Lattner | 271f1a6 | 2007-09-27 15:15:46 +0000 | [diff] [blame] | 612 | TypeTy *Ty = 0; |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 613 | |
Fariborz Jahanian | 19d74e1 | 2007-10-31 21:59:43 +0000 | [diff] [blame] | 614 | // Parse type qualifiers, in, inout, etc. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 615 | ParseObjCTypeQualifierList(DS); |
Steve Naroff | 4fa7afd | 2007-08-22 23:18:22 +0000 | [diff] [blame] | 616 | |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 617 | if (isTypeSpecifierQualifier()) { |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 618 | Ty = ParseTypeName(); |
| 619 | // FIXME: back when Sema support is in place... |
| 620 | // assert(Ty && "Parser::ParseObjCTypeName(): missing type"); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 621 | } |
Chris Lattner | e8904e9 | 2008-08-23 01:48:03 +0000 | [diff] [blame] | 622 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 623 | if (Tok.isNot(tok::r_paren)) { |
Chris Lattner | e8904e9 | 2008-08-23 01:48:03 +0000 | [diff] [blame] | 624 | // If we didn't eat any tokens, then this isn't a type. |
| 625 | if (Tok.getLocation() == TypeStartLoc) { |
| 626 | Diag(Tok.getLocation(), diag::err_expected_type); |
| 627 | SkipUntil(tok::r_brace); |
| 628 | } else { |
| 629 | // Otherwise, we found *something*, but didn't get a ')' in the right |
| 630 | // place. Emit an error then return what we have as the type. |
| 631 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 632 | } |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 633 | } |
| 634 | RParenLoc = ConsumeParen(); |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 635 | return Ty; |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /// objc-method-decl: |
| 639 | /// objc-selector |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 640 | /// objc-keyword-selector objc-parmlist[opt] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 641 | /// objc-type-name objc-selector |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 642 | /// objc-type-name objc-keyword-selector objc-parmlist[opt] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 643 | /// |
| 644 | /// objc-keyword-selector: |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 645 | /// objc-keyword-decl |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 646 | /// objc-keyword-selector objc-keyword-decl |
| 647 | /// |
| 648 | /// objc-keyword-decl: |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 649 | /// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier |
| 650 | /// objc-selector ':' objc-keyword-attributes[opt] identifier |
| 651 | /// ':' objc-type-name objc-keyword-attributes[opt] identifier |
| 652 | /// ':' objc-keyword-attributes[opt] identifier |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 653 | /// |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 654 | /// objc-parmlist: |
| 655 | /// objc-parms objc-ellipsis[opt] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 656 | /// |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 657 | /// objc-parms: |
| 658 | /// objc-parms , parameter-declaration |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 659 | /// |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 660 | /// objc-ellipsis: |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 661 | /// , ... |
| 662 | /// |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 663 | /// objc-keyword-attributes: [OBJC2] |
| 664 | /// __attribute__((unused)) |
| 665 | /// |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 666 | Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc, |
Fariborz Jahanian | 1f7b6f8 | 2007-11-09 19:52:12 +0000 | [diff] [blame] | 667 | tok::TokenKind mType, |
| 668 | DeclTy *IDecl, |
Chris Lattner | cb53b36 | 2007-12-27 19:57:00 +0000 | [diff] [blame] | 669 | tok::ObjCKeywordKind MethodImplKind) |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 670 | { |
Chris Lattner | e8904e9 | 2008-08-23 01:48:03 +0000 | [diff] [blame] | 671 | // Parse the return type if present. |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 672 | TypeTy *ReturnType = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 673 | ObjCDeclSpec DSRet; |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 674 | if (Tok.is(tok::l_paren)) |
Fariborz Jahanian | f1de0ca | 2007-10-31 23:53:01 +0000 | [diff] [blame] | 675 | ReturnType = ParseObjCTypeName(DSRet); |
Chris Lattner | e8904e9 | 2008-08-23 01:48:03 +0000 | [diff] [blame] | 676 | |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 677 | SourceLocation selLoc; |
| 678 | IdentifierInfo *SelIdent = ParseObjCSelector(selLoc); |
Chris Lattner | e8904e9 | 2008-08-23 01:48:03 +0000 | [diff] [blame] | 679 | |
| 680 | if (!SelIdent) { // missing selector name. |
| 681 | Diag(Tok.getLocation(), diag::err_expected_selector_for_method, |
| 682 | SourceRange(mLoc, Tok.getLocation())); |
| 683 | // Skip until we get a ; or {}. |
| 684 | SkipUntil(tok::r_brace); |
| 685 | return 0; |
| 686 | } |
| 687 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 688 | if (Tok.isNot(tok::colon)) { |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 689 | // If attributes exist after the method, parse them. |
| 690 | AttributeList *MethodAttrs = 0; |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 691 | if (getLang().ObjC2 && Tok.is(tok::kw___attribute)) |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 692 | MethodAttrs = ParseAttributes(); |
| 693 | |
| 694 | Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent); |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 695 | return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(), |
Fariborz Jahanian | 1f7b6f8 | 2007-11-09 19:52:12 +0000 | [diff] [blame] | 696 | mType, IDecl, DSRet, ReturnType, Sel, |
Fariborz Jahanian | f1de0ca | 2007-10-31 23:53:01 +0000 | [diff] [blame] | 697 | 0, 0, 0, MethodAttrs, MethodImplKind); |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 698 | } |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 699 | |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 700 | llvm::SmallVector<IdentifierInfo *, 12> KeyIdents; |
| 701 | llvm::SmallVector<Action::TypeTy *, 12> KeyTypes; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 702 | llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals; |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 703 | llvm::SmallVector<IdentifierInfo *, 12> ArgNames; |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 704 | |
| 705 | Action::TypeTy *TypeInfo; |
| 706 | while (1) { |
| 707 | KeyIdents.push_back(SelIdent); |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 708 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 709 | // Each iteration parses a single keyword argument. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 710 | if (Tok.isNot(tok::colon)) { |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 711 | Diag(Tok, diag::err_expected_colon); |
| 712 | break; |
| 713 | } |
| 714 | ConsumeToken(); // Eat the ':'. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 715 | ObjCDeclSpec DSType; |
Chris Lattner | e8904e9 | 2008-08-23 01:48:03 +0000 | [diff] [blame] | 716 | if (Tok.is(tok::l_paren)) // Parse the argument type. |
Fariborz Jahanian | f1de0ca | 2007-10-31 23:53:01 +0000 | [diff] [blame] | 717 | TypeInfo = ParseObjCTypeName(DSType); |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 718 | else |
| 719 | TypeInfo = 0; |
| 720 | KeyTypes.push_back(TypeInfo); |
Fariborz Jahanian | f1de0ca | 2007-10-31 23:53:01 +0000 | [diff] [blame] | 721 | ArgTypeQuals.push_back(DSType); |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 722 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 723 | // If attributes exist before the argument name, parse them. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 724 | if (getLang().ObjC2 && Tok.is(tok::kw___attribute)) |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 725 | ParseAttributes(); // FIXME: pass attributes through. |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 726 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 727 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 728 | Diag(Tok, diag::err_expected_ident); // missing argument name. |
| 729 | break; |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 730 | } |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 731 | ArgNames.push_back(Tok.getIdentifierInfo()); |
| 732 | ConsumeToken(); // Eat the identifier. |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 733 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 734 | // Check for another keyword selector. |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 735 | SourceLocation Loc; |
| 736 | SelIdent = ParseObjCSelector(Loc); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 737 | if (!SelIdent && Tok.isNot(tok::colon)) |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 738 | break; |
| 739 | // We have a selector or a colon, continue parsing. |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 740 | } |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 741 | |
Steve Naroff | 335eafa | 2007-11-15 12:35:21 +0000 | [diff] [blame] | 742 | bool isVariadic = false; |
| 743 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 744 | // Parse the (optional) parameter list. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 745 | while (Tok.is(tok::comma)) { |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 746 | ConsumeToken(); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 747 | if (Tok.is(tok::ellipsis)) { |
Steve Naroff | 335eafa | 2007-11-15 12:35:21 +0000 | [diff] [blame] | 748 | isVariadic = true; |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 749 | ConsumeToken(); |
| 750 | break; |
| 751 | } |
Steve Naroff | 335eafa | 2007-11-15 12:35:21 +0000 | [diff] [blame] | 752 | // FIXME: implement this... |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 753 | // Parse the c-style argument declaration-specifier. |
| 754 | DeclSpec DS; |
| 755 | ParseDeclarationSpecifiers(DS); |
| 756 | // Parse the declarator. |
| 757 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 758 | ParseDeclarator(ParmDecl); |
| 759 | } |
| 760 | |
| 761 | // FIXME: Add support for optional parmameter list... |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 762 | // If attributes exist after the method, parse them. |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 763 | AttributeList *MethodAttrs = 0; |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 764 | if (getLang().ObjC2 && Tok.is(tok::kw___attribute)) |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 765 | MethodAttrs = ParseAttributes(); |
| 766 | |
| 767 | Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(), |
| 768 | &KeyIdents[0]); |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 769 | return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(), |
Fariborz Jahanian | 1f7b6f8 | 2007-11-09 19:52:12 +0000 | [diff] [blame] | 770 | mType, IDecl, DSRet, ReturnType, Sel, |
Fariborz Jahanian | f1de0ca | 2007-10-31 23:53:01 +0000 | [diff] [blame] | 771 | &ArgTypeQuals[0], &KeyTypes[0], |
Steve Naroff | 335eafa | 2007-11-15 12:35:21 +0000 | [diff] [blame] | 772 | &ArgNames[0], MethodAttrs, |
| 773 | MethodImplKind, isVariadic); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 776 | /// objc-protocol-refs: |
| 777 | /// '<' identifier-list '>' |
| 778 | /// |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 779 | bool Parser:: |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 780 | ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclTy*> &Protocols, |
| 781 | bool WarnOnDeclarations, SourceLocation &EndLoc) { |
| 782 | assert(Tok.is(tok::less) && "expected <"); |
| 783 | |
| 784 | ConsumeToken(); // the "<" |
| 785 | |
| 786 | llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents; |
| 787 | |
| 788 | while (1) { |
| 789 | if (Tok.isNot(tok::identifier)) { |
| 790 | Diag(Tok, diag::err_expected_ident); |
| 791 | SkipUntil(tok::greater); |
| 792 | return true; |
| 793 | } |
| 794 | ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(), |
| 795 | Tok.getLocation())); |
| 796 | ConsumeToken(); |
| 797 | |
| 798 | if (Tok.isNot(tok::comma)) |
| 799 | break; |
| 800 | ConsumeToken(); |
| 801 | } |
| 802 | |
| 803 | // Consume the '>'. |
| 804 | if (Tok.isNot(tok::greater)) { |
| 805 | Diag(Tok, diag::err_expected_greater); |
| 806 | return true; |
| 807 | } |
| 808 | |
| 809 | EndLoc = ConsumeAnyToken(); |
| 810 | |
| 811 | // Convert the list of protocols identifiers into a list of protocol decls. |
| 812 | Actions.FindProtocolDeclaration(WarnOnDeclarations, |
| 813 | &ProtocolIdents[0], ProtocolIdents.size(), |
| 814 | Protocols); |
| 815 | return false; |
| 816 | } |
| 817 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 818 | /// objc-class-instance-variables: |
| 819 | /// '{' objc-instance-variable-decl-list[opt] '}' |
| 820 | /// |
| 821 | /// objc-instance-variable-decl-list: |
| 822 | /// objc-visibility-spec |
| 823 | /// objc-instance-variable-decl ';' |
| 824 | /// ';' |
| 825 | /// objc-instance-variable-decl-list objc-visibility-spec |
| 826 | /// objc-instance-variable-decl-list objc-instance-variable-decl ';' |
| 827 | /// objc-instance-variable-decl-list ';' |
| 828 | /// |
| 829 | /// objc-visibility-spec: |
| 830 | /// @private |
| 831 | /// @protected |
| 832 | /// @public |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 833 | /// @package [OBJC2] |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 834 | /// |
| 835 | /// objc-instance-variable-decl: |
| 836 | /// struct-declaration |
| 837 | /// |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 838 | void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl, |
| 839 | SourceLocation atLoc) { |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 840 | assert(Tok.is(tok::l_brace) && "expected {"); |
Fariborz Jahanian | 7d6402f | 2007-09-13 20:56:13 +0000 | [diff] [blame] | 841 | llvm::SmallVector<DeclTy*, 32> AllIvarDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 842 | llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators; |
| 843 | |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 844 | SourceLocation LBraceLoc = ConsumeBrace(); // the "{" |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 845 | |
Fariborz Jahanian | aa847fe | 2008-04-29 23:03:51 +0000 | [diff] [blame] | 846 | tok::ObjCKeywordKind visibility = tok::objc_protected; |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 847 | // While we still have something to read, read the instance variables. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 848 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 849 | // Each iteration of this loop reads one objc-instance-variable-decl. |
| 850 | |
| 851 | // Check for extraneous top-level semicolon. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 852 | if (Tok.is(tok::semi)) { |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 853 | Diag(Tok, diag::ext_extra_struct_semi); |
| 854 | ConsumeToken(); |
| 855 | continue; |
| 856 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 857 | |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 858 | // Set the default visibility to private. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 859 | if (Tok.is(tok::at)) { // parse objc-visibility-spec |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 860 | ConsumeToken(); // eat the @ sign |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 861 | switch (Tok.getObjCKeywordID()) { |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 862 | case tok::objc_private: |
| 863 | case tok::objc_public: |
| 864 | case tok::objc_protected: |
| 865 | case tok::objc_package: |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 866 | visibility = Tok.getObjCKeywordID(); |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 867 | ConsumeToken(); |
| 868 | continue; |
| 869 | default: |
| 870 | Diag(Tok, diag::err_objc_illegal_visibility_spec); |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 871 | continue; |
| 872 | } |
| 873 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 874 | |
| 875 | // Parse all the comma separated declarators. |
| 876 | DeclSpec DS; |
| 877 | FieldDeclarators.clear(); |
| 878 | ParseStructDeclaration(DS, FieldDeclarators); |
| 879 | |
| 880 | // Convert them all to fields. |
| 881 | for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) { |
| 882 | FieldDeclarator &FD = FieldDeclarators[i]; |
| 883 | // Install the declarator into interfaceDecl. |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 884 | DeclTy *Field = Actions.ActOnIvar(CurScope, |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 885 | DS.getSourceRange().getBegin(), |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 886 | FD.D, FD.BitfieldSize, visibility); |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 887 | AllIvarDecls.push_back(Field); |
Fariborz Jahanian | 7d6402f | 2007-09-13 20:56:13 +0000 | [diff] [blame] | 888 | } |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 889 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 890 | if (Tok.is(tok::semi)) { |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 891 | ConsumeToken(); |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 892 | } else { |
| 893 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 894 | // Skip to end of block or statement |
| 895 | SkipUntil(tok::r_brace, true, true); |
| 896 | } |
| 897 | } |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 898 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
Steve Naroff | 8749be5 | 2007-10-31 22:11:35 +0000 | [diff] [blame] | 899 | // Call ActOnFields() even if we don't have any decls. This is useful |
| 900 | // for code rewriting tools that need to be aware of the empty list. |
| 901 | Actions.ActOnFields(CurScope, atLoc, interfaceDecl, |
| 902 | &AllIvarDecls[0], AllIvarDecls.size(), |
Fariborz Jahanian | 1d78cc4 | 2008-04-10 23:32:45 +0000 | [diff] [blame] | 903 | LBraceLoc, RBraceLoc); |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 904 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 905 | } |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 906 | |
| 907 | /// objc-protocol-declaration: |
| 908 | /// objc-protocol-definition |
| 909 | /// objc-protocol-forward-reference |
| 910 | /// |
| 911 | /// objc-protocol-definition: |
| 912 | /// @protocol identifier |
| 913 | /// objc-protocol-refs[opt] |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 914 | /// objc-interface-decl-list |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 915 | /// @end |
| 916 | /// |
| 917 | /// objc-protocol-forward-reference: |
| 918 | /// @protocol identifier-list ';' |
| 919 | /// |
| 920 | /// "@protocol identifier ;" should be resolved as "@protocol |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 921 | /// identifier-list ;": objc-interface-decl-list may not start with a |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 922 | /// semicolon in the first alternative if objc-protocol-refs are omitted. |
Daniel Dunbar | 246e70f | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 923 | Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc, |
| 924 | AttributeList *attrList) { |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 925 | assert(Tok.isObjCAtKeyword(tok::objc_protocol) && |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 926 | "ParseObjCAtProtocolDeclaration(): Expected @protocol"); |
| 927 | ConsumeToken(); // the "protocol" identifier |
| 928 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 929 | if (Tok.isNot(tok::identifier)) { |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 930 | Diag(Tok, diag::err_expected_ident); // missing protocol name. |
| 931 | return 0; |
| 932 | } |
| 933 | // Save the protocol name, then consume it. |
| 934 | IdentifierInfo *protocolName = Tok.getIdentifierInfo(); |
| 935 | SourceLocation nameLoc = ConsumeToken(); |
| 936 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 937 | if (Tok.is(tok::semi)) { // forward declaration of one protocol. |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 938 | IdentifierLocPair ProtoInfo(protocolName, nameLoc); |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 939 | ConsumeToken(); |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 940 | return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1); |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 941 | } |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 942 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 943 | if (Tok.is(tok::comma)) { // list of forward declarations. |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 944 | llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs; |
| 945 | ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc)); |
| 946 | |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 947 | // Parse the list of forward declarations. |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 948 | while (1) { |
| 949 | ConsumeToken(); // the ',' |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 950 | if (Tok.isNot(tok::identifier)) { |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 951 | Diag(Tok, diag::err_expected_ident); |
| 952 | SkipUntil(tok::semi); |
| 953 | return 0; |
| 954 | } |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 955 | ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(), |
| 956 | Tok.getLocation())); |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 957 | ConsumeToken(); // the identifier |
| 958 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 959 | if (Tok.isNot(tok::comma)) |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 960 | break; |
| 961 | } |
| 962 | // Consume the ';'. |
| 963 | if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol")) |
| 964 | return 0; |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 965 | |
Steve Naroff | e440eb8 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 966 | return Actions.ActOnForwardProtocolDeclaration(AtLoc, |
Steve Naroff | 37e58d1 | 2007-10-02 22:39:18 +0000 | [diff] [blame] | 967 | &ProtocolRefs[0], |
| 968 | ProtocolRefs.size()); |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 971 | // Last, and definitely not least, parse a protocol declaration. |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 972 | SourceLocation EndProtoLoc; |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 973 | |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 974 | llvm::SmallVector<DeclTy *, 8> ProtocolRefs; |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 975 | if (Tok.is(tok::less) && |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 976 | ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc)) |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 977 | return 0; |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 978 | |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 979 | DeclTy *ProtoType = |
| 980 | Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc, |
| 981 | &ProtocolRefs[0], ProtocolRefs.size(), |
Daniel Dunbar | 246e70f | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 982 | EndProtoLoc, attrList); |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 983 | ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol); |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 984 | |
| 985 | // The @ sign was already consumed by ParseObjCInterfaceDeclList(). |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 986 | if (Tok.isObjCAtKeyword(tok::objc_end)) { |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 987 | ConsumeToken(); // the "end" identifier |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 988 | return ProtoType; |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 989 | } |
| 990 | Diag(Tok, diag::err_objc_missing_end); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 991 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 992 | } |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 993 | |
| 994 | /// objc-implementation: |
| 995 | /// objc-class-implementation-prologue |
| 996 | /// objc-category-implementation-prologue |
| 997 | /// |
| 998 | /// objc-class-implementation-prologue: |
| 999 | /// @implementation identifier objc-superclass[opt] |
| 1000 | /// objc-class-instance-variables[opt] |
| 1001 | /// |
| 1002 | /// objc-category-implementation-prologue: |
| 1003 | /// @implementation identifier ( identifier ) |
| 1004 | |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1005 | Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration( |
| 1006 | SourceLocation atLoc) { |
| 1007 | assert(Tok.isObjCAtKeyword(tok::objc_implementation) && |
| 1008 | "ParseObjCAtImplementationDeclaration(): Expected @implementation"); |
| 1009 | ConsumeToken(); // the "implementation" identifier |
| 1010 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1011 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1012 | Diag(Tok, diag::err_expected_ident); // missing class or category name. |
| 1013 | return 0; |
| 1014 | } |
| 1015 | // We have a class or category name - consume it. |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 1016 | IdentifierInfo *nameId = Tok.getIdentifierInfo(); |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1017 | SourceLocation nameLoc = ConsumeToken(); // consume class or category name |
| 1018 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1019 | if (Tok.is(tok::l_paren)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1020 | // we have a category implementation. |
| 1021 | SourceLocation lparenLoc = ConsumeParen(); |
| 1022 | SourceLocation categoryLoc, rparenLoc; |
| 1023 | IdentifierInfo *categoryId = 0; |
| 1024 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1025 | if (Tok.is(tok::identifier)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1026 | categoryId = Tok.getIdentifierInfo(); |
| 1027 | categoryLoc = ConsumeToken(); |
| 1028 | } else { |
| 1029 | Diag(Tok, diag::err_expected_ident); // missing category name. |
| 1030 | return 0; |
| 1031 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1032 | if (Tok.isNot(tok::r_paren)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1033 | Diag(Tok, diag::err_expected_rparen); |
| 1034 | SkipUntil(tok::r_paren, false); // don't stop at ';' |
| 1035 | return 0; |
| 1036 | } |
| 1037 | rparenLoc = ConsumeParen(); |
Steve Naroff | e440eb8 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 1038 | DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation( |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 1039 | atLoc, nameId, nameLoc, categoryId, |
| 1040 | categoryLoc); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1041 | ObjCImpDecl = ImplCatType; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 1042 | return 0; |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1043 | } |
| 1044 | // We have a class implementation |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 1045 | SourceLocation superClassLoc; |
| 1046 | IdentifierInfo *superClassId = 0; |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1047 | if (Tok.is(tok::colon)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1048 | // We have a super class |
| 1049 | ConsumeToken(); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1050 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1051 | Diag(Tok, diag::err_expected_ident); // missing super class name. |
| 1052 | return 0; |
| 1053 | } |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 1054 | superClassId = Tok.getIdentifierInfo(); |
| 1055 | superClassLoc = ConsumeToken(); // Consume super class name |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1056 | } |
Steve Naroff | e440eb8 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 1057 | DeclTy *ImplClsType = Actions.ActOnStartClassImplementation( |
Chris Lattner | cb53b36 | 2007-12-27 19:57:00 +0000 | [diff] [blame] | 1058 | atLoc, nameId, nameLoc, |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 1059 | superClassId, superClassLoc); |
| 1060 | |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 1061 | if (Tok.is(tok::l_brace)) // we have ivars |
| 1062 | ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1063 | ObjCImpDecl = ImplClsType; |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1064 | |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 1065 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1066 | } |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 1067 | |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1068 | Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) { |
| 1069 | assert(Tok.isObjCAtKeyword(tok::objc_end) && |
| 1070 | "ParseObjCAtEndDeclaration(): Expected @end"); |
| 1071 | ConsumeToken(); // the "end" identifier |
Fariborz Jahanian | 94cdb25 | 2008-01-10 17:58:07 +0000 | [diff] [blame] | 1072 | if (ObjCImpDecl) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1073 | Actions.ActOnAtEnd(atLoc, ObjCImpDecl); |
Fariborz Jahanian | 94cdb25 | 2008-01-10 17:58:07 +0000 | [diff] [blame] | 1074 | else |
| 1075 | Diag(atLoc, diag::warn_expected_implementation); // missing @implementation |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1076 | return ObjCImpDecl; |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 1077 | } |
Fariborz Jahanian | e992af0 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 1078 | |
| 1079 | /// compatibility-alias-decl: |
| 1080 | /// @compatibility_alias alias-name class-name ';' |
| 1081 | /// |
| 1082 | Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) { |
| 1083 | assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) && |
| 1084 | "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias"); |
| 1085 | ConsumeToken(); // consume compatibility_alias |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1086 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | e992af0 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 1087 | Diag(Tok, diag::err_expected_ident); |
| 1088 | return 0; |
| 1089 | } |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 1090 | IdentifierInfo *aliasId = Tok.getIdentifierInfo(); |
| 1091 | SourceLocation aliasLoc = ConsumeToken(); // consume alias-name |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1092 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | e992af0 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 1093 | Diag(Tok, diag::err_expected_ident); |
| 1094 | return 0; |
| 1095 | } |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 1096 | IdentifierInfo *classId = Tok.getIdentifierInfo(); |
| 1097 | SourceLocation classLoc = ConsumeToken(); // consume class-name; |
| 1098 | if (Tok.isNot(tok::semi)) { |
Fariborz Jahanian | 8cd8c66 | 2007-09-04 21:42:12 +0000 | [diff] [blame] | 1099 | Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias"); |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 1100 | return 0; |
| 1101 | } |
| 1102 | DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc, |
| 1103 | aliasId, aliasLoc, |
| 1104 | classId, classLoc); |
| 1105 | return ClsType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1108 | /// property-synthesis: |
| 1109 | /// @synthesize property-ivar-list ';' |
| 1110 | /// |
| 1111 | /// property-ivar-list: |
| 1112 | /// property-ivar |
| 1113 | /// property-ivar-list ',' property-ivar |
| 1114 | /// |
| 1115 | /// property-ivar: |
| 1116 | /// identifier |
| 1117 | /// identifier '=' identifier |
| 1118 | /// |
| 1119 | Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) { |
| 1120 | assert(Tok.isObjCAtKeyword(tok::objc_synthesize) && |
| 1121 | "ParseObjCPropertyDynamic(): Expected '@synthesize'"); |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1122 | SourceLocation loc = ConsumeToken(); // consume synthesize |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1123 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1124 | Diag(Tok, diag::err_expected_ident); |
| 1125 | return 0; |
| 1126 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1127 | while (Tok.is(tok::identifier)) { |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1128 | IdentifierInfo *propertyIvar = 0; |
| 1129 | IdentifierInfo *propertyId = Tok.getIdentifierInfo(); |
| 1130 | SourceLocation propertyLoc = ConsumeToken(); // consume property name |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1131 | if (Tok.is(tok::equal)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1132 | // property '=' ivar-name |
| 1133 | ConsumeToken(); // consume '=' |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1134 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1135 | Diag(Tok, diag::err_expected_ident); |
| 1136 | break; |
| 1137 | } |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1138 | propertyIvar = Tok.getIdentifierInfo(); |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1139 | ConsumeToken(); // consume ivar-name |
| 1140 | } |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1141 | Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl, |
| 1142 | propertyId, propertyIvar); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1143 | if (Tok.isNot(tok::comma)) |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1144 | break; |
| 1145 | ConsumeToken(); // consume ',' |
| 1146 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1147 | if (Tok.isNot(tok::semi)) |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1148 | Diag(Tok, diag::err_expected_semi_after, "@synthesize"); |
| 1149 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1152 | /// property-dynamic: |
| 1153 | /// @dynamic property-list |
| 1154 | /// |
| 1155 | /// property-list: |
| 1156 | /// identifier |
| 1157 | /// property-list ',' identifier |
| 1158 | /// |
| 1159 | Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) { |
| 1160 | assert(Tok.isObjCAtKeyword(tok::objc_dynamic) && |
| 1161 | "ParseObjCPropertyDynamic(): Expected '@dynamic'"); |
| 1162 | SourceLocation loc = ConsumeToken(); // consume dynamic |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1163 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1164 | Diag(Tok, diag::err_expected_ident); |
| 1165 | return 0; |
| 1166 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1167 | while (Tok.is(tok::identifier)) { |
Fariborz Jahanian | c35b9e4 | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1168 | IdentifierInfo *propertyId = Tok.getIdentifierInfo(); |
| 1169 | SourceLocation propertyLoc = ConsumeToken(); // consume property name |
| 1170 | Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl, |
| 1171 | propertyId, 0); |
| 1172 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1173 | if (Tok.isNot(tok::comma)) |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1174 | break; |
| 1175 | ConsumeToken(); // consume ',' |
| 1176 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1177 | if (Tok.isNot(tok::semi)) |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1178 | Diag(Tok, diag::err_expected_semi_after, "@dynamic"); |
| 1179 | return 0; |
| 1180 | } |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1181 | |
| 1182 | /// objc-throw-statement: |
| 1183 | /// throw expression[opt]; |
| 1184 | /// |
Fariborz Jahanian | 39f8f15 | 2007-11-07 02:00:49 +0000 | [diff] [blame] | 1185 | Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) { |
| 1186 | ExprResult Res; |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1187 | ConsumeToken(); // consume throw |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1188 | if (Tok.isNot(tok::semi)) { |
Fariborz Jahanian | 39f8f15 | 2007-11-07 02:00:49 +0000 | [diff] [blame] | 1189 | Res = ParseExpression(); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1190 | if (Res.isInvalid) { |
| 1191 | SkipUntil(tok::semi); |
Fariborz Jahanian | 39f8f15 | 2007-11-07 02:00:49 +0000 | [diff] [blame] | 1192 | return true; |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1193 | } |
| 1194 | } |
Fariborz Jahanian | 39f8f15 | 2007-11-07 02:00:49 +0000 | [diff] [blame] | 1195 | ConsumeToken(); // consume ';' |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1196 | return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Fariborz Jahanian | c385c90 | 2008-01-29 18:21:32 +0000 | [diff] [blame] | 1199 | /// objc-synchronized-statement: |
Fariborz Jahanian | 78a677b | 2008-01-30 17:38:29 +0000 | [diff] [blame] | 1200 | /// @synchronized '(' expression ')' compound-statement |
Fariborz Jahanian | c385c90 | 2008-01-29 18:21:32 +0000 | [diff] [blame] | 1201 | /// |
| 1202 | Parser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) { |
Fariborz Jahanian | fa3ee8e | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 1203 | ConsumeToken(); // consume synchronized |
| 1204 | if (Tok.isNot(tok::l_paren)) { |
| 1205 | Diag (Tok, diag::err_expected_lparen_after, "@synchronized"); |
| 1206 | return true; |
| 1207 | } |
| 1208 | ConsumeParen(); // '(' |
Fariborz Jahanian | 78a677b | 2008-01-30 17:38:29 +0000 | [diff] [blame] | 1209 | ExprResult Res = ParseExpression(); |
Fariborz Jahanian | fa3ee8e | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 1210 | if (Res.isInvalid) { |
| 1211 | SkipUntil(tok::semi); |
| 1212 | return true; |
| 1213 | } |
| 1214 | if (Tok.isNot(tok::r_paren)) { |
Fariborz Jahanian | 78a677b | 2008-01-30 17:38:29 +0000 | [diff] [blame] | 1215 | Diag (Tok, diag::err_expected_lbrace); |
Fariborz Jahanian | fa3ee8e | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 1216 | return true; |
| 1217 | } |
| 1218 | ConsumeParen(); // ')' |
Fariborz Jahanian | 78a677b | 2008-01-30 17:38:29 +0000 | [diff] [blame] | 1219 | if (Tok.isNot(tok::l_brace)) { |
| 1220 | Diag (Tok, diag::err_expected_lbrace); |
| 1221 | return true; |
| 1222 | } |
Steve Naroff | 3ac438c | 2008-06-04 20:36:13 +0000 | [diff] [blame] | 1223 | // Enter a scope to hold everything within the compound stmt. Compound |
| 1224 | // statements can always hold declarations. |
| 1225 | EnterScope(Scope::DeclScope); |
| 1226 | |
Fariborz Jahanian | fa3ee8e | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 1227 | StmtResult SynchBody = ParseCompoundStatementBody(); |
Steve Naroff | 3ac438c | 2008-06-04 20:36:13 +0000 | [diff] [blame] | 1228 | |
| 1229 | ExitScope(); |
Fariborz Jahanian | fa3ee8e | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 1230 | if (SynchBody.isInvalid) |
| 1231 | SynchBody = Actions.ActOnNullStmt(Tok.getLocation()); |
| 1232 | return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.Val, SynchBody.Val); |
Fariborz Jahanian | c385c90 | 2008-01-29 18:21:32 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1235 | /// objc-try-catch-statement: |
| 1236 | /// @try compound-statement objc-catch-list[opt] |
| 1237 | /// @try compound-statement objc-catch-list[opt] @finally compound-statement |
| 1238 | /// |
| 1239 | /// objc-catch-list: |
| 1240 | /// @catch ( parameter-declaration ) compound-statement |
| 1241 | /// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement |
| 1242 | /// catch-parameter-declaration: |
| 1243 | /// parameter-declaration |
| 1244 | /// '...' [OBJC2] |
| 1245 | /// |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 1246 | Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) { |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1247 | bool catch_or_finally_seen = false; |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 1248 | |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1249 | ConsumeToken(); // consume try |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1250 | if (Tok.isNot(tok::l_brace)) { |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1251 | Diag (Tok, diag::err_expected_lbrace); |
Fariborz Jahanian | b210bd0 | 2007-11-01 21:12:44 +0000 | [diff] [blame] | 1252 | return true; |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1253 | } |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 1254 | StmtResult CatchStmts; |
Fariborz Jahanian | 161a9c5 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 1255 | StmtResult FinallyStmt; |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1256 | StmtResult TryBody = ParseCompoundStatementBody(); |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 1257 | if (TryBody.isInvalid) |
| 1258 | TryBody = Actions.ActOnNullStmt(Tok.getLocation()); |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 1259 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1260 | while (Tok.is(tok::at)) { |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 1261 | // At this point, we need to lookahead to determine if this @ is the start |
| 1262 | // of an @catch or @finally. We don't want to consume the @ token if this |
| 1263 | // is an @try or @encode or something else. |
| 1264 | Token AfterAt = GetLookAheadToken(1); |
| 1265 | if (!AfterAt.isObjCAtKeyword(tok::objc_catch) && |
| 1266 | !AfterAt.isObjCAtKeyword(tok::objc_finally)) |
| 1267 | break; |
| 1268 | |
Fariborz Jahanian | 161a9c5 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 1269 | SourceLocation AtCatchFinallyLoc = ConsumeToken(); |
Chris Lattner | cb53b36 | 2007-12-27 19:57:00 +0000 | [diff] [blame] | 1270 | if (Tok.isObjCAtKeyword(tok::objc_catch)) { |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 1271 | StmtTy *FirstPart = 0; |
| 1272 | ConsumeToken(); // consume catch |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1273 | if (Tok.is(tok::l_paren)) { |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1274 | ConsumeParen(); |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 1275 | EnterScope(Scope::DeclScope); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1276 | if (Tok.isNot(tok::ellipsis)) { |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1277 | DeclSpec DS; |
| 1278 | ParseDeclarationSpecifiers(DS); |
Steve Naroff | 459a1e2 | 2008-06-03 05:36:54 +0000 | [diff] [blame] | 1279 | // For some odd reason, the name of the exception variable is |
| 1280 | // optional. As a result, we need to use PrototypeContext. |
| 1281 | Declarator DeclaratorInfo(DS, Declarator::PrototypeContext); |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 1282 | ParseDeclarator(DeclaratorInfo); |
Steve Naroff | 459a1e2 | 2008-06-03 05:36:54 +0000 | [diff] [blame] | 1283 | if (DeclaratorInfo.getIdentifier()) { |
| 1284 | DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope, |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 1285 | DeclaratorInfo, 0); |
Steve Naroff | 459a1e2 | 2008-06-03 05:36:54 +0000 | [diff] [blame] | 1286 | StmtResult stmtResult = |
| 1287 | Actions.ActOnDeclStmt(aBlockVarDecl, |
| 1288 | DS.getSourceRange().getBegin(), |
| 1289 | DeclaratorInfo.getSourceRange().getEnd()); |
| 1290 | FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val; |
| 1291 | } |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 1292 | } else |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1293 | ConsumeToken(); // consume '...' |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 1294 | SourceLocation RParenLoc = ConsumeParen(); |
Chris Lattner | c1b3ba5 | 2008-02-14 19:27:54 +0000 | [diff] [blame] | 1295 | |
| 1296 | StmtResult CatchBody(true); |
| 1297 | if (Tok.is(tok::l_brace)) |
| 1298 | CatchBody = ParseCompoundStatementBody(); |
| 1299 | else |
| 1300 | Diag(Tok, diag::err_expected_lbrace); |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 1301 | if (CatchBody.isInvalid) |
| 1302 | CatchBody = Actions.ActOnNullStmt(Tok.getLocation()); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1303 | CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc, |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 1304 | FirstPart, CatchBody.Val, CatchStmts.Val); |
| 1305 | ExitScope(); |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 1306 | } else { |
Fariborz Jahanian | 161a9c5 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 1307 | Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after, |
| 1308 | "@catch clause"); |
Fariborz Jahanian | b210bd0 | 2007-11-01 21:12:44 +0000 | [diff] [blame] | 1309 | return true; |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1310 | } |
| 1311 | catch_or_finally_seen = true; |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 1312 | } else { |
| 1313 | assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?"); |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 1314 | ConsumeToken(); // consume finally |
Ted Kremenek | aefc366 | 2008-09-26 00:31:16 +0000 | [diff] [blame] | 1315 | EnterScope(Scope::DeclScope); |
| 1316 | |
Chris Lattner | c1b3ba5 | 2008-02-14 19:27:54 +0000 | [diff] [blame] | 1317 | |
| 1318 | StmtResult FinallyBody(true); |
| 1319 | if (Tok.is(tok::l_brace)) |
| 1320 | FinallyBody = ParseCompoundStatementBody(); |
| 1321 | else |
| 1322 | Diag(Tok, diag::err_expected_lbrace); |
Fariborz Jahanian | 161a9c5 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 1323 | if (FinallyBody.isInvalid) |
| 1324 | FinallyBody = Actions.ActOnNullStmt(Tok.getLocation()); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1325 | FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc, |
Fariborz Jahanian | 161a9c5 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 1326 | FinallyBody.Val); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1327 | catch_or_finally_seen = true; |
Ted Kremenek | aefc366 | 2008-09-26 00:31:16 +0000 | [diff] [blame] | 1328 | ExitScope(); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1329 | break; |
| 1330 | } |
| 1331 | } |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 1332 | if (!catch_or_finally_seen) { |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1333 | Diag(atLoc, diag::err_missing_catch_finally); |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 1334 | return true; |
| 1335 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1336 | return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val, |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 1337 | FinallyStmt.Val); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1338 | } |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1339 | |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 1340 | /// objc-method-def: objc-method-proto ';'[opt] '{' body '}' |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1341 | /// |
Steve Naroff | 71c0a95 | 2007-11-13 23:01:27 +0000 | [diff] [blame] | 1342 | Parser::DeclTy *Parser::ParseObjCMethodDefinition() { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1343 | DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl); |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1344 | // parse optional ';' |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1345 | if (Tok.is(tok::semi)) |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1346 | ConsumeToken(); |
| 1347 | |
Steve Naroff | 409be83 | 2007-11-11 19:54:21 +0000 | [diff] [blame] | 1348 | // We should have an opening brace now. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1349 | if (Tok.isNot(tok::l_brace)) { |
Steve Naroff | da323ad | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 1350 | Diag(Tok, diag::err_expected_method_body); |
Steve Naroff | 409be83 | 2007-11-11 19:54:21 +0000 | [diff] [blame] | 1351 | |
| 1352 | // Skip over garbage, until we get to '{'. Don't eat the '{'. |
| 1353 | SkipUntil(tok::l_brace, true, true); |
| 1354 | |
| 1355 | // If we didn't find the '{', bail out. |
| 1356 | if (Tok.isNot(tok::l_brace)) |
Steve Naroff | 71c0a95 | 2007-11-13 23:01:27 +0000 | [diff] [blame] | 1357 | return 0; |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1358 | } |
Steve Naroff | 409be83 | 2007-11-11 19:54:21 +0000 | [diff] [blame] | 1359 | SourceLocation BraceLoc = Tok.getLocation(); |
| 1360 | |
| 1361 | // Enter a scope for the method body. |
| 1362 | EnterScope(Scope::FnScope|Scope::DeclScope); |
| 1363 | |
| 1364 | // Tell the actions module that we have entered a method definition with the |
Steve Naroff | 394f3f4 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 1365 | // specified Declarator for the method. |
| 1366 | Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl); |
Steve Naroff | 409be83 | 2007-11-11 19:54:21 +0000 | [diff] [blame] | 1367 | |
| 1368 | StmtResult FnBody = ParseCompoundStatementBody(); |
| 1369 | |
| 1370 | // If the function body could not be parsed, make a bogus compoundstmt. |
| 1371 | if (FnBody.isInvalid) |
| 1372 | FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false); |
| 1373 | |
| 1374 | // Leave the function body scope. |
| 1375 | ExitScope(); |
| 1376 | |
| 1377 | // TODO: Pass argument information. |
Steve Naroff | 394f3f4 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 1378 | Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val); |
Steve Naroff | 71c0a95 | 2007-11-13 23:01:27 +0000 | [diff] [blame] | 1379 | return MDecl; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1380 | } |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1381 | |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 1382 | Parser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) { |
| 1383 | if (Tok.isObjCAtKeyword(tok::objc_try)) { |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 1384 | return ParseObjCTryStmt(AtLoc); |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 1385 | } else if (Tok.isObjCAtKeyword(tok::objc_throw)) |
| 1386 | return ParseObjCThrowStmt(AtLoc); |
| 1387 | else if (Tok.isObjCAtKeyword(tok::objc_synchronized)) |
| 1388 | return ParseObjCSynchronizedStmt(AtLoc); |
| 1389 | ExprResult Res = ParseExpressionWithLeadingAt(AtLoc); |
| 1390 | if (Res.isInvalid) { |
| 1391 | // If the expression is invalid, skip ahead to the next semicolon. Not |
| 1392 | // doing this opens us up to the possibility of infinite loops if |
| 1393 | // ParseExpression does not consume any tokens. |
| 1394 | SkipUntil(tok::semi); |
| 1395 | return true; |
| 1396 | } |
| 1397 | // Otherwise, eat the semicolon. |
| 1398 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr); |
| 1399 | return Actions.ActOnExprStmt(Res.Val); |
| 1400 | } |
| 1401 | |
Steve Naroff | a642beb | 2007-10-15 20:55:58 +0000 | [diff] [blame] | 1402 | Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) { |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1403 | switch (Tok.getKind()) { |
Chris Lattner | b3a99cd | 2007-12-12 01:04:12 +0000 | [diff] [blame] | 1404 | case tok::string_literal: // primary-expression: string-literal |
| 1405 | case tok::wide_string_literal: |
| 1406 | return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc)); |
| 1407 | default: |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1408 | if (Tok.getIdentifierInfo() == 0) |
| 1409 | return Diag(AtLoc, diag::err_unexpected_at); |
| 1410 | |
| 1411 | switch (Tok.getIdentifierInfo()->getObjCKeywordID()) { |
| 1412 | case tok::objc_encode: |
| 1413 | return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc)); |
| 1414 | case tok::objc_protocol: |
| 1415 | return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc)); |
| 1416 | case tok::objc_selector: |
| 1417 | return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc)); |
| 1418 | default: |
| 1419 | return Diag(AtLoc, diag::err_unexpected_at); |
| 1420 | } |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1421 | } |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 1424 | /// objc-message-expr: |
| 1425 | /// '[' objc-receiver objc-message-args ']' |
| 1426 | /// |
| 1427 | /// objc-receiver: |
| 1428 | /// expression |
| 1429 | /// class-name |
| 1430 | /// type-name |
Chris Lattner | 699b661 | 2008-01-25 18:59:06 +0000 | [diff] [blame] | 1431 | Parser::ExprResult Parser::ParseObjCMessageExpression() { |
| 1432 | assert(Tok.is(tok::l_square) && "'[' expected"); |
| 1433 | SourceLocation LBracLoc = ConsumeBracket(); // consume '[' |
| 1434 | |
| 1435 | // Parse receiver |
Chris Lattner | 14dd98a | 2008-01-25 19:25:00 +0000 | [diff] [blame] | 1436 | if (isTokObjCMessageIdentifierReceiver()) { |
Chris Lattner | 699b661 | 2008-01-25 18:59:06 +0000 | [diff] [blame] | 1437 | IdentifierInfo *ReceiverName = Tok.getIdentifierInfo(); |
| 1438 | ConsumeToken(); |
| 1439 | return ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 0); |
| 1440 | } |
| 1441 | |
Chris Lattner | 69d349a | 2008-09-19 17:44:00 +0000 | [diff] [blame] | 1442 | ExprResult Res = ParseExpression(); |
Chris Lattner | 699b661 | 2008-01-25 18:59:06 +0000 | [diff] [blame] | 1443 | if (Res.isInvalid) { |
Chris Lattner | 5c74942 | 2008-01-25 19:43:26 +0000 | [diff] [blame] | 1444 | SkipUntil(tok::r_square); |
Chris Lattner | 699b661 | 2008-01-25 18:59:06 +0000 | [diff] [blame] | 1445 | return Res; |
| 1446 | } |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1447 | |
Chris Lattner | 699b661 | 2008-01-25 18:59:06 +0000 | [diff] [blame] | 1448 | return ParseObjCMessageExpressionBody(LBracLoc, 0, Res.Val); |
| 1449 | } |
| 1450 | |
| 1451 | /// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse |
| 1452 | /// the rest of a message expression. |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 1453 | /// |
| 1454 | /// objc-message-args: |
| 1455 | /// objc-selector |
| 1456 | /// objc-keywordarg-list |
| 1457 | /// |
| 1458 | /// objc-keywordarg-list: |
| 1459 | /// objc-keywordarg |
| 1460 | /// objc-keywordarg-list objc-keywordarg |
| 1461 | /// |
| 1462 | /// objc-keywordarg: |
| 1463 | /// selector-name[opt] ':' objc-keywordexpr |
| 1464 | /// |
| 1465 | /// objc-keywordexpr: |
| 1466 | /// nonempty-expr-list |
| 1467 | /// |
| 1468 | /// nonempty-expr-list: |
| 1469 | /// assignment-expression |
| 1470 | /// nonempty-expr-list , assignment-expression |
| 1471 | /// |
Chris Lattner | 699b661 | 2008-01-25 18:59:06 +0000 | [diff] [blame] | 1472 | Parser::ExprResult |
| 1473 | Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc, |
| 1474 | IdentifierInfo *ReceiverName, |
| 1475 | ExprTy *ReceiverExpr) { |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1476 | // Parse objc-selector |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1477 | SourceLocation Loc; |
| 1478 | IdentifierInfo *selIdent = ParseObjCSelector(Loc); |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1479 | |
| 1480 | llvm::SmallVector<IdentifierInfo *, 12> KeyIdents; |
| 1481 | llvm::SmallVector<Action::ExprTy *, 12> KeyExprs; |
| 1482 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1483 | if (Tok.is(tok::colon)) { |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1484 | while (1) { |
| 1485 | // Each iteration parses a single keyword argument. |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1486 | KeyIdents.push_back(selIdent); |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1487 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1488 | if (Tok.isNot(tok::colon)) { |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1489 | Diag(Tok, diag::err_expected_colon); |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1490 | // We must manually skip to a ']', otherwise the expression skipper will |
| 1491 | // stop at the ']' when it skips to the ';'. We want it to skip beyond |
| 1492 | // the enclosing expression. |
| 1493 | SkipUntil(tok::r_square); |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1494 | return true; |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1495 | } |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1496 | |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1497 | ConsumeToken(); // Eat the ':'. |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1498 | /// Parse the expression after ':' |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1499 | ExprResult Res = ParseAssignmentExpression(); |
| 1500 | if (Res.isInvalid) { |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1501 | // We must manually skip to a ']', otherwise the expression skipper will |
| 1502 | // stop at the ']' when it skips to the ';'. We want it to skip beyond |
| 1503 | // the enclosing expression. |
| 1504 | SkipUntil(tok::r_square); |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1505 | return Res; |
| 1506 | } |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1507 | |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1508 | // We have a valid expression. |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1509 | KeyExprs.push_back(Res.Val); |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1510 | |
| 1511 | // Check for another keyword selector. |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1512 | selIdent = ParseObjCSelector(Loc); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1513 | if (!selIdent && Tok.isNot(tok::colon)) |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1514 | break; |
| 1515 | // We have a selector or a colon, continue parsing. |
| 1516 | } |
| 1517 | // Parse the, optional, argument list, comma separated. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1518 | while (Tok.is(tok::comma)) { |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1519 | ConsumeToken(); // Eat the ','. |
| 1520 | /// Parse the expression after ',' |
| 1521 | ExprResult Res = ParseAssignmentExpression(); |
| 1522 | if (Res.isInvalid) { |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1523 | // We must manually skip to a ']', otherwise the expression skipper will |
| 1524 | // stop at the ']' when it skips to the ';'. We want it to skip beyond |
| 1525 | // the enclosing expression. |
| 1526 | SkipUntil(tok::r_square); |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1527 | return Res; |
| 1528 | } |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1529 | |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1530 | // We have a valid expression. |
| 1531 | KeyExprs.push_back(Res.Val); |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1532 | } |
| 1533 | } else if (!selIdent) { |
| 1534 | Diag(Tok, diag::err_expected_ident); // missing selector name. |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1535 | |
| 1536 | // We must manually skip to a ']', otherwise the expression skipper will |
| 1537 | // stop at the ']' when it skips to the ';'. We want it to skip beyond |
| 1538 | // the enclosing expression. |
| 1539 | SkipUntil(tok::r_square); |
Fariborz Jahanian | 0ba0aa1 | 2008-01-02 18:09:46 +0000 | [diff] [blame] | 1540 | return true; |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1541 | } |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1542 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1543 | if (Tok.isNot(tok::r_square)) { |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1544 | Diag(Tok, diag::err_expected_rsquare); |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1545 | // We must manually skip to a ']', otherwise the expression skipper will |
| 1546 | // stop at the ']' when it skips to the ';'. We want it to skip beyond |
| 1547 | // the enclosing expression. |
| 1548 | SkipUntil(tok::r_square); |
Fariborz Jahanian | 0ba0aa1 | 2008-01-02 18:09:46 +0000 | [diff] [blame] | 1549 | return true; |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1550 | } |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1551 | |
Chris Lattner | 699b661 | 2008-01-25 18:59:06 +0000 | [diff] [blame] | 1552 | SourceLocation RBracLoc = ConsumeBracket(); // consume ']' |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1553 | |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 1554 | unsigned nKeys = KeyIdents.size(); |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1555 | if (nKeys == 0) |
| 1556 | KeyIdents.push_back(selIdent); |
| 1557 | Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]); |
| 1558 | |
| 1559 | // We've just parsed a keyword message. |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1560 | if (ReceiverName) |
Fariborz Jahanian | 0523aaf | 2007-11-12 20:13:27 +0000 | [diff] [blame] | 1561 | return Actions.ActOnClassMessage(CurScope, |
Chris Lattner | 699b661 | 2008-01-25 18:59:06 +0000 | [diff] [blame] | 1562 | ReceiverName, Sel, LBracLoc, RBracLoc, |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1563 | &KeyExprs[0], KeyExprs.size()); |
Chris Lattner | 699b661 | 2008-01-25 18:59:06 +0000 | [diff] [blame] | 1564 | return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc, |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1565 | &KeyExprs[0], KeyExprs.size()); |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 1568 | Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) { |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1569 | ExprResult Res = ParseStringLiteralExpression(); |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1570 | if (Res.isInvalid) return Res; |
Chris Lattner | b3a99cd | 2007-12-12 01:04:12 +0000 | [diff] [blame] | 1571 | |
| 1572 | // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string |
| 1573 | // expressions. At this point, we know that the only valid thing that starts |
| 1574 | // with '@' is an @"". |
| 1575 | llvm::SmallVector<SourceLocation, 4> AtLocs; |
| 1576 | llvm::SmallVector<ExprTy*, 4> AtStrings; |
| 1577 | AtLocs.push_back(AtLoc); |
| 1578 | AtStrings.push_back(Res.Val); |
| 1579 | |
| 1580 | while (Tok.is(tok::at)) { |
| 1581 | AtLocs.push_back(ConsumeToken()); // eat the @. |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1582 | |
Chris Lattner | b3a99cd | 2007-12-12 01:04:12 +0000 | [diff] [blame] | 1583 | ExprResult Res(true); // Invalid unless there is a string literal. |
| 1584 | if (isTokenStringLiteral()) |
| 1585 | Res = ParseStringLiteralExpression(); |
| 1586 | else |
| 1587 | Diag(Tok, diag::err_objc_concat_string); |
| 1588 | |
| 1589 | if (Res.isInvalid) { |
| 1590 | while (!AtStrings.empty()) { |
| 1591 | Actions.DeleteExpr(AtStrings.back()); |
| 1592 | AtStrings.pop_back(); |
| 1593 | } |
| 1594 | return Res; |
| 1595 | } |
| 1596 | |
| 1597 | AtStrings.push_back(Res.Val); |
| 1598 | } |
Fariborz Jahanian | 79a99f2 | 2007-12-12 23:55:49 +0000 | [diff] [blame] | 1599 | |
Chris Lattner | b3a99cd | 2007-12-12 01:04:12 +0000 | [diff] [blame] | 1600 | return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0], |
| 1601 | AtStrings.size()); |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1602 | } |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1603 | |
| 1604 | /// objc-encode-expression: |
| 1605 | /// @encode ( type-name ) |
Chris Lattner | 674af95 | 2007-10-16 22:51:17 +0000 | [diff] [blame] | 1606 | Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) { |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 1607 | assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!"); |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1608 | |
| 1609 | SourceLocation EncLoc = ConsumeToken(); |
| 1610 | |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1611 | if (Tok.isNot(tok::l_paren)) |
| 1612 | return Diag(Tok, diag::err_expected_lparen_after, "@encode"); |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1613 | |
| 1614 | SourceLocation LParenLoc = ConsumeParen(); |
| 1615 | |
| 1616 | TypeTy *Ty = ParseTypeName(); |
| 1617 | |
Anders Carlsson | 4988ae3 | 2007-08-23 15:31:37 +0000 | [diff] [blame] | 1618 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1619 | |
Chris Lattner | 674af95 | 2007-10-16 22:51:17 +0000 | [diff] [blame] | 1620 | return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty, |
Anders Carlsson | 4988ae3 | 2007-08-23 15:31:37 +0000 | [diff] [blame] | 1621 | RParenLoc); |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1622 | } |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1623 | |
| 1624 | /// objc-protocol-expression |
| 1625 | /// @protocol ( protocol-name ) |
| 1626 | |
Fariborz Jahanian | 2a35fa9 | 2007-10-16 23:21:02 +0000 | [diff] [blame] | 1627 | Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1628 | { |
| 1629 | SourceLocation ProtoLoc = ConsumeToken(); |
| 1630 | |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1631 | if (Tok.isNot(tok::l_paren)) |
| 1632 | return Diag(Tok, diag::err_expected_lparen_after, "@protocol"); |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1633 | |
| 1634 | SourceLocation LParenLoc = ConsumeParen(); |
| 1635 | |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1636 | if (Tok.isNot(tok::identifier)) |
| 1637 | return Diag(Tok, diag::err_expected_ident); |
| 1638 | |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1639 | IdentifierInfo *protocolId = Tok.getIdentifierInfo(); |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1640 | ConsumeToken(); |
| 1641 | |
Anders Carlsson | 4988ae3 | 2007-08-23 15:31:37 +0000 | [diff] [blame] | 1642 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1643 | |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1644 | return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc, |
| 1645 | LParenLoc, RParenLoc); |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1646 | } |
Fariborz Jahanian | a0818e3 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 1647 | |
| 1648 | /// objc-selector-expression |
| 1649 | /// @selector '(' objc-keyword-selector ')' |
Fariborz Jahanian | 2a35fa9 | 2007-10-16 23:21:02 +0000 | [diff] [blame] | 1650 | Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) |
Fariborz Jahanian | a0818e3 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 1651 | { |
| 1652 | SourceLocation SelectorLoc = ConsumeToken(); |
| 1653 | |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1654 | if (Tok.isNot(tok::l_paren)) |
| 1655 | return Diag(Tok, diag::err_expected_lparen_after, "@selector"); |
Fariborz Jahanian | a0818e3 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 1656 | |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1657 | llvm::SmallVector<IdentifierInfo *, 12> KeyIdents; |
Fariborz Jahanian | a0818e3 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 1658 | SourceLocation LParenLoc = ConsumeParen(); |
| 1659 | SourceLocation sLoc; |
| 1660 | IdentifierInfo *SelIdent = ParseObjCSelector(sLoc); |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1661 | if (!SelIdent && Tok.isNot(tok::colon)) |
| 1662 | return Diag(Tok, diag::err_expected_ident); // missing selector name. |
| 1663 | |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1664 | KeyIdents.push_back(SelIdent); |
Steve Naroff | 887407e | 2007-12-05 22:21:29 +0000 | [diff] [blame] | 1665 | unsigned nColons = 0; |
| 1666 | if (Tok.isNot(tok::r_paren)) { |
Fariborz Jahanian | a0818e3 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 1667 | while (1) { |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1668 | if (Tok.isNot(tok::colon)) |
| 1669 | return Diag(Tok, diag::err_expected_colon); |
| 1670 | |
Chris Lattner | cb53b36 | 2007-12-27 19:57:00 +0000 | [diff] [blame] | 1671 | nColons++; |
Fariborz Jahanian | a0818e3 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 1672 | ConsumeToken(); // Eat the ':'. |
| 1673 | if (Tok.is(tok::r_paren)) |
| 1674 | break; |
| 1675 | // Check for another keyword selector. |
| 1676 | SourceLocation Loc; |
| 1677 | SelIdent = ParseObjCSelector(Loc); |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1678 | KeyIdents.push_back(SelIdent); |
Fariborz Jahanian | a0818e3 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 1679 | if (!SelIdent && Tok.isNot(tok::colon)) |
| 1680 | break; |
| 1681 | } |
Steve Naroff | 887407e | 2007-12-05 22:21:29 +0000 | [diff] [blame] | 1682 | } |
Fariborz Jahanian | a0818e3 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 1683 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Steve Naroff | 887407e | 2007-12-05 22:21:29 +0000 | [diff] [blame] | 1684 | Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]); |
Fariborz Jahanian | 2a35fa9 | 2007-10-16 23:21:02 +0000 | [diff] [blame] | 1685 | return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc, |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1686 | RParenLoc); |
Gabor Greif | 58065b2 | 2007-10-19 15:38:32 +0000 | [diff] [blame] | 1687 | } |