Douglas Gregor | b3bec71 | 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 | 545f39e | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 15 | #include "clang/Parse/ParseDiagnostic.h" |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 16 | #include "clang/Parse/DeclSpec.h" |
| 17 | #include "clang/Parse/Scope.h" |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 18 | #include "AstGuard.h" |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace clang; |
| 21 | |
| 22 | /// ParseTemplateDeclaration - Parse a template declaration, which includes |
| 23 | /// the template parameter list and either a function of class declaration. |
| 24 | /// |
| 25 | /// template-declaration: [C++ temp] |
| 26 | /// 'export'[opt] 'template' '<' template-parameter-list '>' declaration |
| 27 | Parser::DeclTy *Parser::ParseTemplateDeclaration(unsigned Context) { |
| 28 | assert((Tok.is(tok::kw_export) || Tok.is(tok::kw_template)) && |
| 29 | "Token does not start a template declaration."); |
| 30 | |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 31 | // Enter template-parameter scope. |
Douglas Gregor | 95d4079 | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 32 | ParseScope TemplateParmScope(this, Scope::TemplateParamScope); |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 33 | |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 34 | // Parse multiple levels of template headers within this template |
| 35 | // parameter scope, e.g., |
| 36 | // |
| 37 | // template<typename T> |
| 38 | // template<typename U> |
| 39 | // class A<T>::B { ... }; |
| 40 | // |
| 41 | // We parse multiple levels non-recursively so that we can build a |
| 42 | // single data structure containing all of the template parameter |
Douglas Gregor | 279272e | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 43 | // lists easily differentiate between the case above and: |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 44 | // |
| 45 | // template<typename T> |
| 46 | // class A { |
| 47 | // template<typename U> class B; |
| 48 | // }; |
| 49 | // |
| 50 | // In the first case, the action for declaring A<T>::B receives |
| 51 | // both template parameter lists. In the second case, the action for |
| 52 | // defining A<T>::B receives just the inner template parameter list |
| 53 | // (and retrieves the outer template parameter list from its |
| 54 | // context). |
| 55 | TemplateParameterLists ParamLists; |
| 56 | do { |
| 57 | // Consume the 'export', if any. |
| 58 | SourceLocation ExportLoc; |
| 59 | if (Tok.is(tok::kw_export)) { |
| 60 | ExportLoc = ConsumeToken(); |
| 61 | } |
| 62 | |
| 63 | // Consume the 'template', which should be here. |
| 64 | SourceLocation TemplateLoc; |
| 65 | if (Tok.is(tok::kw_template)) { |
| 66 | TemplateLoc = ConsumeToken(); |
| 67 | } else { |
| 68 | Diag(Tok.getLocation(), diag::err_expected_template); |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | // Parse the '<' template-parameter-list '>' |
| 73 | SourceLocation LAngleLoc, RAngleLoc; |
| 74 | TemplateParameterList TemplateParams; |
| 75 | ParseTemplateParameters(ParamLists.size(), TemplateParams, LAngleLoc, |
| 76 | RAngleLoc); |
| 77 | |
| 78 | ParamLists.push_back( |
| 79 | Actions.ActOnTemplateParameterList(ParamLists.size(), ExportLoc, |
| 80 | TemplateLoc, LAngleLoc, |
| 81 | &TemplateParams[0], |
| 82 | TemplateParams.size(), RAngleLoc)); |
| 83 | } while (Tok.is(tok::kw_export) || Tok.is(tok::kw_template)); |
| 84 | |
| 85 | // Parse the actual template declaration. |
Douglas Gregor | 279272e | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 86 | return ParseDeclarationOrFunctionDefinition(&ParamLists); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /// ParseTemplateParameters - Parses a template-parameter-list enclosed in |
Douglas Gregor | 279272e | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 90 | /// angle brackets. Depth is the depth of this template-parameter-list, which |
| 91 | /// is the number of template headers directly enclosing this template header. |
| 92 | /// TemplateParams is the current list of template parameters we're building. |
| 93 | /// The template parameter we parse will be added to this list. LAngleLoc and |
| 94 | /// RAngleLoc will receive the positions of the '<' and '>', respectively, |
| 95 | /// that enclose this template parameter list. |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 96 | bool Parser::ParseTemplateParameters(unsigned Depth, |
| 97 | TemplateParameterList &TemplateParams, |
| 98 | SourceLocation &LAngleLoc, |
| 99 | SourceLocation &RAngleLoc) { |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 100 | // Get the template parameter list. |
| 101 | if(!Tok.is(tok::less)) { |
| 102 | Diag(Tok.getLocation(), diag::err_expected_less_after) << "template"; |
| 103 | return false; |
| 104 | } |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 105 | LAngleLoc = ConsumeToken(); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 106 | |
| 107 | // Try to parse the template parameter list. |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 108 | if (Tok.is(tok::greater)) |
| 109 | RAngleLoc = ConsumeToken(); |
| 110 | else if(ParseTemplateParameterList(Depth, TemplateParams)) { |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 111 | if(!Tok.is(tok::greater)) { |
| 112 | Diag(Tok.getLocation(), diag::err_expected_greater); |
| 113 | return false; |
| 114 | } |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 115 | RAngleLoc = ConsumeToken(); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 116 | } |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | /// ParseTemplateParameterList - Parse a template parameter list. If |
| 121 | /// the parsing fails badly (i.e., closing bracket was left out), this |
| 122 | /// will try to put the token stream in a reasonable position (closing |
| 123 | /// a statement, etc.) and return false. |
| 124 | /// |
| 125 | /// template-parameter-list: [C++ temp] |
| 126 | /// template-parameter |
| 127 | /// template-parameter-list ',' template-parameter |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 128 | bool |
| 129 | Parser::ParseTemplateParameterList(unsigned Depth, |
| 130 | TemplateParameterList &TemplateParams) { |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 131 | while(1) { |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 132 | if (DeclTy* TmpParam |
| 133 | = ParseTemplateParameter(Depth, TemplateParams.size())) { |
| 134 | TemplateParams.push_back(TmpParam); |
| 135 | } else { |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 136 | // If we failed to parse a template parameter, skip until we find |
| 137 | // a comma or closing brace. |
| 138 | SkipUntil(tok::comma, tok::greater, true, true); |
| 139 | } |
| 140 | |
| 141 | // Did we find a comma or the end of the template parmeter list? |
| 142 | if(Tok.is(tok::comma)) { |
| 143 | ConsumeToken(); |
| 144 | } else if(Tok.is(tok::greater)) { |
| 145 | // Don't consume this... that's done by template parser. |
| 146 | break; |
| 147 | } else { |
| 148 | // Somebody probably forgot to close the template. Skip ahead and |
| 149 | // try to get out of the expression. This error is currently |
| 150 | // subsumed by whatever goes on in ParseTemplateParameter. |
| 151 | // TODO: This could match >>, and it would be nice to avoid those |
| 152 | // silly errors with template <vec<T>>. |
| 153 | // Diag(Tok.getLocation(), diag::err_expected_comma_greater); |
| 154 | SkipUntil(tok::greater, true, true); |
| 155 | return false; |
| 156 | } |
| 157 | } |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | /// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]). |
| 162 | /// |
| 163 | /// template-parameter: [C++ temp.param] |
| 164 | /// type-parameter |
| 165 | /// parameter-declaration |
| 166 | /// |
| 167 | /// type-parameter: (see below) |
| 168 | /// 'class' identifier[opt] |
| 169 | /// 'class' identifier[opt] '=' type-id |
| 170 | /// 'typename' identifier[opt] |
| 171 | /// 'typename' identifier[opt] '=' type-id |
| 172 | /// 'template' '<' template-parameter-list '>' 'class' identifier[opt] |
| 173 | /// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 174 | Parser::DeclTy * |
| 175 | Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) { |
Chris Lattner | 4e7a420 | 2009-01-04 23:51:17 +0000 | [diff] [blame] | 176 | if(Tok.is(tok::kw_class) || |
| 177 | (Tok.is(tok::kw_typename) && |
| 178 | // FIXME: Next token has not been annotated! |
Chris Lattner | 5d7eace | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 179 | NextToken().isNot(tok::annot_typename))) { |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 180 | return ParseTypeParameter(Depth, Position); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 181 | } |
Chris Lattner | 4e7a420 | 2009-01-04 23:51:17 +0000 | [diff] [blame] | 182 | |
| 183 | if(Tok.is(tok::kw_template)) |
| 184 | return ParseTemplateTemplateParameter(Depth, Position); |
| 185 | |
| 186 | // If it's none of the above, then it must be a parameter declaration. |
| 187 | // NOTE: This will pick up errors in the closure of the template parameter |
| 188 | // list (e.g., template < ; Check here to implement >> style closures. |
| 189 | return ParseNonTypeTemplateParameter(Depth, Position); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]). |
| 193 | /// Other kinds of template parameters are parsed in |
| 194 | /// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter. |
| 195 | /// |
| 196 | /// type-parameter: [C++ temp.param] |
| 197 | /// 'class' identifier[opt] |
| 198 | /// 'class' identifier[opt] '=' type-id |
| 199 | /// 'typename' identifier[opt] |
| 200 | /// 'typename' identifier[opt] '=' type-id |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 201 | Parser::DeclTy *Parser::ParseTypeParameter(unsigned Depth, unsigned Position) { |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 202 | assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) && |
| 203 | "A type-parameter starts with 'class' or 'typename'"); |
| 204 | |
| 205 | // Consume the 'class' or 'typename' keyword. |
| 206 | bool TypenameKeyword = Tok.is(tok::kw_typename); |
| 207 | SourceLocation KeyLoc = ConsumeToken(); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 208 | |
| 209 | // Grab the template parameter name (if given) |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 210 | SourceLocation NameLoc; |
| 211 | IdentifierInfo* ParamName = 0; |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 212 | if(Tok.is(tok::identifier)) { |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 213 | ParamName = Tok.getIdentifierInfo(); |
| 214 | NameLoc = ConsumeToken(); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 215 | } else if(Tok.is(tok::equal) || Tok.is(tok::comma) || |
| 216 | Tok.is(tok::greater)) { |
| 217 | // Unnamed template parameter. Don't have to do anything here, just |
| 218 | // don't consume this token. |
| 219 | } else { |
| 220 | Diag(Tok.getLocation(), diag::err_expected_ident); |
| 221 | return 0; |
| 222 | } |
| 223 | |
Douglas Gregor | 279272e | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 224 | DeclTy *TypeParam = Actions.ActOnTypeParameter(CurScope, TypenameKeyword, |
| 225 | KeyLoc, ParamName, NameLoc, |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 226 | Depth, Position); |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 227 | |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 228 | // Grab a default type id (if given). |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 229 | if(Tok.is(tok::equal)) { |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 230 | SourceLocation EqualLoc = ConsumeToken(); |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 231 | if (TypeTy *DefaultType = ParseTypeName()) |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 232 | Actions.ActOnTypeParameterDefault(TypeParam, DefaultType); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 235 | return TypeParam; |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /// ParseTemplateTemplateParameter - Handle the parsing of template |
| 239 | /// template parameters. |
| 240 | /// |
| 241 | /// type-parameter: [C++ temp.param] |
| 242 | /// 'template' '<' template-parameter-list '>' 'class' identifier[opt] |
| 243 | /// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 244 | Parser::DeclTy * |
| 245 | Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) { |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 246 | assert(Tok.is(tok::kw_template) && "Expected 'template' keyword"); |
| 247 | |
| 248 | // Handle the template <...> part. |
| 249 | SourceLocation TemplateLoc = ConsumeToken(); |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 250 | TemplateParameterList TemplateParams; |
Douglas Gregor | d406b03 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 251 | SourceLocation LAngleLoc, RAngleLoc; |
| 252 | if(!ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc, |
| 253 | RAngleLoc)) { |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | // Generate a meaningful error if the user forgot to put class before the |
| 258 | // identifier, comma, or greater. |
| 259 | if(!Tok.is(tok::kw_class)) { |
| 260 | Diag(Tok.getLocation(), diag::err_expected_class_before) |
| 261 | << PP.getSpelling(Tok); |
| 262 | return 0; |
| 263 | } |
| 264 | SourceLocation ClassLoc = ConsumeToken(); |
| 265 | |
| 266 | // Get the identifier, if given. |
Douglas Gregor | 279272e | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 267 | SourceLocation NameLoc; |
| 268 | IdentifierInfo* ParamName = 0; |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 269 | if(Tok.is(tok::identifier)) { |
Douglas Gregor | 279272e | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 270 | ParamName = Tok.getIdentifierInfo(); |
| 271 | NameLoc = ConsumeToken(); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 272 | } else if(Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::greater)) { |
| 273 | // Unnamed template parameter. Don't have to do anything here, just |
| 274 | // don't consume this token. |
| 275 | } else { |
| 276 | Diag(Tok.getLocation(), diag::err_expected_ident); |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | // Get the a default value, if given. |
Douglas Gregor | 279272e | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 281 | // FIXME: I think that the results of this block need to be passed to the |
| 282 | // act-on call, so we can assemble the parameter correctly. |
Sebastian Redl | 6226104 | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 283 | OwningExprResult DefaultExpr(Actions); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 284 | if(Tok.is(tok::equal)) { |
| 285 | ConsumeToken(); |
Sebastian Redl | bb4dae7 | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 286 | DefaultExpr = ParseCXXIdExpression(); |
| 287 | if(DefaultExpr.isInvalid()) { |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 288 | return 0; |
| 289 | } |
| 290 | } |
| 291 | |
Douglas Gregor | d406b03 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 292 | TemplateParamsTy *ParamList = |
| 293 | Actions.ActOnTemplateParameterList(Depth, SourceLocation(), |
| 294 | TemplateLoc, LAngleLoc, |
| 295 | &TemplateParams[0], |
| 296 | TemplateParams.size(), |
| 297 | RAngleLoc); |
| 298 | |
Douglas Gregor | 279272e | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 299 | return Actions.ActOnTemplateTemplateParameter(CurScope, TemplateLoc, |
Douglas Gregor | d406b03 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 300 | ParamList, ParamName, |
Douglas Gregor | 279272e | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 301 | NameLoc, Depth, Position); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /// ParseNonTypeTemplateParameter - Handle the parsing of non-type |
| 305 | /// template parameters (e.g., in "template<int Size> class array;"). |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 306 | /// |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 307 | /// template-parameter: |
| 308 | /// ... |
| 309 | /// parameter-declaration |
| 310 | /// |
| 311 | /// NOTE: It would be ideal to simply call out to ParseParameterDeclaration(), |
| 312 | /// but that didn't work out to well. Instead, this tries to recrate the basic |
| 313 | /// parsing of parameter declarations, but tries to constrain it for template |
| 314 | /// parameters. |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 315 | /// FIXME: We need to make a ParseParameterDeclaration that works for |
| 316 | /// non-type template parameters and normal function parameters. |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 317 | Parser::DeclTy * |
| 318 | Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) { |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 319 | SourceLocation StartLoc = Tok.getLocation(); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 320 | |
| 321 | // Parse the declaration-specifiers (i.e., the type). |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 322 | // FIXME: The type should probably be restricted in some way... Not all |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 323 | // declarators (parts of declarators?) are accepted for parameters. |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 324 | DeclSpec DS; |
| 325 | ParseDeclarationSpecifiers(DS); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 326 | |
| 327 | // Parse this as a typename. |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 328 | Declarator ParamDecl(DS, Declarator::TemplateParamContext); |
| 329 | ParseDeclarator(ParamDecl); |
Chris Lattner | 8376d2e | 2009-01-05 01:24:05 +0000 | [diff] [blame] | 330 | if (DS.getTypeSpecType() == DeclSpec::TST_unspecified && !DS.getTypeRep()) { |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 331 | // This probably shouldn't happen - and it's more of a Sema thing, but |
| 332 | // basically we didn't parse the type name because we couldn't associate |
| 333 | // it with an AST node. we should just skip to the comma or greater. |
| 334 | // TODO: This is currently a placeholder for some kind of Sema Error. |
| 335 | Diag(Tok.getLocation(), diag::err_parse_error); |
| 336 | SkipUntil(tok::comma, tok::greater, true, true); |
| 337 | return 0; |
| 338 | } |
| 339 | |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 340 | // Create the parameter. |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 341 | DeclTy *Param = Actions.ActOnNonTypeTemplateParameter(CurScope, ParamDecl, |
| 342 | Depth, Position); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 343 | |
| 344 | // Is there a default value? Parsing this can be fairly annoying because |
| 345 | // we have to stop on the first non-nested (paren'd) '>' as the closure |
| 346 | // for the template parameter list. Or a ','. |
Chris Lattner | 8376d2e | 2009-01-05 01:24:05 +0000 | [diff] [blame] | 347 | if (Tok.is(tok::equal)) { |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 348 | // TODO: Implement default non-type values. |
| 349 | SkipUntil(tok::comma, tok::greater, true, true); |
| 350 | } |
| 351 | |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 352 | return Param; |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 353 | } |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 354 | |
| 355 | /// AnnotateTemplateIdToken - The current token is an identifier that |
| 356 | /// refers to the template declaration Template, and is followed by a |
| 357 | /// '<'. Turn this template-id into a template-id annotation token. |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 358 | void Parser::AnnotateTemplateIdToken(DeclTy *Template, TemplateNameKind TNK, |
| 359 | const CXXScopeSpec *SS) { |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 360 | assert(getLang().CPlusPlus && "Can only annotate template-ids in C++"); |
| 361 | assert(Template && Tok.is(tok::identifier) && NextToken().is(tok::less) && |
| 362 | "Parser isn't at the beginning of a template-id"); |
| 363 | |
| 364 | // Consume the template-name. |
| 365 | SourceLocation TemplateNameLoc = ConsumeToken(); |
| 366 | |
| 367 | // Consume the '<'. |
| 368 | SourceLocation LAngleLoc = ConsumeToken(); |
| 369 | |
| 370 | // Parse the optional template-argument-list. |
Douglas Gregor | 6f37b58 | 2009-02-09 19:34:22 +0000 | [diff] [blame^] | 371 | TemplateArgList TemplateArgs; |
| 372 | TemplateArgIsTypeList TemplateArgIsType; |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 373 | { |
| 374 | MakeGreaterThanTemplateArgumentListTerminator G(GreaterThanIsOperator); |
Douglas Gregor | 6f37b58 | 2009-02-09 19:34:22 +0000 | [diff] [blame^] | 375 | if (Tok.isNot(tok::greater) && |
| 376 | ParseTemplateArgumentList(TemplateArgs, TemplateArgIsType)) { |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 377 | // Try to find the closing '>'. |
| 378 | SkipUntil(tok::greater, true, true); |
| 379 | |
| 380 | // FIXME: What's our recovery strategy for failed template-argument-lists? |
| 381 | return; |
| 382 | } |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | if (Tok.isNot(tok::greater)) |
| 386 | return; |
| 387 | |
| 388 | // Determine the location of the '>'. We won't actually consume this |
| 389 | // token, because we'll be replacing it with the template-id. |
| 390 | SourceLocation RAngleLoc = Tok.getLocation(); |
| 391 | |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 392 | // Build the annotation token. |
| 393 | if (TNK == Action::TNK_Function_template) { |
| 394 | // This is a function template. We'll be building a template-id |
| 395 | // annotation token. |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 396 | Tok.setKind(tok::annot_template_id); |
| 397 | TemplateIdAnnotation *TemplateId |
| 398 | = (TemplateIdAnnotation *)malloc(sizeof(TemplateIdAnnotation) + |
Douglas Gregor | 6f37b58 | 2009-02-09 19:34:22 +0000 | [diff] [blame^] | 399 | sizeof(void*) * TemplateArgs.size()); |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 400 | TemplateId->TemplateNameLoc = TemplateNameLoc; |
| 401 | TemplateId->Template = Template; |
| 402 | TemplateId->LAngleLoc = LAngleLoc; |
| 403 | TemplateId->NumArgs = TemplateArgs.size(); |
Douglas Gregor | 6f37b58 | 2009-02-09 19:34:22 +0000 | [diff] [blame^] | 404 | void **Args = (void**)(TemplateId + 1); |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 405 | for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg) |
| 406 | Args[Arg] = TemplateArgs[Arg]; |
| 407 | Tok.setAnnotationValue(TemplateId); |
| 408 | } else { |
| 409 | // This is a type template, e.g., a class template, template |
| 410 | // template parameter, or template alias. We'll be building a |
| 411 | // "typename" annotation token. |
Douglas Gregor | 6f37b58 | 2009-02-09 19:34:22 +0000 | [diff] [blame^] | 412 | ASTTemplateArgsPtr TemplateArgsPtr(Actions, &TemplateArgs[0], |
| 413 | &TemplateArgIsType[0], |
| 414 | TemplateArgs.size()); |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 415 | TypeTy *Ty |
Douglas Gregor | 6f37b58 | 2009-02-09 19:34:22 +0000 | [diff] [blame^] | 416 | = Actions.ActOnClassTemplateSpecialization(Template, LAngleLoc, |
| 417 | TemplateArgsPtr, |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 418 | RAngleLoc, SS); |
| 419 | Tok.setKind(tok::annot_typename); |
| 420 | Tok.setAnnotationValue(Ty); |
| 421 | } |
| 422 | |
| 423 | // Common fields for the annotation token |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 424 | Tok.setAnnotationEndLoc(RAngleLoc); |
| 425 | Tok.setLocation(TemplateNameLoc); |
| 426 | if (SS && SS->isNotEmpty()) |
| 427 | Tok.setLocation(SS->getBeginLoc()); |
| 428 | |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 429 | // In case the tokens were cached, have Preprocessor replace them with the |
| 430 | // annotation token. |
| 431 | PP.AnnotateCachedTokens(Tok); |
| 432 | } |
| 433 | |
| 434 | /// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]). |
| 435 | /// |
| 436 | /// template-argument: [C++ 14.2] |
| 437 | /// assignment-expression |
| 438 | /// type-id |
| 439 | /// id-expression |
Douglas Gregor | 6f37b58 | 2009-02-09 19:34:22 +0000 | [diff] [blame^] | 440 | void *Parser::ParseTemplateArgument(bool &ArgIsType) { |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 441 | // C++ [temp.arg]p2: |
| 442 | // In a template-argument, an ambiguity between a type-id and an |
| 443 | // expression is resolved to a type-id, regardless of the form of |
| 444 | // the corresponding template-parameter. |
| 445 | // |
| 446 | // Therefore, we initially try to parse a type-id. |
| 447 | if (isTypeIdInParens()) { |
Douglas Gregor | 6f37b58 | 2009-02-09 19:34:22 +0000 | [diff] [blame^] | 448 | ArgIsType = true; |
| 449 | return ParseTypeName(); |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | OwningExprResult ExprArg = ParseExpression(); |
| 453 | if (ExprArg.isInvalid()) |
Douglas Gregor | 6f37b58 | 2009-02-09 19:34:22 +0000 | [diff] [blame^] | 454 | return 0; |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 455 | |
Douglas Gregor | 6f37b58 | 2009-02-09 19:34:22 +0000 | [diff] [blame^] | 456 | ArgIsType = false; |
| 457 | return ExprArg.release(); |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /// ParseTemplateArgumentList - Parse a C++ template-argument-list |
| 461 | /// (C++ [temp.names]). Returns true if there was an error. |
| 462 | /// |
| 463 | /// template-argument-list: [C++ 14.2] |
| 464 | /// template-argument |
| 465 | /// template-argument-list ',' template-argument |
Douglas Gregor | 6f37b58 | 2009-02-09 19:34:22 +0000 | [diff] [blame^] | 466 | bool |
| 467 | Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs, |
| 468 | TemplateArgIsTypeList &TemplateArgIsType) { |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 469 | while (true) { |
Douglas Gregor | 6f37b58 | 2009-02-09 19:34:22 +0000 | [diff] [blame^] | 470 | bool IsType = false; |
| 471 | void *Arg = ParseTemplateArgument(IsType); |
| 472 | if (Arg) { |
| 473 | TemplateArgs.push_back(Arg); |
| 474 | TemplateArgIsType.push_back(IsType); |
| 475 | } else { |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 476 | SkipUntil(tok::comma, tok::greater, true, true); |
| 477 | return true; |
| 478 | } |
Douglas Gregor | 6f37b58 | 2009-02-09 19:34:22 +0000 | [diff] [blame^] | 479 | |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 480 | // If the next token is a comma, consume it and keep reading |
| 481 | // arguments. |
| 482 | if (Tok.isNot(tok::comma)) break; |
| 483 | |
| 484 | // Consume the comma. |
| 485 | ConsumeToken(); |
| 486 | } |
| 487 | |
| 488 | return Tok.isNot(tok::greater); |
| 489 | } |
| 490 | |