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"
|
|
|
|
#include "clang/Basic/Diagnostic.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.
|
2006-08-14 05:54:02 +08:00
|
|
|
static bool MayBeDesignationStart(tok::TokenKind K) {
|
|
|
|
switch (K) {
|
|
|
|
default: return false;
|
|
|
|
case tok::period: // designator: '.' identifier
|
|
|
|
case tok::l_square: // designator: array-designator
|
|
|
|
case tok::identifier: // designation: identifier ':'
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
///
|
2008-10-27 06:36:07 +08:00
|
|
|
Parser::ExprResult Parser::
|
|
|
|
ParseInitializerWithPotentialDesignator(InitListDesignations &Designations,
|
|
|
|
unsigned InitNum) {
|
|
|
|
|
|
|
|
// 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)) {
|
|
|
|
if (NextToken().is(tok::colon)) {
|
|
|
|
Diag(Tok, diag::ext_gnu_old_style_field_designator);
|
|
|
|
|
|
|
|
Designation &D = Designations.CreateDesignation(InitNum);
|
|
|
|
D.AddDesignator(Designator::getField(Tok.getIdentifierInfo()));
|
|
|
|
ConsumeToken(); // Eat the identifier.
|
|
|
|
|
|
|
|
assert(Tok.is(tok::colon) && "NextToken() not working properly!");
|
|
|
|
ConsumeToken();
|
|
|
|
return ParseInitializer();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, parse the assignment-expression.
|
|
|
|
return ParseAssignmentExpression();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-14 05:54:02 +08:00
|
|
|
// Parse each designator in the designator list until we find an initializer.
|
|
|
|
while (1) {
|
|
|
|
switch (Tok.getKind()) {
|
|
|
|
case tok::equal:
|
|
|
|
// We read some number (at least one due to the grammar we implemented)
|
|
|
|
// of designators and found an '=' sign. The following tokens must be
|
|
|
|
// the initializer.
|
|
|
|
ConsumeToken();
|
|
|
|
return ParseInitializer();
|
|
|
|
|
|
|
|
default: {
|
|
|
|
// We read some number (at least one due to the grammar we implemented)
|
|
|
|
// of designators and found something that isn't an = or an initializer.
|
|
|
|
// If we have exactly one array designator [TODO CHECK], this is the GNU
|
|
|
|
// 'designation: array-designator' extension. Otherwise, it is a parse
|
|
|
|
// error.
|
|
|
|
SourceLocation Loc = Tok.getLocation();
|
|
|
|
ExprResult Init = ParseInitializer();
|
|
|
|
if (Init.isInvalid) return Init;
|
|
|
|
|
|
|
|
Diag(Tok, diag::ext_gnu_missing_equal_designator);
|
|
|
|
return Init;
|
|
|
|
}
|
|
|
|
case tok::period:
|
|
|
|
// designator: '.' identifier
|
|
|
|
ConsumeToken();
|
|
|
|
if (ExpectAndConsume(tok::identifier, diag::err_expected_ident))
|
|
|
|
return ExprResult(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case tok::l_square: {
|
|
|
|
// array-designator: '[' constant-expression ']'
|
|
|
|
// array-designator: '[' constant-expression '...' constant-expression ']'
|
2008-01-26 03:37:24 +08:00
|
|
|
// When designation is empty, this can be '[' objc-message-expr ']'. Note
|
|
|
|
// that we also have the case of [4][foo bar], which is the gnu designator
|
|
|
|
// extension + objc message send.
|
2006-10-16 14:12:55 +08:00
|
|
|
SourceLocation StartLoc = ConsumeBracket();
|
2006-08-14 05:54:02 +08:00
|
|
|
|
2008-01-26 03:37:24 +08:00
|
|
|
// 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-01-26 03:43:26 +08:00
|
|
|
// FIXME: Emit ext_gnu_missing_equal_designator for inits like
|
|
|
|
// [4][foo bar].
|
2008-01-26 03:37:24 +08:00
|
|
|
IdentifierInfo *Name = Tok.getIdentifierInfo();
|
|
|
|
ConsumeToken();
|
2008-06-03 05:31:07 +08:00
|
|
|
return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, Name, 0);
|
2008-01-26 03:37:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note that we parse this as an assignment expression, not a constant
|
2008-01-26 03:43:26 +08:00
|
|
|
// expression (allowing *=, =, etc) to handle the objc case. Sema needs
|
|
|
|
// to validate that the expression is a constant.
|
2008-01-26 03:37:24 +08:00
|
|
|
ExprResult Idx = ParseAssignmentExpression();
|
2006-08-14 05:54:02 +08:00
|
|
|
if (Idx.isInvalid) {
|
|
|
|
SkipUntil(tok::r_square);
|
|
|
|
return Idx;
|
|
|
|
}
|
|
|
|
|
2008-01-26 03:43:26 +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
|
2008-06-03 05:31:07 +08:00
|
|
|
// message send, handle it now. An objc-message send is the start of
|
|
|
|
// an assignment-expression production.
|
2008-01-26 03:43:26 +08:00
|
|
|
if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) &&
|
|
|
|
Tok.isNot(tok::r_square)) {
|
|
|
|
// FIXME: Emit ext_gnu_missing_equal_designator for inits like
|
|
|
|
// [4][foo bar].
|
2008-06-03 05:31:07 +08:00
|
|
|
return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, 0,Idx.Val);
|
2008-01-26 03:43:26 +08:00
|
|
|
}
|
|
|
|
|
2006-08-14 05:54:02 +08:00
|
|
|
// Handle the gnu array range extension.
|
2007-10-10 01:33:22 +08:00
|
|
|
if (Tok.is(tok::ellipsis)) {
|
2006-08-14 05:54:02 +08:00
|
|
|
Diag(Tok, diag::ext_gnu_array_range);
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
ExprResult RHS = ParseConstantExpression();
|
|
|
|
if (RHS.isInvalid) {
|
|
|
|
SkipUntil(tok::r_square);
|
|
|
|
return RHS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-15 12:55:54 +08:00
|
|
|
MatchRHSPunctuation(tok::r_square, StartLoc);
|
2006-08-14 05:54:02 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// ParseInitializer
|
|
|
|
/// initializer: [C99 6.7.8]
|
|
|
|
/// assignment-expression
|
|
|
|
/// '{' initializer-list '}'
|
|
|
|
/// '{' initializer-list ',' '}'
|
|
|
|
/// [GNU] '{' '}'
|
|
|
|
///
|
|
|
|
/// initializer-list:
|
|
|
|
/// designation[opt] initializer
|
|
|
|
/// initializer-list ',' designation[opt] initializer
|
|
|
|
///
|
|
|
|
Parser::ExprResult Parser::ParseInitializer() {
|
2008-10-27 06:36:07 +08:00
|
|
|
// TODO: Split this up into ParseInitializer + ParseBraceInitializer, make
|
|
|
|
// ParseInitializer inline so that the non-brace case is short-cut.
|
2007-10-10 01:33:22 +08:00
|
|
|
if (Tok.isNot(tok::l_brace))
|
2006-08-14 05:54:02 +08:00
|
|
|
return ParseAssignmentExpression();
|
|
|
|
|
2006-10-16 14:12:55 +08:00
|
|
|
SourceLocation LBraceLoc = ConsumeBrace();
|
2006-08-14 05:54:02 +08:00
|
|
|
|
|
|
|
// We support empty initializers, but tell the user that they aren't using
|
|
|
|
// C99-clean code.
|
2007-10-10 01:33:22 +08:00
|
|
|
if (Tok.is(tok::r_brace)) {
|
2006-08-14 05:54:02 +08:00
|
|
|
Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
|
2007-07-19 09:06:55 +08:00
|
|
|
// Match the '}'.
|
2007-09-16 11:34:24 +08:00
|
|
|
return Actions.ActOnInitList(LBraceLoc, 0, 0, ConsumeBrace());
|
2007-07-19 09:06:55 +08:00
|
|
|
}
|
2008-10-27 06:36:07 +08:00
|
|
|
|
|
|
|
/// InitExprs - This is the actual list of expressions contained in the
|
|
|
|
/// initializer.
|
2007-07-19 09:06:55 +08:00
|
|
|
llvm::SmallVector<ExprTy*, 8> InitExprs;
|
2008-10-27 06:36:07 +08:00
|
|
|
|
|
|
|
/// ExprDesignators - For each initializer, keep track of the designator that
|
|
|
|
/// was specified for it, if any.
|
|
|
|
InitListDesignations InitExprDesignations(Actions);
|
|
|
|
|
2007-07-19 09:06:55 +08:00
|
|
|
bool InitExprsOk = true;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
// Parse: designation[opt] initializer
|
2006-08-14 05:54:02 +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.
|
|
|
|
ExprResult SubElt;
|
|
|
|
if (!MayBeDesignationStart(Tok.getKind()))
|
|
|
|
SubElt = ParseInitializer();
|
|
|
|
else
|
2008-10-27 06:36:07 +08:00
|
|
|
SubElt = ParseInitializerWithPotentialDesignator(InitExprDesignations,
|
|
|
|
InitExprs.size());
|
2007-07-19 09:06:55 +08:00
|
|
|
|
|
|
|
// If we couldn't parse the subelement, bail out.
|
2008-04-21 03:07:56 +08:00
|
|
|
if (!SubElt.isInvalid) {
|
2007-07-19 09:06:55 +08:00
|
|
|
InitExprs.push_back(SubElt.Val);
|
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.
|
|
|
|
if (Tok.isNot(tok::comma)) {
|
|
|
|
SkipUntil(tok::r_brace, false, true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-08-14 05:54:02 +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;
|
2007-07-19 09:06:55 +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();
|
|
|
|
|
|
|
|
// 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))
|
2007-09-16 11:34:24 +08:00
|
|
|
return Actions.ActOnInitList(LBraceLoc, &InitExprs[0], InitExprs.size(),
|
2007-07-19 09:06:55 +08:00
|
|
|
ConsumeBrace());
|
2008-04-21 03:07:56 +08:00
|
|
|
|
2008-10-27 06:36:07 +08:00
|
|
|
// On error, delete any parsed subexpressions.
|
2008-04-21 03:07:56 +08:00
|
|
|
for (unsigned i = 0, e = InitExprs.size(); i != e; ++i)
|
|
|
|
Actions.DeleteExpr(InitExprs[i]);
|
|
|
|
|
2006-08-14 05:54:02 +08:00
|
|
|
// Match the '}'.
|
2006-08-15 12:55:54 +08:00
|
|
|
MatchRHSPunctuation(tok::r_brace, LBraceLoc);
|
2007-07-19 09:06:55 +08:00
|
|
|
return ExprResult(true); // an error occurred.
|
2006-08-14 05:54:02 +08:00
|
|
|
}
|
|
|
|
|