/// should be the name of the unmatched LHS token.
SourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok,
SourceLocation LHSLoc) {
-
+
if (Tok.is(RHSTok))
return ConsumeAnyToken();
-
+
SourceLocation R = Tok.getLocation();
const char *LHSName = "unknown";
diag::kind DID = diag::err_parse_error;
ConsumeAnyToken();
return false;
}
-
+
Diag(Tok, DiagID, Msg);
if (SkipToTok != tok::unknown)
SkipUntil(SkipToTok);
/// 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.
-///
+///
/// If SkipUntil finds the specified token, it returns true, otherwise it
-/// returns false.
+/// returns false.
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
return true;
}
}
-
+
switch (Tok.getKind()) {
case tok::eof:
// Ran out of tokens.
return false;
-
+
case tok::l_paren:
// Recursively skip properly-nested parens.
ConsumeParen();
ConsumeBrace();
SkipUntil(tok::r_brace, false);
break;
-
+
// 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
return false; // Matches something.
ConsumeBrace();
break;
-
+
case tok::string_literal:
case tok::wide_string_literal:
ConsumeStringToken();
break;
}
isFirstTokenSkipped = false;
- }
+ }
}
//===----------------------------------------------------------------------===//
// decls in it.
if (!CurScope->decl_empty())
Actions.ActOnPopScope(Tok.getLocation(), CurScope);
-
+
Scope *OldScope = CurScope;
CurScope = OldScope->getParent();
-
+
if (NumCachedScopes == ScopeCacheSize)
delete OldScope;
else
Parser::~Parser() {
// If we still have scopes active, delete the scope tree.
delete CurScope;
-
+
// Free the scope cache.
for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
delete ScopeCache[i];
void Parser::Initialize() {
// Prime the lexer look-ahead.
ConsumeToken();
-
+
// Create the translation unit scope. Install it as the current scope.
assert(CurScope == 0 && "A scope is already active?");
EnterScope(Scope::DeclScope);
Actions.ActOnTranslationUnitScope(Tok.getLocation(), CurScope);
-
+
if (Tok.is(tok::eof) &&
!getLang().CPlusPlus) // Empty source file is an extension in C
Diag(Tok, diag::ext_empty_source_file);
-
+
// Initialization for Objective-C context sensitive keywords recognition.
// Referenced in Parser::ParseObjCTypeQualifierList.
if (getLang().ObjC1) {
ObjCPropertyAttrs[objc_getter] = &PP.getIdentifierTable().get("getter");
ObjCPropertyAttrs[objc_setter] = &PP.getIdentifierTable().get("setter");
ObjCPropertyAttrs[objc_assign] = &PP.getIdentifierTable().get("assign");
- ObjCPropertyAttrs[objc_readwrite] =
+ ObjCPropertyAttrs[objc_readwrite] =
&PP.getIdentifierTable().get("readwrite");
ObjCPropertyAttrs[objc_retain] = &PP.getIdentifierTable().get("retain");
ObjCPropertyAttrs[objc_copy] = &PP.getIdentifierTable().get("copy");
- ObjCPropertyAttrs[objc_nonatomic] =
+ ObjCPropertyAttrs[objc_nonatomic] =
&PP.getIdentifierTable().get("nonatomic");
ObjCForCollectionInKW = &PP.getIdentifierTable().get("in");
}
bool Parser::ParseTopLevelDecl(DeclTy*& Result) {
Result = 0;
if (Tok.is(tok::eof)) return true;
-
+
Result = ParseExternalDeclaration();
return false;
}
/// ParseTranslationUnit:
/// translation-unit: [C99 6.9]
-/// external-declaration
-/// translation-unit external-declaration
+/// external-declaration
+/// translation-unit external-declaration
void Parser::ParseTranslationUnit() {
Initialize();
-
+
DeclTy *Res;
while (!ParseTopLevelDecl(Res))
/*parse them all*/;
-
+
Finalize();
}
}
case tok::kw_asm: {
ExprResult Result = ParseSimpleAsm();
-
+
ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
"top-level asm block");
/// function-definition: [C99 6.9.1]
/// decl-specs declarator declaration-list[opt] compound-statement
/// [C90] function-definition: [C99 6.7.1] - implicit int result
-/// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
+/// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
///
/// declaration: [C99 6.7]
/// declaration-specifiers init-declarator-list[opt] ';'
// Parse the common declaration-specifiers piece.
DeclSpec DS;
ParseDeclarationSpecifiers(DS);
-
+
// C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
// declaration-specifiers init-declarator-list[opt] ';'
if (Tok.is(tok::semi)) {
ConsumeToken();
return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
}
-
+
// ObjC2 allows prefix attributes on class interfaces.
if (getLang().ObjC2 && Tok.is(tok::at)) {
SourceLocation AtLoc = ConsumeToken(); // the "@"
const char *PrevSpec = 0;
if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec))
Diag(AtLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
- return ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes());
+ return ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes());
}
-
+
// 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"'.
isDeclarationSpecifier())) { // int X(f) int f; {}
if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Diag(Tok, diag::err_function_declared_typedef);
-
+
if (Tok.is(tok::l_brace)) {
// This recovery skips the entire function body. It would be nice
- // to simply call ParseFunctionDefintion() below, however Sema
+ // to simply call ParseFunctionDefintion() below, however Sema
// assumes the declarator represents a function, not a typedef.
ConsumeBrace();
SkipUntil(tok::r_brace, true);
/// function-definition: [C99 6.9.1]
/// decl-specs declarator declaration-list[opt] compound-statement
/// [C90] function-definition: [C99 6.7.1] - implicit int result
-/// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
+/// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
///
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;
-
+
// 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.
D.getDeclSpec().SetTypeSpecType(DeclSpec::TST_int, D.getIdentifierLoc(),
PrevSpec);
}
-
+
// 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; {}
// Skip over garbage, until we get to '{'. Don't eat the '{'.
SkipUntil(tok::l_brace, true, true);
-
+
// If we didn't find the '{', bail out.
if (Tok.isNot(tok::l_brace))
return 0;
}
-
+
SourceLocation BraceLoc = Tok.getLocation();
-
+
// Enter a scope for the function body.
EnterScope(Scope::FnScope|Scope::DeclScope);
-
+
// Tell the actions module that we have entered a function definition with the
// specified Declarator for the function.
DeclTy *Res = Actions.ActOnStartOfFunctionDef(CurScope, D);
-
- return ParseFunctionStatementBody(Res, BraceLoc, BraceLoc);
+
+ return ParseFunctionStatementBody(Res, BraceLoc, BraceLoc);
}
/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
// Read all the argument declarations.
while (isDeclarationSpecifier()) {
SourceLocation DSStart = Tok.getLocation();
-
+
// Parse the common declaration-specifiers piece.
DeclSpec DS;
ParseDeclarationSpecifiers(DS);
-
+
// 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
ConsumeToken();
continue;
}
-
+
// C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
// than register.
if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
diag::err_invalid_storage_class_in_func_decl);
DS.ClearStorageClassSpecs();
}
-
+
// Parse the first declarator attached to this declspec.
Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
ParseDeclarator(ParmDeclarator);
if (Tok.is(tok::kw___attribute))
// FIXME: attach attributes too.
AttrList = ParseAttributes();
-
+
// Ask the actions module to compute the type for this declarator.
- Action::DeclTy *Param =
+ Action::DeclTy *Param =
Actions.ActOnParamDeclarator(CurScope, ParmDeclarator);
- if (Param &&
+ if (Param &&
// A missing identifier has already been diagnosed.
ParmDeclarator.getIdentifier()) {
ParmDeclarator.getIdentifier()->getName());
break;
}
-
+
if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
// Reject redefinitions of parameters.
if (FTI.ArgInfo[i].Param) {
// an error, bail out.
if (Tok.isNot(tok::comma))
break;
-
+
// Consume the comma.
ConsumeToken();
-
+
// Parse the next declarator.
ParmDeclarator.clear();
ParseDeclarator(ParmDeclarator);
}
-
+
if (Tok.is(tok::semi)) {
ConsumeToken();
} else {
ConsumeToken();
}
}
-
+
// Leave prototype scope.
ExitScope();
Diag(Tok, diag::err_expected_string_literal);
return true;
}
-
+
ExprResult Res = ParseStringLiteralExpression();
if (Res.isInvalid) return true;
-
+
// TODO: Diagnose: wide string literal in 'asm'
-
+
return Res;
}
Parser::ExprResult Parser::ParseSimpleAsm() {
assert(Tok.is(tok::kw_asm) && "Not an asm!");
SourceLocation Loc = ConsumeToken();
-
+
if (Tok.isNot(tok::l_paren)) {
Diag(Tok, diag::err_expected_lparen_after, "asm");
return true;
}
-
+
ConsumeParen();
-
+
ExprResult Result = ParseAsmStringLiteral();
-
+
MatchRHSPunctuation(tok::r_paren, Loc);
-
+
return Result;
}