2007-07-11 21:01:13 +04:00
|
|
|
//===--- Parser.cpp - C Language Family Parser ----------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 22:59:25 +03:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-07-11 21:01:13 +04:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Parser interfaces.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Parse/Parser.h"
|
2008-08-11 07:45:03 +04:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
#include "clang/Parse/DeclSpec.h"
|
|
|
|
#include "clang/Parse/Scope.h"
|
2008-10-04 23:21:03 +04:00
|
|
|
#include "ParsePragma.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
Parser::Parser(Preprocessor &pp, Action &actions)
|
|
|
|
: PP(pp), Actions(actions), Diags(PP.getDiagnostics()) {
|
|
|
|
Tok.setKind(tok::eof);
|
|
|
|
CurScope = 0;
|
2007-07-15 04:04:39 +04:00
|
|
|
NumCachedScopes = 0;
|
2007-07-11 21:01:13 +04:00
|
|
|
ParenCount = BracketCount = BraceCount = 0;
|
2008-01-07 22:49:32 +03:00
|
|
|
ObjCImpDecl = 0;
|
2008-10-04 23:21:03 +04:00
|
|
|
|
|
|
|
// Add #pragma handlers. These are removed and destroyed in the
|
|
|
|
// destructor.
|
|
|
|
PackHandler =
|
|
|
|
new PragmaPackHandler(&PP.getIdentifierTable().get("pack"), actions);
|
|
|
|
PP.AddPragmaHandler(0, PackHandler);
|
|
|
|
|
2008-06-25 02:12:16 +04:00
|
|
|
// Instantiate a LexedMethodsForTopClass for all the non-nested classes.
|
|
|
|
PushTopClassStack();
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Out-of-line virtual destructor to provide home for Action class.
|
|
|
|
Action::~Action() {}
|
|
|
|
|
|
|
|
|
2008-08-05 10:19:09 +04:00
|
|
|
bool Parser::Diag(SourceLocation Loc, unsigned DiagID,
|
2007-07-11 21:01:13 +04:00
|
|
|
const std::string &Msg) {
|
2007-12-13 01:39:36 +03:00
|
|
|
Diags.Report(FullSourceLoc(Loc,PP.getSourceManager()), DiagID, &Msg, 1);
|
2008-08-05 10:19:09 +04:00
|
|
|
return true;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2008-08-24 16:51:04 +04:00
|
|
|
bool Parser::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
|
2008-08-24 17:14:02 +04:00
|
|
|
const SourceRange& Range) {
|
2008-08-24 16:51:04 +04:00
|
|
|
Diags.Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-08-05 10:19:09 +04:00
|
|
|
bool Parser::Diag(SourceLocation Loc, unsigned DiagID, const SourceRange &R) {
|
2008-07-26 04:16:04 +04:00
|
|
|
Diags.Report(FullSourceLoc(Loc,PP.getSourceManager()), DiagID, 0, 0,
|
|
|
|
&R, 1);
|
2008-08-05 10:19:09 +04:00
|
|
|
return true;
|
2008-07-26 04:16:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
/// MatchRHSPunctuation - For punctuation with a LHS and RHS (e.g. '['/']'),
|
|
|
|
/// this helper function matches and consumes the specified RHS token if
|
|
|
|
/// present. If not present, it emits the specified diagnostic indicating
|
|
|
|
/// that the parser failed to match the RHS of the token at LHSLoc. LHSName
|
|
|
|
/// should be the name of the unmatched LHS token.
|
|
|
|
SourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok,
|
|
|
|
SourceLocation LHSLoc) {
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-10-09 21:23:58 +04:00
|
|
|
if (Tok.is(RHSTok))
|
2007-07-11 21:01:13 +04:00
|
|
|
return ConsumeAnyToken();
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
SourceLocation R = Tok.getLocation();
|
|
|
|
const char *LHSName = "unknown";
|
|
|
|
diag::kind DID = diag::err_parse_error;
|
|
|
|
switch (RHSTok) {
|
|
|
|
default: break;
|
|
|
|
case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
|
|
|
|
case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
|
|
|
|
case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
|
|
|
|
case tok::greater: LHSName = "<"; DID = diag::err_expected_greater; break;
|
|
|
|
}
|
|
|
|
Diag(Tok, DID);
|
|
|
|
Diag(LHSLoc, diag::err_matching, LHSName);
|
|
|
|
SkipUntil(RHSTok);
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
|
|
|
|
/// input. If so, it is consumed and false is returned.
|
|
|
|
///
|
|
|
|
/// If the input is malformed, this emits the specified diagnostic. Next, if
|
|
|
|
/// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is
|
|
|
|
/// returned.
|
|
|
|
bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
|
|
|
|
const char *Msg, tok::TokenKind SkipToTok) {
|
2007-10-09 21:23:58 +04:00
|
|
|
if (Tok.is(ExpectedTok)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
ConsumeAnyToken();
|
|
|
|
return false;
|
|
|
|
}
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(Tok, DiagID, Msg);
|
|
|
|
if (SkipToTok != tok::unknown)
|
|
|
|
SkipUntil(SkipToTok);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Error recovery.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// SkipUntil - Read tokens until we get to the specified token, then consume
|
2007-07-24 21:03:04 +04:00
|
|
|
/// it (unless DontConsume is true). Because we cannot guarantee that the
|
2007-07-11 21:01:13 +04:00
|
|
|
/// token will ever occur, this skips to the next token, or to some likely
|
|
|
|
/// good stopping point. If StopAtSemi is true, skipping will stop at a ';'
|
|
|
|
/// character.
|
2008-06-19 23:28:49 +04:00
|
|
|
///
|
2007-07-11 21:01:13 +04:00
|
|
|
/// If SkipUntil finds the specified token, it returns true, otherwise it
|
2008-06-19 23:28:49 +04:00
|
|
|
/// returns false.
|
2007-07-11 21:01:13 +04:00
|
|
|
bool Parser::SkipUntil(const tok::TokenKind *Toks, unsigned NumToks,
|
|
|
|
bool StopAtSemi, bool DontConsume) {
|
|
|
|
// We always want this function to skip at least one token if the first token
|
|
|
|
// isn't T and if not at EOF.
|
|
|
|
bool isFirstTokenSkipped = true;
|
|
|
|
while (1) {
|
|
|
|
// If we found one of the tokens, stop and return true.
|
|
|
|
for (unsigned i = 0; i != NumToks; ++i) {
|
2007-10-09 21:23:58 +04:00
|
|
|
if (Tok.is(Toks[i])) {
|
2007-07-11 21:01:13 +04:00
|
|
|
if (DontConsume) {
|
|
|
|
// Noop, don't consume the token.
|
|
|
|
} else {
|
|
|
|
ConsumeAnyToken();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
switch (Tok.getKind()) {
|
|
|
|
case tok::eof:
|
|
|
|
// Ran out of tokens.
|
|
|
|
return false;
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
case tok::l_paren:
|
|
|
|
// Recursively skip properly-nested parens.
|
|
|
|
ConsumeParen();
|
|
|
|
SkipUntil(tok::r_paren, false);
|
|
|
|
break;
|
|
|
|
case tok::l_square:
|
|
|
|
// Recursively skip properly-nested square brackets.
|
|
|
|
ConsumeBracket();
|
|
|
|
SkipUntil(tok::r_square, false);
|
|
|
|
break;
|
|
|
|
case tok::l_brace:
|
|
|
|
// Recursively skip properly-nested braces.
|
|
|
|
ConsumeBrace();
|
|
|
|
SkipUntil(tok::r_brace, false);
|
|
|
|
break;
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Okay, we found a ']' or '}' or ')', which we think should be balanced.
|
|
|
|
// Since the user wasn't looking for this token (if they were, it would
|
|
|
|
// already be handled), this isn't balanced. If there is a LHS token at a
|
|
|
|
// higher level, we will assume that this matches the unbalanced token
|
|
|
|
// and return it. Otherwise, this is a spurious RHS token, which we skip.
|
|
|
|
case tok::r_paren:
|
|
|
|
if (ParenCount && !isFirstTokenSkipped)
|
|
|
|
return false; // Matches something.
|
|
|
|
ConsumeParen();
|
|
|
|
break;
|
|
|
|
case tok::r_square:
|
|
|
|
if (BracketCount && !isFirstTokenSkipped)
|
|
|
|
return false; // Matches something.
|
|
|
|
ConsumeBracket();
|
|
|
|
break;
|
|
|
|
case tok::r_brace:
|
|
|
|
if (BraceCount && !isFirstTokenSkipped)
|
|
|
|
return false; // Matches something.
|
|
|
|
ConsumeBrace();
|
|
|
|
break;
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
case tok::string_literal:
|
|
|
|
case tok::wide_string_literal:
|
|
|
|
ConsumeStringToken();
|
|
|
|
break;
|
|
|
|
case tok::semi:
|
|
|
|
if (StopAtSemi)
|
|
|
|
return false;
|
|
|
|
// FALL THROUGH.
|
|
|
|
default:
|
|
|
|
// Skip this token.
|
|
|
|
ConsumeToken();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
isFirstTokenSkipped = false;
|
2008-06-19 23:28:49 +04:00
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Scope manipulation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// EnterScope - Start a new scope.
|
|
|
|
void Parser::EnterScope(unsigned ScopeFlags) {
|
2007-07-15 04:04:39 +04:00
|
|
|
if (NumCachedScopes) {
|
|
|
|
Scope *N = ScopeCache[--NumCachedScopes];
|
2007-07-11 21:01:13 +04:00
|
|
|
N->Init(CurScope, ScopeFlags);
|
|
|
|
CurScope = N;
|
|
|
|
} else {
|
|
|
|
CurScope = new Scope(CurScope, ScopeFlags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ExitScope - Pop a scope off the scope stack.
|
|
|
|
void Parser::ExitScope() {
|
|
|
|
assert(CurScope && "Scope imbalance!");
|
|
|
|
|
2007-10-10 00:37:18 +04:00
|
|
|
// Inform the actions module that this scope is going away if there are any
|
|
|
|
// decls in it.
|
|
|
|
if (!CurScope->decl_empty())
|
2007-10-10 02:01:59 +04:00
|
|
|
Actions.ActOnPopScope(Tok.getLocation(), CurScope);
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-07-15 04:04:39 +04:00
|
|
|
Scope *OldScope = CurScope;
|
|
|
|
CurScope = OldScope->getParent();
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-07-15 04:04:39 +04:00
|
|
|
if (NumCachedScopes == ScopeCacheSize)
|
|
|
|
delete OldScope;
|
2007-07-11 21:01:13 +04:00
|
|
|
else
|
2007-07-15 04:04:39 +04:00
|
|
|
ScopeCache[NumCachedScopes++] = OldScope;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// C99 6.9: External Definitions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Parser::~Parser() {
|
|
|
|
// If we still have scopes active, delete the scope tree.
|
|
|
|
delete CurScope;
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Free the scope cache.
|
2007-07-15 04:04:39 +04:00
|
|
|
for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
|
|
|
|
delete ScopeCache[i];
|
2008-10-04 23:21:03 +04:00
|
|
|
|
|
|
|
// Remove the pragma handlers we installed.
|
|
|
|
PP.RemovePragmaHandler(0, PackHandler);
|
|
|
|
delete PackHandler;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Initialize - Warm up the parser.
|
|
|
|
///
|
|
|
|
void Parser::Initialize() {
|
|
|
|
// Prime the lexer look-ahead.
|
|
|
|
ConsumeToken();
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-08-26 10:24:45 +04:00
|
|
|
// Create the translation unit scope. Install it as the current scope.
|
2007-07-11 21:01:13 +04:00
|
|
|
assert(CurScope == 0 && "A scope is already active?");
|
2007-08-26 10:24:45 +04:00
|
|
|
EnterScope(Scope::DeclScope);
|
2007-10-10 02:01:59 +04:00
|
|
|
Actions.ActOnTranslationUnitScope(Tok.getLocation(), CurScope);
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-10-09 21:23:58 +04:00
|
|
|
if (Tok.is(tok::eof) &&
|
2007-08-25 09:47:03 +04:00
|
|
|
!getLang().CPlusPlus) // Empty source file is an extension in C
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(Tok, diag::ext_empty_source_file);
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-08-30 02:54:08 +04:00
|
|
|
// Initialization for Objective-C context sensitive keywords recognition.
|
2008-01-07 22:49:32 +03:00
|
|
|
// Referenced in Parser::ParseObjCTypeQualifierList.
|
2007-08-30 02:54:08 +04:00
|
|
|
if (getLang().ObjC1) {
|
2008-01-07 22:49:32 +03:00
|
|
|
ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
|
|
|
|
ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
|
|
|
|
ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
|
|
|
|
ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
|
|
|
|
ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
|
|
|
|
ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
|
2007-08-30 02:54:08 +04:00
|
|
|
}
|
2007-08-31 20:11:31 +04:00
|
|
|
if (getLang().ObjC2) {
|
2008-01-07 22:49:32 +03:00
|
|
|
ObjCPropertyAttrs[objc_readonly] = &PP.getIdentifierTable().get("readonly");
|
|
|
|
ObjCPropertyAttrs[objc_getter] = &PP.getIdentifierTable().get("getter");
|
|
|
|
ObjCPropertyAttrs[objc_setter] = &PP.getIdentifierTable().get("setter");
|
|
|
|
ObjCPropertyAttrs[objc_assign] = &PP.getIdentifierTable().get("assign");
|
2008-06-19 23:28:49 +04:00
|
|
|
ObjCPropertyAttrs[objc_readwrite] =
|
2007-08-31 20:11:31 +04:00
|
|
|
&PP.getIdentifierTable().get("readwrite");
|
2008-01-07 22:49:32 +03:00
|
|
|
ObjCPropertyAttrs[objc_retain] = &PP.getIdentifierTable().get("retain");
|
|
|
|
ObjCPropertyAttrs[objc_copy] = &PP.getIdentifierTable().get("copy");
|
2008-06-19 23:28:49 +04:00
|
|
|
ObjCPropertyAttrs[objc_nonatomic] =
|
2007-08-31 20:11:31 +04:00
|
|
|
&PP.getIdentifierTable().get("nonatomic");
|
|
|
|
}
|
2008-08-15 02:04:54 +04:00
|
|
|
|
|
|
|
Ident_super = &PP.getIdentifierTable().get("super");
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
|
|
|
|
/// action tells us to. This returns true if the EOF was encountered.
|
2007-11-30 02:05:20 +03:00
|
|
|
bool Parser::ParseTopLevelDecl(DeclTy*& Result) {
|
|
|
|
Result = 0;
|
2008-08-23 07:19:52 +04:00
|
|
|
if (Tok.is(tok::eof)) {
|
|
|
|
Actions.ActOnEndOfTranslationUnit();
|
|
|
|
return true;
|
|
|
|
}
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-11-30 02:05:20 +03:00
|
|
|
Result = ParseExternalDeclaration();
|
2007-07-11 21:01:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseTranslationUnit:
|
|
|
|
/// translation-unit: [C99 6.9]
|
2008-06-19 23:28:49 +04:00
|
|
|
/// external-declaration
|
|
|
|
/// translation-unit external-declaration
|
2007-07-11 21:01:13 +04:00
|
|
|
void Parser::ParseTranslationUnit() {
|
2008-08-23 06:06:50 +04:00
|
|
|
Initialize(); // pushes a scope.
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-11-30 02:05:20 +03:00
|
|
|
DeclTy *Res;
|
|
|
|
while (!ParseTopLevelDecl(Res))
|
2007-07-11 21:01:13 +04:00
|
|
|
/*parse them all*/;
|
2008-08-23 06:00:52 +04:00
|
|
|
|
|
|
|
ExitScope();
|
|
|
|
assert(CurScope == 0 && "Scope imbalance!");
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseExternalDeclaration:
|
|
|
|
/// external-declaration: [C99 6.9]
|
2007-08-11 00:57:02 +04:00
|
|
|
/// function-definition
|
|
|
|
/// declaration
|
2007-07-11 21:01:13 +04:00
|
|
|
/// [EXT] ';'
|
|
|
|
/// [GNU] asm-definition
|
2007-08-11 00:57:02 +04:00
|
|
|
/// [GNU] __extension__ external-declaration
|
2007-07-11 21:01:13 +04:00
|
|
|
/// [OBJC] objc-class-definition
|
|
|
|
/// [OBJC] objc-class-declaration
|
|
|
|
/// [OBJC] objc-alias-declaration
|
|
|
|
/// [OBJC] objc-protocol-definition
|
|
|
|
/// [OBJC] objc-method-definition
|
|
|
|
/// [OBJC] @end
|
|
|
|
///
|
|
|
|
/// [GNU] asm-definition:
|
|
|
|
/// simple-asm-expr ';'
|
|
|
|
///
|
|
|
|
Parser::DeclTy *Parser::ParseExternalDeclaration() {
|
|
|
|
switch (Tok.getKind()) {
|
|
|
|
case tok::semi:
|
|
|
|
Diag(Tok, diag::ext_top_level_semi);
|
|
|
|
ConsumeToken();
|
|
|
|
// TODO: Invoke action for top-level semicolon.
|
|
|
|
return 0;
|
2007-08-11 00:57:02 +04:00
|
|
|
case tok::kw___extension__: {
|
|
|
|
ConsumeToken();
|
|
|
|
// FIXME: Disable extension warnings.
|
|
|
|
DeclTy *RV = ParseExternalDeclaration();
|
|
|
|
// FIXME: Restore extension warnings.
|
|
|
|
return RV;
|
|
|
|
}
|
2008-02-08 03:33:21 +03:00
|
|
|
case tok::kw_asm: {
|
|
|
|
ExprResult Result = ParseSimpleAsm();
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2008-02-08 03:23:11 +03:00
|
|
|
ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
|
|
|
|
"top-level asm block");
|
2008-02-08 03:33:21 +03:00
|
|
|
|
|
|
|
if (!Result.isInvalid)
|
|
|
|
return Actions.ActOnFileScopeAsmDecl(Tok.getLocation(), Result.Val);
|
2008-05-28 03:32:43 +04:00
|
|
|
return 0;
|
2008-02-08 03:33:21 +03:00
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
case tok::at:
|
|
|
|
// @ is not a legal token unless objc is enabled, no need to check.
|
2007-09-11 00:51:04 +04:00
|
|
|
return ParseObjCAtDirectives();
|
2007-07-11 21:01:13 +04:00
|
|
|
case tok::minus:
|
|
|
|
case tok::plus:
|
2007-11-10 19:31:34 +03:00
|
|
|
if (getLang().ObjC1)
|
2007-11-14 02:01:27 +03:00
|
|
|
return ParseObjCMethodDefinition();
|
2007-11-10 19:31:34 +03:00
|
|
|
else {
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(Tok, diag::err_expected_external_declaration);
|
|
|
|
ConsumeToken();
|
|
|
|
}
|
|
|
|
return 0;
|
2007-08-25 10:57:03 +04:00
|
|
|
case tok::kw_namespace:
|
2007-07-11 21:01:13 +04:00
|
|
|
case tok::kw_typedef:
|
2007-08-25 22:15:16 +04:00
|
|
|
// A function definition cannot start with a these keywords.
|
2007-07-11 21:01:13 +04:00
|
|
|
return ParseDeclaration(Declarator::FileContext);
|
|
|
|
default:
|
|
|
|
// We can't tell whether this is a function-definition or declaration yet.
|
|
|
|
return ParseDeclarationOrFunctionDefinition();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
|
|
|
|
/// a declaration. We can't tell which we have until we read up to the
|
|
|
|
/// compound-statement in function-definition.
|
|
|
|
///
|
|
|
|
/// function-definition: [C99 6.9.1]
|
2008-04-05 09:52:15 +04:00
|
|
|
/// decl-specs declarator declaration-list[opt] compound-statement
|
|
|
|
/// [C90] function-definition: [C99 6.7.1] - implicit int result
|
2008-06-19 23:28:49 +04:00
|
|
|
/// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
|
2008-04-05 09:52:15 +04:00
|
|
|
///
|
2007-07-11 21:01:13 +04:00
|
|
|
/// declaration: [C99 6.7]
|
2007-08-22 10:06:56 +04:00
|
|
|
/// declaration-specifiers init-declarator-list[opt] ';'
|
|
|
|
/// [!C99] init-declarator-list ';' [TODO: warn in c99 mode]
|
2007-07-11 21:01:13 +04:00
|
|
|
/// [OMP] threadprivate-directive [TODO]
|
|
|
|
///
|
|
|
|
Parser::DeclTy *Parser::ParseDeclarationOrFunctionDefinition() {
|
|
|
|
// Parse the common declaration-specifiers piece.
|
|
|
|
DeclSpec DS;
|
|
|
|
ParseDeclarationSpecifiers(DS);
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
|
|
|
|
// declaration-specifiers init-declarator-list[opt] ';'
|
2007-10-09 21:23:58 +04:00
|
|
|
if (Tok.is(tok::semi)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
ConsumeToken();
|
|
|
|
return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
|
|
|
|
}
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2008-09-26 08:48:09 +04:00
|
|
|
// ObjC2 allows prefix attributes on class interfaces and protocols.
|
|
|
|
// FIXME: This still needs better diagnostics. We should only accept
|
|
|
|
// attributes here, no types, etc.
|
2007-10-09 21:23:58 +04:00
|
|
|
if (getLang().ObjC2 && Tok.is(tok::at)) {
|
2007-08-21 01:31:48 +04:00
|
|
|
SourceLocation AtLoc = ConsumeToken(); // the "@"
|
2008-09-26 08:48:09 +04:00
|
|
|
if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
|
|
|
|
!Tok.isObjCAtKeyword(tok::objc_protocol)) {
|
|
|
|
Diag(Tok, diag::err_objc_unexpected_attr);
|
2007-12-27 22:57:00 +03:00
|
|
|
SkipUntil(tok::semi); // FIXME: better skip?
|
|
|
|
return 0;
|
|
|
|
}
|
2008-01-02 22:17:38 +03:00
|
|
|
const char *PrevSpec = 0;
|
|
|
|
if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec))
|
|
|
|
Diag(AtLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
|
2008-09-26 08:48:09 +04:00
|
|
|
if (Tok.isObjCAtKeyword(tok::objc_protocol))
|
|
|
|
return ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
|
2008-06-19 23:28:49 +04:00
|
|
|
return ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes());
|
2007-08-21 01:31:48 +04:00
|
|
|
}
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2008-01-12 10:05:38 +03:00
|
|
|
// If the declspec consisted only of 'extern' and we have a string
|
|
|
|
// literal following it, this must be a C++ linkage specifier like
|
|
|
|
// 'extern "C"'.
|
2008-01-12 10:08:43 +03:00
|
|
|
if (Tok.is(tok::string_literal) && getLang().CPlusPlus &&
|
2008-01-12 10:05:38 +03:00
|
|
|
DS.getStorageClassSpec() == DeclSpec::SCS_extern &&
|
|
|
|
DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier)
|
|
|
|
return ParseLinkage(Declarator::FileContext);
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Parse the first declarator.
|
|
|
|
Declarator DeclaratorInfo(DS, Declarator::FileContext);
|
|
|
|
ParseDeclarator(DeclaratorInfo);
|
|
|
|
// Error parsing the declarator?
|
|
|
|
if (DeclaratorInfo.getIdentifier() == 0) {
|
|
|
|
// If so, skip until the semi-colon or a }.
|
|
|
|
SkipUntil(tok::r_brace, true);
|
2007-10-09 21:23:58 +04:00
|
|
|
if (Tok.is(tok::semi))
|
2007-07-11 21:01:13 +04:00
|
|
|
ConsumeToken();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the declarator is the start of a function definition, handle it.
|
2007-10-09 21:23:58 +04:00
|
|
|
if (Tok.is(tok::equal) || // int X()= -> not a function def
|
|
|
|
Tok.is(tok::comma) || // int X(), -> not a function def
|
|
|
|
Tok.is(tok::semi) || // int X(); -> not a function def
|
|
|
|
Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def
|
2008-10-06 21:10:33 +04:00
|
|
|
Tok.is(tok::kw___attribute) || // int X() __attr__ -> not a function def
|
|
|
|
(getLang().CPlusPlus &&
|
|
|
|
Tok.is(tok::l_paren)) ) { // int X(0) -> not a function def [C++]
|
2007-07-11 21:01:13 +04:00
|
|
|
// FALL THROUGH.
|
|
|
|
} else if (DeclaratorInfo.isFunctionDeclarator() &&
|
2008-06-21 14:00:56 +04:00
|
|
|
(Tok.is(tok::l_brace) || // int X() {}
|
|
|
|
( !getLang().CPlusPlus &&
|
|
|
|
isDeclarationSpecifier() ))) { // int X(f) int f; {}
|
2008-02-14 05:58:32 +03:00
|
|
|
if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
|
|
|
|
Diag(Tok, diag::err_function_declared_typedef);
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2008-02-14 05:58:32 +03:00
|
|
|
if (Tok.is(tok::l_brace)) {
|
|
|
|
// This recovery skips the entire function body. It would be nice
|
2008-06-19 23:28:49 +04:00
|
|
|
// to simply call ParseFunctionDefintion() below, however Sema
|
2008-02-14 05:58:32 +03:00
|
|
|
// assumes the declarator represents a function, not a typedef.
|
|
|
|
ConsumeBrace();
|
|
|
|
SkipUntil(tok::r_brace, true);
|
|
|
|
} else {
|
|
|
|
SkipUntil(tok::semi);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
return ParseFunctionDefinition(DeclaratorInfo);
|
|
|
|
} else {
|
|
|
|
if (DeclaratorInfo.isFunctionDeclarator())
|
|
|
|
Diag(Tok, diag::err_expected_fn_body);
|
|
|
|
else
|
|
|
|
Diag(Tok, diag::err_expected_after_declarator);
|
|
|
|
SkipUntil(tok::semi);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the init-declarator-list for a normal declaration.
|
|
|
|
return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseFunctionDefinition - We parsed and verified that the specified
|
|
|
|
/// Declarator is well formed. If this is a K&R-style function, read the
|
|
|
|
/// parameters declaration-list, then start the compound-statement.
|
|
|
|
///
|
2008-04-05 09:52:15 +04:00
|
|
|
/// function-definition: [C99 6.9.1]
|
|
|
|
/// decl-specs declarator declaration-list[opt] compound-statement
|
|
|
|
/// [C90] function-definition: [C99 6.7.1] - implicit int result
|
2008-06-19 23:28:49 +04:00
|
|
|
/// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
|
2007-07-11 21:01:13 +04:00
|
|
|
///
|
|
|
|
Parser::DeclTy *Parser::ParseFunctionDefinition(Declarator &D) {
|
|
|
|
const DeclaratorChunk &FnTypeInfo = D.getTypeObject(0);
|
|
|
|
assert(FnTypeInfo.Kind == DeclaratorChunk::Function &&
|
|
|
|
"This isn't a function declarator!");
|
|
|
|
const DeclaratorChunk::FunctionTypeInfo &FTI = FnTypeInfo.Fun;
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2008-04-05 09:52:15 +04:00
|
|
|
// If this is C90 and the declspecs were completely missing, fudge in an
|
|
|
|
// implicit int. We do this here because this is the only place where
|
|
|
|
// declaration-specifiers are completely optional in the grammar.
|
2008-04-05 10:32:51 +04:00
|
|
|
if (getLang().ImplicitInt && D.getDeclSpec().getParsedSpecifiers() == 0) {
|
2008-04-05 09:52:15 +04:00
|
|
|
const char *PrevSpec;
|
2008-10-20 06:01:34 +04:00
|
|
|
D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
|
|
|
|
D.getIdentifierLoc(),
|
|
|
|
PrevSpec);
|
2008-04-05 09:52:15 +04:00
|
|
|
}
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// If this declaration was formed with a K&R-style identifier list for the
|
|
|
|
// arguments, parse declarations for all of the args next.
|
|
|
|
// int foo(a,b) int a; float b; {}
|
|
|
|
if (!FTI.hasPrototype && FTI.NumArgs != 0)
|
|
|
|
ParseKNRParamDeclarations(D);
|
|
|
|
|
|
|
|
// We should have an opening brace now.
|
2007-10-09 21:23:58 +04:00
|
|
|
if (Tok.isNot(tok::l_brace)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(Tok, diag::err_expected_fn_body);
|
|
|
|
|
|
|
|
// Skip over garbage, until we get to '{'. Don't eat the '{'.
|
|
|
|
SkipUntil(tok::l_brace, true, true);
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// If we didn't find the '{', bail out.
|
2007-10-09 21:23:58 +04:00
|
|
|
if (Tok.isNot(tok::l_brace))
|
2007-07-11 21:01:13 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-10-09 21:14:05 +04:00
|
|
|
SourceLocation BraceLoc = Tok.getLocation();
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-10-09 21:14:05 +04:00
|
|
|
// Enter a scope for the function body.
|
|
|
|
EnterScope(Scope::FnScope|Scope::DeclScope);
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-10-09 21:14:05 +04:00
|
|
|
// Tell the actions module that we have entered a function definition with the
|
|
|
|
// specified Declarator for the function.
|
|
|
|
DeclTy *Res = Actions.ActOnStartOfFunctionDef(CurScope, D);
|
2008-06-19 23:28:49 +04:00
|
|
|
|
|
|
|
return ParseFunctionStatementBody(Res, BraceLoc, BraceLoc);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
|
|
|
|
/// types for a function with a K&R-style identifier list for arguments.
|
|
|
|
void Parser::ParseKNRParamDeclarations(Declarator &D) {
|
|
|
|
// We know that the top-level of this declarator is a function.
|
|
|
|
DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
|
|
|
|
|
2008-04-08 08:40:51 +04:00
|
|
|
// Enter function-declaration scope, limiting any declarators to the
|
|
|
|
// function prototype scope, including parameter declarators.
|
2008-05-20 13:10:20 +04:00
|
|
|
EnterScope(Scope::FnScope|Scope::DeclScope);
|
2008-04-08 08:40:51 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Read all the argument declarations.
|
|
|
|
while (isDeclarationSpecifier()) {
|
|
|
|
SourceLocation DSStart = Tok.getLocation();
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Parse the common declaration-specifiers piece.
|
|
|
|
DeclSpec DS;
|
|
|
|
ParseDeclarationSpecifiers(DS);
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// C99 6.9.1p6: 'each declaration in the declaration list shall have at
|
|
|
|
// least one declarator'.
|
|
|
|
// NOTE: GCC just makes this an ext-warn. It's not clear what it does with
|
|
|
|
// the declarations though. It's trivial to ignore them, really hard to do
|
|
|
|
// anything else with them.
|
2007-10-09 21:23:58 +04:00
|
|
|
if (Tok.is(tok::semi)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(DSStart, diag::err_declaration_does_not_declare_param);
|
|
|
|
ConsumeToken();
|
|
|
|
continue;
|
|
|
|
}
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
|
|
|
|
// than register.
|
|
|
|
if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
|
|
|
|
DS.getStorageClassSpec() != DeclSpec::SCS_register) {
|
|
|
|
Diag(DS.getStorageClassSpecLoc(),
|
|
|
|
diag::err_invalid_storage_class_in_func_decl);
|
|
|
|
DS.ClearStorageClassSpecs();
|
|
|
|
}
|
|
|
|
if (DS.isThreadSpecified()) {
|
|
|
|
Diag(DS.getThreadSpecLoc(),
|
|
|
|
diag::err_invalid_storage_class_in_func_decl);
|
|
|
|
DS.ClearStorageClassSpecs();
|
|
|
|
}
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Parse the first declarator attached to this declspec.
|
|
|
|
Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
|
|
|
|
ParseDeclarator(ParmDeclarator);
|
|
|
|
|
|
|
|
// Handle the full declarator list.
|
|
|
|
while (1) {
|
|
|
|
DeclTy *AttrList;
|
|
|
|
// If attributes are present, parse them.
|
2007-10-09 21:23:58 +04:00
|
|
|
if (Tok.is(tok::kw___attribute))
|
2007-07-11 21:01:13 +04:00
|
|
|
// FIXME: attach attributes too.
|
|
|
|
AttrList = ParseAttributes();
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Ask the actions module to compute the type for this declarator.
|
2008-06-19 23:28:49 +04:00
|
|
|
Action::DeclTy *Param =
|
2008-04-08 08:40:51 +04:00
|
|
|
Actions.ActOnParamDeclarator(CurScope, ParmDeclarator);
|
2007-09-11 00:51:04 +04:00
|
|
|
|
2008-06-19 23:28:49 +04:00
|
|
|
if (Param &&
|
2007-07-11 21:01:13 +04:00
|
|
|
// A missing identifier has already been diagnosed.
|
|
|
|
ParmDeclarator.getIdentifier()) {
|
|
|
|
|
|
|
|
// Scan the argument list looking for the correct param to apply this
|
|
|
|
// type.
|
|
|
|
for (unsigned i = 0; ; ++i) {
|
|
|
|
// C99 6.9.1p6: those declarators shall declare only identifiers from
|
|
|
|
// the identifier list.
|
|
|
|
if (i == FTI.NumArgs) {
|
|
|
|
Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param,
|
|
|
|
ParmDeclarator.getIdentifier()->getName());
|
|
|
|
break;
|
|
|
|
}
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
|
|
|
|
// Reject redefinitions of parameters.
|
2008-04-08 08:40:51 +04:00
|
|
|
if (FTI.ArgInfo[i].Param) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(ParmDeclarator.getIdentifierLoc(),
|
|
|
|
diag::err_param_redefinition,
|
|
|
|
ParmDeclarator.getIdentifier()->getName());
|
|
|
|
} else {
|
2008-04-08 08:40:51 +04:00
|
|
|
FTI.ArgInfo[i].Param = Param;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have a comma, it is either the end of the list (a ';') or
|
|
|
|
// an error, bail out.
|
2007-10-09 21:23:58 +04:00
|
|
|
if (Tok.isNot(tok::comma))
|
2007-07-11 21:01:13 +04:00
|
|
|
break;
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Consume the comma.
|
|
|
|
ConsumeToken();
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Parse the next declarator.
|
|
|
|
ParmDeclarator.clear();
|
|
|
|
ParseDeclarator(ParmDeclarator);
|
|
|
|
}
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-10-09 21:23:58 +04:00
|
|
|
if (Tok.is(tok::semi)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
ConsumeToken();
|
|
|
|
} else {
|
|
|
|
Diag(Tok, diag::err_parse_error);
|
|
|
|
// Skip to end of block or statement
|
|
|
|
SkipUntil(tok::semi, true);
|
2007-10-09 21:23:58 +04:00
|
|
|
if (Tok.is(tok::semi))
|
2007-07-11 21:01:13 +04:00
|
|
|
ConsumeToken();
|
|
|
|
}
|
|
|
|
}
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2008-04-08 08:40:51 +04:00
|
|
|
// Leave prototype scope.
|
|
|
|
ExitScope();
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// The actions module must verify that all arguments were declared.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
|
|
|
|
/// allowed to be a wide string, and is not subject to character translation.
|
|
|
|
///
|
|
|
|
/// [GNU] asm-string-literal:
|
|
|
|
/// string-literal
|
|
|
|
///
|
2007-11-20 22:21:03 +03:00
|
|
|
Parser::ExprResult Parser::ParseAsmStringLiteral() {
|
2007-07-11 21:01:13 +04:00
|
|
|
if (!isTokenStringLiteral()) {
|
|
|
|
Diag(Tok, diag::err_expected_string_literal);
|
2007-11-20 22:21:03 +03:00
|
|
|
return true;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
ExprResult Res = ParseStringLiteralExpression();
|
2007-11-20 22:21:03 +03:00
|
|
|
if (Res.isInvalid) return true;
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// TODO: Diagnose: wide string literal in 'asm'
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-11-20 22:21:03 +03:00
|
|
|
return Res;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseSimpleAsm
|
|
|
|
///
|
|
|
|
/// [GNU] simple-asm-expr:
|
|
|
|
/// 'asm' '(' asm-string-literal ')'
|
|
|
|
///
|
2008-02-08 03:33:21 +03:00
|
|
|
Parser::ExprResult Parser::ParseSimpleAsm() {
|
2007-10-09 21:23:58 +04:00
|
|
|
assert(Tok.is(tok::kw_asm) && "Not an asm!");
|
2008-02-08 03:33:21 +03:00
|
|
|
SourceLocation Loc = ConsumeToken();
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2007-10-09 21:23:58 +04:00
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after, "asm");
|
2008-05-28 03:32:43 +04:00
|
|
|
return true;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2008-02-08 03:33:21 +03:00
|
|
|
ConsumeParen();
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2008-02-08 03:33:21 +03:00
|
|
|
ExprResult Result = ParseAsmStringLiteral();
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2008-08-05 05:35:17 +04:00
|
|
|
if (Result.isInvalid) {
|
|
|
|
SkipUntil(tok::r_paren);
|
|
|
|
} else {
|
|
|
|
MatchRHSPunctuation(tok::r_paren, Loc);
|
|
|
|
}
|
2008-06-19 23:28:49 +04:00
|
|
|
|
2008-02-08 03:33:21 +03:00
|
|
|
return Result;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|