2006-11-05 15:46:30 +08:00
|
|
|
//===--- ParseInit.cpp - Initializer Parsing ------------------------------===//
|
2006-08-14 05:54:02 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-08-14 05:54:02 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements initializer parsing as specified by C99 6.7.8.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-10-27 06:36:07 +08:00
|
|
|
#include "clang/Parse/Designator.h"
|
2006-08-14 05:54:02 +08:00
|
|
|
#include "clang/Parse/Parser.h"
|
2008-11-26 06:21:31 +08:00
|
|
|
#include "AstGuard.h"
|
2009-01-29 13:15:15 +08:00
|
|
|
#include "clang/Parse/ParseDiagnostic.h"
|
2007-07-19 09:06:55 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2006-08-14 05:54:02 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
|
|
|
|
/// MayBeDesignationStart - Return true if this token might be the start of a
|
2008-10-27 05:46:13 +08:00
|
|
|
/// designator. If we can tell it is impossible that it is a designator, return
|
|
|
|
/// false.
|
2008-10-27 06:41:58 +08:00
|
|
|
static bool MayBeDesignationStart(tok::TokenKind K, Preprocessor &PP) {
|
2006-08-14 05:54:02 +08:00
|
|
|
switch (K) {
|
|
|
|
default: return false;
|
|
|
|
case tok::period: // designator: '.' identifier
|
|
|
|
case tok::l_square: // designator: array-designator
|
2008-10-27 06:41:58 +08:00
|
|
|
return true;
|
2006-08-14 05:54:02 +08:00
|
|
|
case tok::identifier: // designation: identifier ':'
|
2008-10-27 06:41:58 +08:00
|
|
|
return PP.LookAhead(0).is(tok::colon);
|
2006-08-14 05:54:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-14 05:54:51 +08:00
|
|
|
/// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
|
|
|
|
/// checking to see if the token stream starts with a designator.
|
|
|
|
///
|
2006-08-14 05:54:02 +08:00
|
|
|
/// designation:
|
|
|
|
/// designator-list '='
|
|
|
|
/// [GNU] array-designator
|
|
|
|
/// [GNU] identifier ':'
|
|
|
|
///
|
|
|
|
/// designator-list:
|
|
|
|
/// designator
|
|
|
|
/// designator-list designator
|
|
|
|
///
|
|
|
|
/// designator:
|
|
|
|
/// array-designator
|
|
|
|
/// '.' identifier
|
|
|
|
///
|
|
|
|
/// array-designator:
|
|
|
|
/// '[' constant-expression ']'
|
|
|
|
/// [GNU] '[' constant-expression '...' constant-expression ']'
|
|
|
|
///
|
|
|
|
/// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
|
2008-10-27 05:46:13 +08:00
|
|
|
/// initializer (because it is an expression). We need to consider this case
|
|
|
|
/// when parsing array designators.
|
2006-08-14 05:54:02 +08:00
|
|
|
///
|
2009-03-21 07:11:49 +08:00
|
|
|
Parser::OwningExprResult Parser::ParseInitializerWithPotentialDesignator() {
|
2008-12-12 06:51:44 +08:00
|
|
|
|
2008-10-27 06:36:07 +08:00
|
|
|
// If this is the old-style GNU extension:
|
|
|
|
// designation ::= identifier ':'
|
|
|
|
// Handle it as a field designator. Otherwise, this must be the start of a
|
|
|
|
// normal expression.
|
|
|
|
if (Tok.is(tok::identifier)) {
|
2008-10-27 06:41:58 +08:00
|
|
|
Diag(Tok, diag::ext_gnu_old_style_field_designator);
|
|
|
|
|
2009-01-22 08:58:24 +08:00
|
|
|
const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
|
|
|
|
SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
|
2008-10-27 06:36:07 +08:00
|
|
|
|
2008-10-27 06:49:49 +08:00
|
|
|
assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
|
2009-01-22 08:58:24 +08:00
|
|
|
SourceLocation ColonLoc = ConsumeToken();
|
|
|
|
|
2009-03-21 07:11:49 +08:00
|
|
|
Designation D;
|
2009-01-22 08:58:24 +08:00
|
|
|
D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
|
|
|
|
return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
|
|
|
|
ParseInitializer());
|
2008-10-27 06:36:07 +08:00
|
|
|
}
|
|
|
|
|
2008-10-27 06:59:19 +08:00
|
|
|
// Desig - This is initialized when we see our first designator. We may have
|
|
|
|
// an objc message send with no designator, so we don't want to create this
|
|
|
|
// eagerly.
|
2009-03-21 07:11:49 +08:00
|
|
|
Designation Desig;
|
2008-10-27 06:59:19 +08:00
|
|
|
|
2006-08-14 05:54:02 +08:00
|
|
|
// Parse each designator in the designator list until we find an initializer.
|
2008-10-27 06:49:49 +08:00
|
|
|
while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
|
|
|
|
if (Tok.is(tok::period)) {
|
2006-08-14 05:54:02 +08:00
|
|
|
// designator: '.' identifier
|
2009-01-22 08:58:24 +08:00
|
|
|
SourceLocation DotLoc = ConsumeToken();
|
2008-10-27 06:59:19 +08:00
|
|
|
|
|
|
|
if (Tok.isNot(tok::identifier)) {
|
|
|
|
Diag(Tok.getLocation(), diag::err_expected_field_designator);
|
2008-12-12 06:51:44 +08:00
|
|
|
return ExprError();
|
2008-10-27 06:59:19 +08:00
|
|
|
}
|
|
|
|
|
2009-03-21 07:11:49 +08:00
|
|
|
Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
|
|
|
|
Tok.getLocation()));
|
2008-10-27 06:59:19 +08:00
|
|
|
ConsumeToken(); // Eat the identifier.
|
2008-10-27 06:49:49 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We must have either an array designator now or an objc message send.
|
|
|
|
assert(Tok.is(tok::l_square) && "Unexpected token!");
|
|
|
|
|
2008-10-27 07:06:54 +08:00
|
|
|
// Handle the two forms of array designator:
|
|
|
|
// array-designator: '[' constant-expression ']'
|
|
|
|
// array-designator: '[' constant-expression '...' constant-expression ']'
|
|
|
|
//
|
|
|
|
// Also, we have to handle the case where the expression after the
|
|
|
|
// designator an an objc message send: '[' objc-message-expr ']'.
|
|
|
|
// Interesting cases are:
|
|
|
|
// [foo bar] -> objc message send
|
|
|
|
// [foo] -> array designator
|
|
|
|
// [foo ... bar] -> array designator
|
|
|
|
// [4][foo bar] -> obsolete GNU designation with objc message send.
|
|
|
|
//
|
2008-10-27 06:49:49 +08:00
|
|
|
SourceLocation StartLoc = ConsumeBracket();
|
|
|
|
|
|
|
|
// If Objective-C is enabled and this is a typename or other identifier
|
|
|
|
// receiver, parse this as a message send expression.
|
|
|
|
if (getLang().ObjC1 && isTokObjCMessageIdentifierReceiver()) {
|
2008-10-27 07:29:41 +08:00
|
|
|
// If we have exactly one array designator, this used the GNU
|
|
|
|
// 'designation: array-designator' extension, otherwise there should be no
|
|
|
|
// designators at all!
|
2009-03-21 07:11:49 +08:00
|
|
|
if (Desig.getNumDesignators() == 1 &&
|
|
|
|
(Desig.getDesignator(0).isArrayDesignator() ||
|
|
|
|
Desig.getDesignator(0).isArrayRangeDesignator()))
|
|
|
|
Diag(StartLoc, diag::ext_gnu_missing_equal_designator);
|
|
|
|
else if (Desig.getNumDesignators() > 0)
|
|
|
|
Diag(Tok, diag::err_expected_equal_designator);
|
2008-12-13 23:32:12 +08:00
|
|
|
|
2008-10-27 06:49:49 +08:00
|
|
|
IdentifierInfo *Name = Tok.getIdentifierInfo();
|
2008-11-19 23:54:23 +08:00
|
|
|
SourceLocation NameLoc = ConsumeToken();
|
2008-12-13 23:32:12 +08:00
|
|
|
return ParseAssignmentExprWithObjCMessageExprStart(
|
|
|
|
StartLoc, NameLoc, Name, ExprArg(Actions));
|
2008-10-27 06:49:49 +08:00
|
|
|
}
|
2008-12-13 23:32:12 +08:00
|
|
|
|
2008-10-27 06:49:49 +08:00
|
|
|
// Note that we parse this as an assignment expression, not a constant
|
|
|
|
// expression (allowing *=, =, etc) to handle the objc case. Sema needs
|
|
|
|
// to validate that the expression is a constant.
|
2008-12-12 05:36:32 +08:00
|
|
|
OwningExprResult Idx(ParseAssignmentExpression());
|
2008-12-09 21:15:23 +08:00
|
|
|
if (Idx.isInvalid()) {
|
2008-10-27 06:49:49 +08:00
|
|
|
SkipUntil(tok::r_square);
|
2008-12-12 06:51:44 +08:00
|
|
|
return move(Idx);
|
2008-10-27 06:49:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Given an expression, we could either have a designator (if the next
|
|
|
|
// tokens are '...' or ']' or an objc message send. If this is an objc
|
|
|
|
// message send, handle it now. An objc-message send is the start of
|
|
|
|
// an assignment-expression production.
|
|
|
|
if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) &&
|
|
|
|
Tok.isNot(tok::r_square)) {
|
2008-10-27 07:29:41 +08:00
|
|
|
|
|
|
|
// If we have exactly one array designator, this used the GNU
|
|
|
|
// 'designation: array-designator' extension, otherwise there should be no
|
|
|
|
// designators at all!
|
2009-03-21 07:11:49 +08:00
|
|
|
if (Desig.getNumDesignators() == 1 &&
|
|
|
|
(Desig.getDesignator(0).isArrayDesignator() ||
|
|
|
|
Desig.getDesignator(0).isArrayRangeDesignator()))
|
|
|
|
Diag(StartLoc, diag::ext_gnu_missing_equal_designator);
|
|
|
|
else if (Desig.getNumDesignators() > 0)
|
|
|
|
Diag(Tok, diag::err_expected_equal_designator);
|
2008-12-12 06:51:44 +08:00
|
|
|
|
2008-12-13 23:32:12 +08:00
|
|
|
return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
|
|
|
|
SourceLocation(),
|
2009-02-05 23:02:23 +08:00
|
|
|
0, move(Idx));
|
2008-10-27 06:49:49 +08:00
|
|
|
}
|
2008-10-27 07:06:54 +08:00
|
|
|
|
|
|
|
// If this is a normal array designator, remember it.
|
|
|
|
if (Tok.isNot(tok::ellipsis)) {
|
2009-03-21 07:11:49 +08:00
|
|
|
Desig.AddDesignator(Designator::getArray(Idx.release(), StartLoc));
|
2008-10-27 07:06:54 +08:00
|
|
|
} else {
|
|
|
|
// Handle the gnu array range extension.
|
2008-10-27 06:49:49 +08:00
|
|
|
Diag(Tok, diag::ext_gnu_array_range);
|
2009-01-22 08:58:24 +08:00
|
|
|
SourceLocation EllipsisLoc = ConsumeToken();
|
2008-12-12 05:36:32 +08:00
|
|
|
|
|
|
|
OwningExprResult RHS(ParseConstantExpression());
|
2008-12-09 21:15:23 +08:00
|
|
|
if (RHS.isInvalid()) {
|
2006-08-14 05:54:02 +08:00
|
|
|
SkipUntil(tok::r_square);
|
2008-12-12 06:51:44 +08:00
|
|
|
return move(RHS);
|
2008-01-26 03:43:26 +08:00
|
|
|
}
|
2009-03-21 07:11:49 +08:00
|
|
|
Desig.AddDesignator(Designator::getArrayRange(Idx.release(),
|
|
|
|
RHS.release(),
|
|
|
|
StartLoc, EllipsisLoc));
|
2006-08-14 05:54:02 +08:00
|
|
|
}
|
2008-12-09 21:15:23 +08:00
|
|
|
|
2009-01-22 08:58:24 +08:00
|
|
|
SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
|
2009-03-21 07:11:49 +08:00
|
|
|
Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(EndLoc);
|
2006-08-14 05:54:02 +08:00
|
|
|
}
|
2008-10-27 06:49:49 +08:00
|
|
|
|
2008-10-27 06:59:19 +08:00
|
|
|
// Okay, we're done with the designator sequence. We know that there must be
|
|
|
|
// at least one designator, because the only case we can get into this method
|
|
|
|
// without a designator is when we have an objc message send. That case is
|
|
|
|
// handled and returned from above.
|
2009-03-21 07:11:49 +08:00
|
|
|
assert(!Desig.empty() && "Designator is empty?");
|
2008-12-12 06:51:44 +08:00
|
|
|
|
2008-10-27 06:59:19 +08:00
|
|
|
// Handle a normal designator sequence end, which is an equal.
|
2008-10-27 06:49:49 +08:00
|
|
|
if (Tok.is(tok::equal)) {
|
2009-01-22 08:58:24 +08:00
|
|
|
SourceLocation EqualLoc = ConsumeToken();
|
2009-03-21 07:11:49 +08:00
|
|
|
return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
|
2009-01-22 08:58:24 +08:00
|
|
|
ParseInitializer());
|
2008-10-27 06:49:49 +08:00
|
|
|
}
|
2008-12-12 06:51:44 +08:00
|
|
|
|
2008-10-27 06:59:19 +08:00
|
|
|
// We read some number of designators and found something that isn't an = or
|
2008-10-27 07:22:23 +08:00
|
|
|
// an initializer. If we have exactly one array designator, this
|
2008-10-27 06:59:19 +08:00
|
|
|
// is the GNU 'designation: array-designator' extension. Otherwise, it is a
|
|
|
|
// parse error.
|
2009-03-21 07:11:49 +08:00
|
|
|
if (Desig.getNumDesignators() == 1 &&
|
|
|
|
(Desig.getDesignator(0).isArrayDesignator() ||
|
|
|
|
Desig.getDesignator(0).isArrayRangeDesignator())) {
|
2008-10-27 07:22:23 +08:00
|
|
|
Diag(Tok, diag::ext_gnu_missing_equal_designator);
|
|
|
|
return ParseInitializer();
|
|
|
|
}
|
2008-12-12 06:51:44 +08:00
|
|
|
|
2008-10-27 07:22:23 +08:00
|
|
|
Diag(Tok, diag::err_expected_equal_designator);
|
2008-12-12 06:51:44 +08:00
|
|
|
return ExprError();
|
2006-08-14 05:54:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-27 06:38:55 +08:00
|
|
|
/// ParseBraceInitializer - Called when parsing an initializer that has a
|
|
|
|
/// leading open brace.
|
|
|
|
///
|
2006-08-14 05:54:02 +08:00
|
|
|
/// initializer: [C99 6.7.8]
|
|
|
|
/// '{' initializer-list '}'
|
|
|
|
/// '{' initializer-list ',' '}'
|
|
|
|
/// [GNU] '{' '}'
|
|
|
|
///
|
|
|
|
/// initializer-list:
|
|
|
|
/// designation[opt] initializer
|
|
|
|
/// initializer-list ',' designation[opt] initializer
|
|
|
|
///
|
2008-12-12 06:51:44 +08:00
|
|
|
Parser::OwningExprResult Parser::ParseBraceInitializer() {
|
2006-10-16 14:12:55 +08:00
|
|
|
SourceLocation LBraceLoc = ConsumeBrace();
|
2008-11-26 06:21:31 +08:00
|
|
|
|
2008-10-27 06:36:07 +08:00
|
|
|
/// InitExprs - This is the actual list of expressions contained in the
|
|
|
|
/// initializer.
|
2008-11-26 06:21:31 +08:00
|
|
|
ExprVector InitExprs(Actions);
|
|
|
|
|
2008-10-27 07:35:51 +08:00
|
|
|
if (Tok.is(tok::r_brace)) {
|
2009-01-31 06:09:00 +08:00
|
|
|
// Empty initializers are a C++ feature and a GNU extension to C.
|
|
|
|
if (!getLang().CPlusPlus)
|
|
|
|
Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
|
2008-10-27 07:35:51 +08:00
|
|
|
// Match the '}'.
|
2009-01-20 06:31:54 +08:00
|
|
|
return Actions.ActOnInitList(LBraceLoc, Action::MultiExprArg(Actions),
|
2009-03-21 07:11:49 +08:00
|
|
|
ConsumeBrace());
|
2008-10-27 07:35:51 +08:00
|
|
|
}
|
2008-12-12 06:51:44 +08:00
|
|
|
|
2007-07-19 09:06:55 +08:00
|
|
|
bool InitExprsOk = true;
|
2008-12-12 06:51:44 +08:00
|
|
|
|
2007-07-19 09:06:55 +08:00
|
|
|
while (1) {
|
|
|
|
// Parse: designation[opt] initializer
|
2008-12-12 06:51:44 +08:00
|
|
|
|
2007-07-19 09:06:55 +08:00
|
|
|
// If we know that this cannot be a designation, just parse the nested
|
|
|
|
// initializer directly.
|
2008-12-10 04:22:58 +08:00
|
|
|
OwningExprResult SubElt(Actions);
|
2009-03-21 07:11:49 +08:00
|
|
|
if (MayBeDesignationStart(Tok.getKind(), PP))
|
|
|
|
SubElt = ParseInitializerWithPotentialDesignator();
|
|
|
|
else
|
2007-07-19 09:06:55 +08:00
|
|
|
SubElt = ParseInitializer();
|
2008-11-03 17:28:22 +08:00
|
|
|
|
2007-07-19 09:06:55 +08:00
|
|
|
// If we couldn't parse the subelement, bail out.
|
2008-12-09 21:15:23 +08:00
|
|
|
if (!SubElt.isInvalid()) {
|
2008-12-10 08:02:53 +08:00
|
|
|
InitExprs.push_back(SubElt.release());
|
2008-04-21 03:07:56 +08:00
|
|
|
} else {
|
|
|
|
InitExprsOk = false;
|
|
|
|
|
|
|
|
// We have two ways to try to recover from this error: if the code looks
|
2008-10-27 05:46:13 +08:00
|
|
|
// gramatically ok (i.e. we have a comma coming up) try to continue
|
2008-04-21 03:07:56 +08:00
|
|
|
// parsing the rest of the initializer. This allows us to emit
|
|
|
|
// diagnostics for later elements that we find. If we don't see a comma,
|
|
|
|
// assume there is a parse error, and just skip to recover.
|
2008-11-26 06:21:31 +08:00
|
|
|
// FIXME: This comment doesn't sound right. If there is a r_brace
|
|
|
|
// immediately, it can't be an error, since there is no other way of
|
|
|
|
// leaving this loop except through this if.
|
2008-04-21 03:07:56 +08:00
|
|
|
if (Tok.isNot(tok::comma)) {
|
|
|
|
SkipUntil(tok::r_brace, false, true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-12-09 21:15:23 +08:00
|
|
|
|
2007-07-19 09:06:55 +08:00
|
|
|
// If we don't have a comma continued list, we're done.
|
2007-10-10 01:33:22 +08:00
|
|
|
if (Tok.isNot(tok::comma)) break;
|
2008-12-12 06:51:44 +08:00
|
|
|
|
2008-10-27 06:36:07 +08:00
|
|
|
// TODO: save comma locations if some client cares.
|
2007-07-19 09:06:55 +08:00
|
|
|
ConsumeToken();
|
2008-12-12 06:51:44 +08:00
|
|
|
|
2007-07-19 09:06:55 +08:00
|
|
|
// Handle trailing comma.
|
2007-10-10 01:33:22 +08:00
|
|
|
if (Tok.is(tok::r_brace)) break;
|
2006-08-14 05:54:02 +08:00
|
|
|
}
|
2007-10-10 01:33:22 +08:00
|
|
|
if (InitExprsOk && Tok.is(tok::r_brace))
|
2009-01-20 06:31:54 +08:00
|
|
|
return Actions.ActOnInitList(LBraceLoc, move_arg(InitExprs),
|
2009-03-21 07:11:49 +08:00
|
|
|
ConsumeBrace());
|
2008-12-12 06:51:44 +08:00
|
|
|
|
2006-08-14 05:54:02 +08:00
|
|
|
// Match the '}'.
|
2006-08-15 12:55:54 +08:00
|
|
|
MatchRHSPunctuation(tok::r_brace, LBraceLoc);
|
2008-12-12 06:51:44 +08:00
|
|
|
return ExprError(); // an error occurred.
|
2006-08-14 05:54:02 +08:00
|
|
|
}
|
|
|
|
|