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