Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1 | //===--- ParseTemplate.cpp - Template Parsing -----------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements parsing of C++ templates. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Parse/Parser.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 15 | #include "clang/Parse/ParseDiagnostic.h" |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 16 | #include "clang/Parse/DeclSpec.h" |
| 17 | #include "clang/Parse/Scope.h" |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 18 | #include "AstGuard.h" |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace clang; |
| 21 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 22 | /// \brief Parse a template declaration or an explicit specialization. |
| 23 | /// |
| 24 | /// Template declarations include one or more template parameter lists |
| 25 | /// and either the function or class template declaration. Explicit |
| 26 | /// specializations contain one or more 'template < >' prefixes |
| 27 | /// followed by a (possibly templated) declaration. Since the |
| 28 | /// syntactic form of both features is nearly identical, we parse all |
| 29 | /// of the template headers together and let semantic analysis sort |
| 30 | /// the declarations from the explicit specializations. |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 31 | /// |
| 32 | /// template-declaration: [C++ temp] |
| 33 | /// 'export'[opt] 'template' '<' template-parameter-list '>' declaration |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 34 | /// |
| 35 | /// explicit-specialization: [ C++ temp.expl.spec] |
| 36 | /// 'template' '<' '>' declaration |
| 37 | Parser::DeclTy * |
| 38 | Parser::ParseTemplateDeclarationOrSpecialization(unsigned Context) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 39 | assert((Tok.is(tok::kw_export) || Tok.is(tok::kw_template)) && |
| 40 | "Token does not start a template declaration."); |
| 41 | |
Douglas Gregor | 26236e8 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 42 | // Enter template-parameter scope. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 43 | ParseScope TemplateParmScope(this, Scope::TemplateParamScope); |
Douglas Gregor | 26236e8 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 44 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 45 | // Parse multiple levels of template headers within this template |
| 46 | // parameter scope, e.g., |
| 47 | // |
| 48 | // template<typename T> |
| 49 | // template<typename U> |
| 50 | // class A<T>::B { ... }; |
| 51 | // |
| 52 | // We parse multiple levels non-recursively so that we can build a |
| 53 | // single data structure containing all of the template parameter |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 54 | // lists to easily differentiate between the case above and: |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 55 | // |
| 56 | // template<typename T> |
| 57 | // class A { |
| 58 | // template<typename U> class B; |
| 59 | // }; |
| 60 | // |
| 61 | // In the first case, the action for declaring A<T>::B receives |
| 62 | // both template parameter lists. In the second case, the action for |
| 63 | // defining A<T>::B receives just the inner template parameter list |
| 64 | // (and retrieves the outer template parameter list from its |
| 65 | // context). |
| 66 | TemplateParameterLists ParamLists; |
| 67 | do { |
| 68 | // Consume the 'export', if any. |
| 69 | SourceLocation ExportLoc; |
| 70 | if (Tok.is(tok::kw_export)) { |
| 71 | ExportLoc = ConsumeToken(); |
| 72 | } |
| 73 | |
| 74 | // Consume the 'template', which should be here. |
| 75 | SourceLocation TemplateLoc; |
| 76 | if (Tok.is(tok::kw_template)) { |
| 77 | TemplateLoc = ConsumeToken(); |
| 78 | } else { |
| 79 | Diag(Tok.getLocation(), diag::err_expected_template); |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | // Parse the '<' template-parameter-list '>' |
| 84 | SourceLocation LAngleLoc, RAngleLoc; |
| 85 | TemplateParameterList TemplateParams; |
| 86 | ParseTemplateParameters(ParamLists.size(), TemplateParams, LAngleLoc, |
| 87 | RAngleLoc); |
| 88 | |
| 89 | ParamLists.push_back( |
| 90 | Actions.ActOnTemplateParameterList(ParamLists.size(), ExportLoc, |
| 91 | TemplateLoc, LAngleLoc, |
| 92 | &TemplateParams[0], |
| 93 | TemplateParams.size(), RAngleLoc)); |
| 94 | } while (Tok.is(tok::kw_export) || Tok.is(tok::kw_template)); |
| 95 | |
| 96 | // Parse the actual template declaration. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 97 | return ParseDeclarationOrFunctionDefinition(&ParamLists); |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /// ParseTemplateParameters - Parses a template-parameter-list enclosed in |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 101 | /// angle brackets. Depth is the depth of this template-parameter-list, which |
| 102 | /// is the number of template headers directly enclosing this template header. |
| 103 | /// TemplateParams is the current list of template parameters we're building. |
| 104 | /// The template parameter we parse will be added to this list. LAngleLoc and |
| 105 | /// RAngleLoc will receive the positions of the '<' and '>', respectively, |
| 106 | /// that enclose this template parameter list. |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 107 | bool Parser::ParseTemplateParameters(unsigned Depth, |
| 108 | TemplateParameterList &TemplateParams, |
| 109 | SourceLocation &LAngleLoc, |
| 110 | SourceLocation &RAngleLoc) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 111 | // Get the template parameter list. |
| 112 | if(!Tok.is(tok::less)) { |
| 113 | Diag(Tok.getLocation(), diag::err_expected_less_after) << "template"; |
| 114 | return false; |
| 115 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 116 | LAngleLoc = ConsumeToken(); |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 117 | |
| 118 | // Try to parse the template parameter list. |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 119 | if (Tok.is(tok::greater)) |
| 120 | RAngleLoc = ConsumeToken(); |
| 121 | else if(ParseTemplateParameterList(Depth, TemplateParams)) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 122 | if(!Tok.is(tok::greater)) { |
| 123 | Diag(Tok.getLocation(), diag::err_expected_greater); |
| 124 | return false; |
| 125 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 126 | RAngleLoc = ConsumeToken(); |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 127 | } |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | /// ParseTemplateParameterList - Parse a template parameter list. If |
| 132 | /// the parsing fails badly (i.e., closing bracket was left out), this |
| 133 | /// will try to put the token stream in a reasonable position (closing |
| 134 | /// a statement, etc.) and return false. |
| 135 | /// |
| 136 | /// template-parameter-list: [C++ temp] |
| 137 | /// template-parameter |
| 138 | /// template-parameter-list ',' template-parameter |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 139 | bool |
| 140 | Parser::ParseTemplateParameterList(unsigned Depth, |
| 141 | TemplateParameterList &TemplateParams) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 142 | while(1) { |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 143 | if (DeclTy* TmpParam |
| 144 | = ParseTemplateParameter(Depth, TemplateParams.size())) { |
| 145 | TemplateParams.push_back(TmpParam); |
| 146 | } else { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 147 | // If we failed to parse a template parameter, skip until we find |
| 148 | // a comma or closing brace. |
| 149 | SkipUntil(tok::comma, tok::greater, true, true); |
| 150 | } |
| 151 | |
| 152 | // Did we find a comma or the end of the template parmeter list? |
| 153 | if(Tok.is(tok::comma)) { |
| 154 | ConsumeToken(); |
| 155 | } else if(Tok.is(tok::greater)) { |
| 156 | // Don't consume this... that's done by template parser. |
| 157 | break; |
| 158 | } else { |
| 159 | // Somebody probably forgot to close the template. Skip ahead and |
| 160 | // try to get out of the expression. This error is currently |
| 161 | // subsumed by whatever goes on in ParseTemplateParameter. |
| 162 | // TODO: This could match >>, and it would be nice to avoid those |
| 163 | // silly errors with template <vec<T>>. |
| 164 | // Diag(Tok.getLocation(), diag::err_expected_comma_greater); |
| 165 | SkipUntil(tok::greater, true, true); |
| 166 | return false; |
| 167 | } |
| 168 | } |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | /// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]). |
| 173 | /// |
| 174 | /// template-parameter: [C++ temp.param] |
| 175 | /// type-parameter |
| 176 | /// parameter-declaration |
| 177 | /// |
| 178 | /// type-parameter: (see below) |
| 179 | /// 'class' identifier[opt] |
| 180 | /// 'class' identifier[opt] '=' type-id |
| 181 | /// 'typename' identifier[opt] |
| 182 | /// 'typename' identifier[opt] '=' type-id |
| 183 | /// 'template' '<' template-parameter-list '>' 'class' identifier[opt] |
| 184 | /// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 185 | Parser::DeclTy * |
| 186 | Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) { |
Chris Lattner | 532e19b | 2009-01-04 23:51:17 +0000 | [diff] [blame] | 187 | if(Tok.is(tok::kw_class) || |
| 188 | (Tok.is(tok::kw_typename) && |
| 189 | // FIXME: Next token has not been annotated! |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 190 | NextToken().isNot(tok::annot_typename))) { |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 191 | return ParseTypeParameter(Depth, Position); |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 192 | } |
Chris Lattner | 532e19b | 2009-01-04 23:51:17 +0000 | [diff] [blame] | 193 | |
| 194 | if(Tok.is(tok::kw_template)) |
| 195 | return ParseTemplateTemplateParameter(Depth, Position); |
| 196 | |
| 197 | // If it's none of the above, then it must be a parameter declaration. |
| 198 | // NOTE: This will pick up errors in the closure of the template parameter |
| 199 | // list (e.g., template < ; Check here to implement >> style closures. |
| 200 | return ParseNonTypeTemplateParameter(Depth, Position); |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]). |
| 204 | /// Other kinds of template parameters are parsed in |
| 205 | /// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter. |
| 206 | /// |
| 207 | /// type-parameter: [C++ temp.param] |
| 208 | /// 'class' identifier[opt] |
| 209 | /// 'class' identifier[opt] '=' type-id |
| 210 | /// 'typename' identifier[opt] |
| 211 | /// 'typename' identifier[opt] '=' type-id |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 212 | Parser::DeclTy *Parser::ParseTypeParameter(unsigned Depth, unsigned Position) { |
Douglas Gregor | 26236e8 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 213 | assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) && |
| 214 | "A type-parameter starts with 'class' or 'typename'"); |
| 215 | |
| 216 | // Consume the 'class' or 'typename' keyword. |
| 217 | bool TypenameKeyword = Tok.is(tok::kw_typename); |
| 218 | SourceLocation KeyLoc = ConsumeToken(); |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 219 | |
| 220 | // Grab the template parameter name (if given) |
Douglas Gregor | 26236e8 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 221 | SourceLocation NameLoc; |
| 222 | IdentifierInfo* ParamName = 0; |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 223 | if(Tok.is(tok::identifier)) { |
Douglas Gregor | 26236e8 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 224 | ParamName = Tok.getIdentifierInfo(); |
| 225 | NameLoc = ConsumeToken(); |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 226 | } else if(Tok.is(tok::equal) || Tok.is(tok::comma) || |
| 227 | Tok.is(tok::greater)) { |
| 228 | // Unnamed template parameter. Don't have to do anything here, just |
| 229 | // don't consume this token. |
| 230 | } else { |
| 231 | Diag(Tok.getLocation(), diag::err_expected_ident); |
| 232 | return 0; |
| 233 | } |
| 234 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 235 | DeclTy *TypeParam = Actions.ActOnTypeParameter(CurScope, TypenameKeyword, |
| 236 | KeyLoc, ParamName, NameLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 237 | Depth, Position); |
Douglas Gregor | 26236e8 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 238 | |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 239 | // Grab a default type id (if given). |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 240 | if(Tok.is(tok::equal)) { |
Douglas Gregor | 26236e8 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 241 | SourceLocation EqualLoc = ConsumeToken(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 242 | SourceLocation DefaultLoc = Tok.getLocation(); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 243 | if (TypeTy *DefaultType = ParseTypeName()) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 244 | Actions.ActOnTypeParameterDefault(TypeParam, EqualLoc, DefaultLoc, |
| 245 | DefaultType); |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Douglas Gregor | 26236e8 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 248 | return TypeParam; |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /// ParseTemplateTemplateParameter - Handle the parsing of template |
| 252 | /// template parameters. |
| 253 | /// |
| 254 | /// type-parameter: [C++ temp.param] |
| 255 | /// 'template' '<' template-parameter-list '>' 'class' identifier[opt] |
| 256 | /// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 257 | Parser::DeclTy * |
| 258 | Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 259 | assert(Tok.is(tok::kw_template) && "Expected 'template' keyword"); |
| 260 | |
| 261 | // Handle the template <...> part. |
| 262 | SourceLocation TemplateLoc = ConsumeToken(); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 263 | TemplateParameterList TemplateParams; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 264 | SourceLocation LAngleLoc, RAngleLoc; |
Douglas Gregor | 68c6993 | 2009-02-10 19:52:54 +0000 | [diff] [blame] | 265 | { |
| 266 | ParseScope TemplateParmScope(this, Scope::TemplateParamScope); |
| 267 | if(!ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc, |
| 268 | RAngleLoc)) { |
| 269 | return 0; |
| 270 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | // Generate a meaningful error if the user forgot to put class before the |
| 274 | // identifier, comma, or greater. |
| 275 | if(!Tok.is(tok::kw_class)) { |
| 276 | Diag(Tok.getLocation(), diag::err_expected_class_before) |
| 277 | << PP.getSpelling(Tok); |
| 278 | return 0; |
| 279 | } |
| 280 | SourceLocation ClassLoc = ConsumeToken(); |
| 281 | |
| 282 | // Get the identifier, if given. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 283 | SourceLocation NameLoc; |
| 284 | IdentifierInfo* ParamName = 0; |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 285 | if(Tok.is(tok::identifier)) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 286 | ParamName = Tok.getIdentifierInfo(); |
| 287 | NameLoc = ConsumeToken(); |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 288 | } else if(Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::greater)) { |
| 289 | // Unnamed template parameter. Don't have to do anything here, just |
| 290 | // don't consume this token. |
| 291 | } else { |
| 292 | Diag(Tok.getLocation(), diag::err_expected_ident); |
| 293 | return 0; |
| 294 | } |
| 295 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 296 | TemplateParamsTy *ParamList = |
| 297 | Actions.ActOnTemplateParameterList(Depth, SourceLocation(), |
| 298 | TemplateLoc, LAngleLoc, |
| 299 | &TemplateParams[0], |
| 300 | TemplateParams.size(), |
| 301 | RAngleLoc); |
| 302 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 303 | Parser::DeclTy * Param |
| 304 | = Actions.ActOnTemplateTemplateParameter(CurScope, TemplateLoc, |
| 305 | ParamList, ParamName, |
| 306 | NameLoc, Depth, Position); |
| 307 | |
| 308 | // Get the a default value, if given. |
| 309 | if (Tok.is(tok::equal)) { |
| 310 | SourceLocation EqualLoc = ConsumeToken(); |
| 311 | OwningExprResult DefaultExpr = ParseCXXIdExpression(); |
| 312 | if (DefaultExpr.isInvalid()) |
| 313 | return Param; |
| 314 | else if (Param) |
| 315 | Actions.ActOnTemplateTemplateParameterDefault(Param, EqualLoc, |
| 316 | move(DefaultExpr)); |
| 317 | } |
| 318 | |
| 319 | return Param; |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /// ParseNonTypeTemplateParameter - Handle the parsing of non-type |
| 323 | /// template parameters (e.g., in "template<int Size> class array;"). |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 324 | /// |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 325 | /// template-parameter: |
| 326 | /// ... |
| 327 | /// parameter-declaration |
| 328 | /// |
| 329 | /// NOTE: It would be ideal to simply call out to ParseParameterDeclaration(), |
| 330 | /// but that didn't work out to well. Instead, this tries to recrate the basic |
| 331 | /// parsing of parameter declarations, but tries to constrain it for template |
| 332 | /// parameters. |
Douglas Gregor | 26236e8 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 333 | /// FIXME: We need to make a ParseParameterDeclaration that works for |
| 334 | /// non-type template parameters and normal function parameters. |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 335 | Parser::DeclTy * |
| 336 | Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) { |
Douglas Gregor | 26236e8 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 337 | SourceLocation StartLoc = Tok.getLocation(); |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 338 | |
| 339 | // Parse the declaration-specifiers (i.e., the type). |
Douglas Gregor | 26236e8 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 340 | // FIXME: The type should probably be restricted in some way... Not all |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 341 | // declarators (parts of declarators?) are accepted for parameters. |
Douglas Gregor | 26236e8 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 342 | DeclSpec DS; |
| 343 | ParseDeclarationSpecifiers(DS); |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 344 | |
| 345 | // Parse this as a typename. |
Douglas Gregor | 26236e8 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 346 | Declarator ParamDecl(DS, Declarator::TemplateParamContext); |
| 347 | ParseDeclarator(ParamDecl); |
Chris Lattner | 7452c6f | 2009-01-05 01:24:05 +0000 | [diff] [blame] | 348 | if (DS.getTypeSpecType() == DeclSpec::TST_unspecified && !DS.getTypeRep()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 349 | // This probably shouldn't happen - and it's more of a Sema thing, but |
| 350 | // basically we didn't parse the type name because we couldn't associate |
| 351 | // it with an AST node. we should just skip to the comma or greater. |
| 352 | // TODO: This is currently a placeholder for some kind of Sema Error. |
| 353 | Diag(Tok.getLocation(), diag::err_parse_error); |
| 354 | SkipUntil(tok::comma, tok::greater, true, true); |
| 355 | return 0; |
| 356 | } |
| 357 | |
Douglas Gregor | 26236e8 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 358 | // Create the parameter. |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 359 | DeclTy *Param = Actions.ActOnNonTypeTemplateParameter(CurScope, ParamDecl, |
| 360 | Depth, Position); |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 361 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 362 | // If there is a default value, parse it. |
Chris Lattner | 7452c6f | 2009-01-05 01:24:05 +0000 | [diff] [blame] | 363 | if (Tok.is(tok::equal)) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 364 | SourceLocation EqualLoc = ConsumeToken(); |
| 365 | |
| 366 | // C++ [temp.param]p15: |
| 367 | // When parsing a default template-argument for a non-type |
| 368 | // template-parameter, the first non-nested > is taken as the |
| 369 | // end of the template-parameter-list rather than a greater-than |
| 370 | // operator. |
| 371 | GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); |
| 372 | |
| 373 | OwningExprResult DefaultArg = ParseAssignmentExpression(); |
| 374 | if (DefaultArg.isInvalid()) |
| 375 | SkipUntil(tok::comma, tok::greater, true, true); |
| 376 | else if (Param) |
| 377 | Actions.ActOnNonTypeTemplateParameterDefault(Param, EqualLoc, |
| 378 | move(DefaultArg)); |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Douglas Gregor | 26236e8 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 381 | return Param; |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 382 | } |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 383 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 384 | /// \brief Parses a template-id that after the template name has |
| 385 | /// already been parsed. |
| 386 | /// |
| 387 | /// This routine takes care of parsing the enclosed template argument |
| 388 | /// list ('<' template-parameter-list [opt] '>') and placing the |
| 389 | /// results into a form that can be transferred to semantic analysis. |
| 390 | /// |
| 391 | /// \param Template the template declaration produced by isTemplateName |
| 392 | /// |
| 393 | /// \param TemplateNameLoc the source location of the template name |
| 394 | /// |
| 395 | /// \param SS if non-NULL, the nested-name-specifier preceding the |
| 396 | /// template name. |
| 397 | /// |
| 398 | /// \param ConsumeLastToken if true, then we will consume the last |
| 399 | /// token that forms the template-id. Otherwise, we will leave the |
| 400 | /// last token in the stream (e.g., so that it can be replaced with an |
| 401 | /// annotation token). |
| 402 | bool |
| 403 | Parser::ParseTemplateIdAfterTemplateName(DeclTy *Template, |
| 404 | SourceLocation TemplateNameLoc, |
| 405 | const CXXScopeSpec *SS, |
| 406 | bool ConsumeLastToken, |
| 407 | SourceLocation &LAngleLoc, |
| 408 | TemplateArgList &TemplateArgs, |
| 409 | TemplateArgIsTypeList &TemplateArgIsType, |
| 410 | TemplateArgLocationList &TemplateArgLocations, |
| 411 | SourceLocation &RAngleLoc) { |
| 412 | assert(Tok.is(tok::less) && "Must have already parsed the template-name"); |
| 413 | |
| 414 | // Consume the '<'. |
| 415 | LAngleLoc = ConsumeToken(); |
| 416 | |
| 417 | // Parse the optional template-argument-list. |
| 418 | bool Invalid = false; |
| 419 | { |
| 420 | GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); |
| 421 | if (Tok.isNot(tok::greater)) |
| 422 | Invalid = ParseTemplateArgumentList(TemplateArgs, TemplateArgIsType, |
| 423 | TemplateArgLocations); |
| 424 | |
| 425 | if (Invalid) { |
| 426 | // Try to find the closing '>'. |
| 427 | SkipUntil(tok::greater, true, !ConsumeLastToken); |
| 428 | |
| 429 | return true; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | if (Tok.isNot(tok::greater)) |
| 434 | return true; |
| 435 | |
| 436 | // Determine the location of the '>'. Only consume this token if the |
| 437 | // caller asked us to. |
| 438 | RAngleLoc = Tok.getLocation(); |
| 439 | |
| 440 | if (ConsumeLastToken) |
| 441 | ConsumeToken(); |
| 442 | |
| 443 | return false; |
| 444 | } |
| 445 | |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 446 | /// AnnotateTemplateIdToken - The current token is an identifier that |
| 447 | /// refers to the template declaration Template, and is followed by a |
| 448 | /// '<'. Turn this template-id into a template-id annotation token. |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 449 | void Parser::AnnotateTemplateIdToken(DeclTy *Template, TemplateNameKind TNK, |
| 450 | const CXXScopeSpec *SS) { |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 451 | assert(getLang().CPlusPlus && "Can only annotate template-ids in C++"); |
| 452 | assert(Template && Tok.is(tok::identifier) && NextToken().is(tok::less) && |
| 453 | "Parser isn't at the beginning of a template-id"); |
| 454 | |
| 455 | // Consume the template-name. |
| 456 | SourceLocation TemplateNameLoc = ConsumeToken(); |
| 457 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 458 | // Parse the enclosed template argument list. |
| 459 | SourceLocation LAngleLoc, RAngleLoc; |
Douglas Gregor | 5908e9f | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 460 | TemplateArgList TemplateArgs; |
| 461 | TemplateArgIsTypeList TemplateArgIsType; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 462 | TemplateArgLocationList TemplateArgLocations; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 463 | bool Invalid = ParseTemplateIdAfterTemplateName(Template, TemplateNameLoc, |
| 464 | SS, false, LAngleLoc, |
| 465 | TemplateArgs, |
| 466 | TemplateArgIsType, |
| 467 | TemplateArgLocations, |
| 468 | RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 469 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 470 | ASTTemplateArgsPtr TemplateArgsPtr(Actions, &TemplateArgs[0], |
| 471 | &TemplateArgIsType[0], |
| 472 | TemplateArgs.size()); |
Douglas Gregor | f02da89 | 2009-02-09 21:04:56 +0000 | [diff] [blame] | 473 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 474 | if (Invalid) // FIXME: How to recover from a broken template-id? |
| 475 | return; |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 476 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 477 | // Build the annotation token. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 478 | if (TNK == Action::TNK_Class_template) { |
| 479 | Action::TypeResult Type |
| 480 | = Actions.ActOnClassTemplateId(Template, TemplateNameLoc, |
| 481 | LAngleLoc, TemplateArgsPtr, |
| 482 | &TemplateArgLocations[0], |
| 483 | RAngleLoc, SS); |
| 484 | if (Type.isInvalid()) // FIXME: better recovery? |
| 485 | return; |
| 486 | |
| 487 | Tok.setKind(tok::annot_typename); |
| 488 | Tok.setAnnotationValue(Type.get()); |
| 489 | } else { |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 490 | // This is a function template. We'll be building a template-id |
| 491 | // annotation token. |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 492 | Tok.setKind(tok::annot_template_id); |
| 493 | TemplateIdAnnotation *TemplateId |
| 494 | = (TemplateIdAnnotation *)malloc(sizeof(TemplateIdAnnotation) + |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 495 | sizeof(void*) * TemplateArgs.size()); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 496 | TemplateId->TemplateNameLoc = TemplateNameLoc; |
| 497 | TemplateId->Template = Template; |
| 498 | TemplateId->LAngleLoc = LAngleLoc; |
| 499 | TemplateId->NumArgs = TemplateArgs.size(); |
Douglas Gregor | 5908e9f | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 500 | void **Args = (void**)(TemplateId + 1); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 501 | for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg) |
| 502 | Args[Arg] = TemplateArgs[Arg]; |
| 503 | Tok.setAnnotationValue(TemplateId); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | // Common fields for the annotation token |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 507 | Tok.setAnnotationEndLoc(RAngleLoc); |
| 508 | Tok.setLocation(TemplateNameLoc); |
| 509 | if (SS && SS->isNotEmpty()) |
| 510 | Tok.setLocation(SS->getBeginLoc()); |
| 511 | |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 512 | // In case the tokens were cached, have Preprocessor replace them with the |
| 513 | // annotation token. |
| 514 | PP.AnnotateCachedTokens(Tok); |
| 515 | } |
| 516 | |
| 517 | /// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]). |
| 518 | /// |
| 519 | /// template-argument: [C++ 14.2] |
| 520 | /// assignment-expression |
| 521 | /// type-id |
| 522 | /// id-expression |
Douglas Gregor | 5908e9f | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 523 | void *Parser::ParseTemplateArgument(bool &ArgIsType) { |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 524 | // C++ [temp.arg]p2: |
| 525 | // In a template-argument, an ambiguity between a type-id and an |
| 526 | // expression is resolved to a type-id, regardless of the form of |
| 527 | // the corresponding template-parameter. |
| 528 | // |
| 529 | // Therefore, we initially try to parse a type-id. |
Douglas Gregor | 8b64259 | 2009-02-10 00:53:15 +0000 | [diff] [blame] | 530 | if (isCXXTypeId(TypeIdAsTemplateArgument)) { |
Douglas Gregor | 5908e9f | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 531 | ArgIsType = true; |
| 532 | return ParseTypeName(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 535 | OwningExprResult ExprArg = ParseAssignmentExpression(); |
| 536 | if (ExprArg.isInvalid() || !ExprArg.get()) |
Douglas Gregor | 5908e9f | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 537 | return 0; |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 538 | |
Douglas Gregor | 5908e9f | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 539 | ArgIsType = false; |
| 540 | return ExprArg.release(); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /// ParseTemplateArgumentList - Parse a C++ template-argument-list |
| 544 | /// (C++ [temp.names]). Returns true if there was an error. |
| 545 | /// |
| 546 | /// template-argument-list: [C++ 14.2] |
| 547 | /// template-argument |
| 548 | /// template-argument-list ',' template-argument |
Douglas Gregor | 5908e9f | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 549 | bool |
| 550 | Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs, |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 551 | TemplateArgIsTypeList &TemplateArgIsType, |
| 552 | TemplateArgLocationList &TemplateArgLocations) { |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 553 | while (true) { |
Douglas Gregor | 5908e9f | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 554 | bool IsType = false; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 555 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 5908e9f | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 556 | void *Arg = ParseTemplateArgument(IsType); |
| 557 | if (Arg) { |
| 558 | TemplateArgs.push_back(Arg); |
| 559 | TemplateArgIsType.push_back(IsType); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 560 | TemplateArgLocations.push_back(Loc); |
Douglas Gregor | 5908e9f | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 561 | } else { |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 562 | SkipUntil(tok::comma, tok::greater, true, true); |
| 563 | return true; |
| 564 | } |
Douglas Gregor | 5908e9f | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 565 | |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 566 | // If the next token is a comma, consume it and keep reading |
| 567 | // arguments. |
| 568 | if (Tok.isNot(tok::comma)) break; |
| 569 | |
| 570 | // Consume the comma. |
| 571 | ConsumeToken(); |
| 572 | } |
| 573 | |
| 574 | return Tok.isNot(tok::greater); |
| 575 | } |
| 576 | |