aboutsummaryrefslogtreecommitdiff
path: root/lib/Parse/ParseStmt.cpp
diff options
context:
space:
mode:
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>2008-07-09 22:53:07 +0000
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>2008-07-09 22:53:07 +0000
commitf7da726f09a6b7c5b9f5308e9690cb015398e671 (patch)
tree5ad8b5e569574f0626a77cc6be84b9c88a0e5e72 /lib/Parse/ParseStmt.cpp
parent9e0ed0bd5a3a7bac73973980ff32132a7724e674 (diff)
Simplify the parser a bit by looking at the next token without consuming it (by Preprocessor::LookNext):
-Remove ParseExpressionWithLeadingIdentifier and ParseAssignmentExprWithLeadingIdentifier. -Separate ParseLabeledStatement from ParseIdentifierStatement. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@53376 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Parse/ParseStmt.cpp')
-rw-r--r--lib/Parse/ParseStmt.cpp71
1 files changed, 41 insertions, 30 deletions
diff --git a/lib/Parse/ParseStmt.cpp b/lib/Parse/ParseStmt.cpp
index 670181e224..b8ebc42a06 100644
--- a/lib/Parse/ParseStmt.cpp
+++ b/lib/Parse/ParseStmt.cpp
@@ -79,8 +79,11 @@ Parser::StmtResult Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
tok::TokenKind Kind = Tok.getKind();
SourceLocation AtLoc;
switch (Kind) {
- case tok::identifier: // C99 6.8.1: labeled-statement
- // identifier ':' statement
+ case tok::identifier:
+ if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
+ // identifier ':' statement
+ return ParseLabeledStatement();
+ }
// declaration (if !OnlyStatement)
// expression[opt] ';'
return ParseIdentifierStatement(OnlyStatement);
@@ -174,53 +177,61 @@ Parser::StmtResult Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
return Res;
}
-/// ParseIdentifierStatement - Because we don't have two-token lookahead, we
-/// have a bit of a quandry here. Reading the identifier is necessary to see if
-/// there is a ':' after it. If there is, this is a label, regardless of what
-/// else the identifier can mean. If not, this is either part of a declaration
-/// (if the identifier is a type-name) or part of an expression.
+/// ParseLabeledStatement - We have an identifier and a ':' after it.
///
/// labeled-statement:
/// identifier ':' statement
/// [GNU] identifier ':' attributes[opt] statement
-/// declaration (if !OnlyStatement)
-/// expression[opt] ';'
///
-Parser::StmtResult Parser::ParseIdentifierStatement(bool OnlyStatement) {
+Parser::StmtResult Parser::ParseLabeledStatement() {
assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
"Not an identifier!");
Token IdentTok = Tok; // Save the whole token.
ConsumeToken(); // eat the identifier.
+
+ assert(Tok.is(tok::colon) && "Not a label!");
// identifier ':' statement
- if (Tok.is(tok::colon)) {
- SourceLocation ColonLoc = ConsumeToken();
+ SourceLocation ColonLoc = ConsumeToken();
- // Read label attributes, if present.
- DeclTy *AttrList = 0;
- if (Tok.is(tok::kw___attribute))
- // TODO: save these somewhere.
- AttrList = ParseAttributes();
+ // Read label attributes, if present.
+ DeclTy *AttrList = 0;
+ if (Tok.is(tok::kw___attribute))
+ // TODO: save these somewhere.
+ AttrList = ParseAttributes();
- StmtResult SubStmt = ParseStatement();
-
- // Broken substmt shouldn't prevent the label from being added to the AST.
- if (SubStmt.isInvalid)
- SubStmt = Actions.ActOnNullStmt(ColonLoc);
-
- return Actions.ActOnLabelStmt(IdentTok.getLocation(),
- IdentTok.getIdentifierInfo(),
- ColonLoc, SubStmt.Val);
- }
+ StmtResult SubStmt = ParseStatement();
+
+ // Broken substmt shouldn't prevent the label from being added to the AST.
+ if (SubStmt.isInvalid)
+ SubStmt = Actions.ActOnNullStmt(ColonLoc);
+
+ return Actions.ActOnLabelStmt(IdentTok.getLocation(),
+ IdentTok.getIdentifierInfo(),
+ ColonLoc, SubStmt.Val);
+}
+/// ParseIdentifierStatement - This is either part of a declaration
+/// (if the identifier is a type-name) or part of an expression.
+///
+/// declaration (if !OnlyStatement)
+/// expression[opt] ';'
+///
+Parser::StmtResult Parser::ParseIdentifierStatement(bool OnlyStatement) {
+ assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
+ "Not an identifier!");
+
// Check to see if this is a declaration.
void *TypeRep;
if (!OnlyStatement &&
- (TypeRep = Actions.isTypeName(*IdentTok.getIdentifierInfo(), CurScope))) {
+ (TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope))) {
// Handle this. Warn/disable if in middle of block and !C99.
DeclSpec DS;
+ Token IdentTok = Tok; // Save the whole token.
+ ConsumeToken(); // eat the identifier.
+
// Add the typedef name to the start of the decl-specs.
const char *PrevSpec = 0;
int isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef,
@@ -263,8 +274,8 @@ Parser::StmtResult Parser::ParseIdentifierStatement(bool OnlyStatement) {
DeclaratorInfo.getSourceRange().getEnd());
}
- // Otherwise, this is an expression. Seed it with II and parse it.
- ExprResult Res = ParseExpressionWithLeadingIdentifier(IdentTok);
+ // Otherwise, this is an expression.
+ ExprResult Res = ParseExpression();
if (Res.isInvalid) {
SkipUntil(tok::semi);
return true;