From 38ba3363ef2b79d30ade343fb6e7f0aae4bb33a9 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 17 Aug 2006 07:04:37 +0000 Subject: [PATCH] Hook up more of the ASTStreamer llvm-svn: 38936 --- clang/AST/ASTStreamer.cpp | 14 ++++++++-- clang/Parse/Parser.cpp | 44 ++++++++++++++++++++++-------- clang/Sema/ASTStreamer.cpp | 14 ++++++++-- clang/include/clang/Parse/Parser.h | 28 +++++++++++++++---- 4 files changed, 77 insertions(+), 23 deletions(-) diff --git a/clang/AST/ASTStreamer.cpp b/clang/AST/ASTStreamer.cpp index 77436cf10eec..59798f124a35 100644 --- a/clang/AST/ASTStreamer.cpp +++ b/clang/AST/ASTStreamer.cpp @@ -30,13 +30,21 @@ namespace { : P(PP, Builder) { PP.EnterSourceFile(MainFileID, 0, true); - // Parsing the specified input file. - P.ParseTranslationUnit(); + // Initialize the parser. + P.Initialize(); } /// ReadTopLevelDecl - Parse and return the next top-level declaration. Decl *ReadTopLevelDecl() { - return 0; + Parser::DeclTy *Result; + if (P.ParseTopLevelDecl(Result)) + return 0; + Result = (Decl*)1; // FIXME! + return (Decl*)Result; + } + + ~ASTStreamer() { + P.Finalize(); } }; } diff --git a/clang/Parse/Parser.cpp b/clang/Parse/Parser.cpp index 2dba273c4c31..c74c634632c8 100644 --- a/clang/Parse/Parser.cpp +++ b/clang/Parse/Parser.cpp @@ -195,18 +195,16 @@ void Parser::ExitScope() { // C99 6.9: External Definitions. //===----------------------------------------------------------------------===// -/// ParseTranslationUnit: -/// translation-unit: [C99 6.9] -/// external-declaration -/// translation-unit external-declaration -void Parser::ParseTranslationUnit() { +/// Initialize - Warm up the parser. +/// +void Parser::Initialize() { // Prime the lexer look-ahead. ConsumeToken(); // Create the global scope, install it as the current scope. assert(CurScope == 0 && "A scope is already active?"); EnterScope(); - + // Install builtin types. // TODO: Move this someplace more useful. @@ -217,23 +215,47 @@ void Parser::ParseTranslationUnit() { // TODO: add a 'TST_builtin' type? DS.TypeSpecType = DeclSpec::TST_typedef; - + Declarator D(DS, Declarator::FileContext); D.SetIdentifier(PP.getIdentifierInfo("__builtin_va_list"),SourceLocation()); Actions.ParseDeclarator(SourceLocation(), CurScope, D, 0); } - if (Tok.getKind() == tok::eof) // Empty source file is an extension. Diag(diag::ext_empty_source_file); +} + +/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the +/// action tells us to. This returns true if the EOF was encountered. +bool Parser::ParseTopLevelDecl(DeclTy*& Result) { + Result = 0; + if (Tok.getKind() == tok::eof) return true; - while (Tok.getKind() != tok::eof) - ParseExternalDeclaration(); - + ParseExternalDeclaration(); + return false; +} + +/// Finalize - Shut down the parser. +/// +void Parser::Finalize() { ExitScope(); assert(CurScope == 0 && "Scope imbalance!"); } +/// ParseTranslationUnit: +/// translation-unit: [C99 6.9] +/// external-declaration +/// translation-unit external-declaration +void Parser::ParseTranslationUnit() { + Initialize(); + + DeclTy *Res; + while (!ParseTopLevelDecl(Res)) + /*parse them all*/; + + Finalize(); +} + /// ParseExternalDeclaration: /// external-declaration: [C99 6.9] /// function-definition [TODO] diff --git a/clang/Sema/ASTStreamer.cpp b/clang/Sema/ASTStreamer.cpp index 77436cf10eec..59798f124a35 100644 --- a/clang/Sema/ASTStreamer.cpp +++ b/clang/Sema/ASTStreamer.cpp @@ -30,13 +30,21 @@ namespace { : P(PP, Builder) { PP.EnterSourceFile(MainFileID, 0, true); - // Parsing the specified input file. - P.ParseTranslationUnit(); + // Initialize the parser. + P.Initialize(); } /// ReadTopLevelDecl - Parse and return the next top-level declaration. Decl *ReadTopLevelDecl() { - return 0; + Parser::DeclTy *Result; + if (P.ParseTopLevelDecl(Result)) + return 0; + Result = (Decl*)1; // FIXME! + return (Decl*)Result; + } + + ~ASTStreamer() { + P.Finalize(); } }; } diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h index 6062fa53d662..8014a1a6b289 100644 --- a/clang/include/clang/Parse/Parser.h +++ b/clang/include/clang/Parse/Parser.h @@ -46,16 +46,25 @@ public: // Type forwarding. All of these are statically 'void*', but they may all be // different actual classes based on the actions in place. typedef Action::ExprTy ExprTy; + typedef Action::DeclTy DeclTy; // Parsing methods. + + /// ParseTranslationUnit - All in one method that initializes parses, and + /// shuts down the parser. void ParseTranslationUnit(); - struct ExprResult { - ExprTy *Val; - bool isInvalid; - - ExprResult(bool Invalid = false) : Val(0), isInvalid(Invalid) {} - }; + /// Initialize - Warm up the parser. + /// + void Initialize(); + + /// ParseTopLevelDecl - Parse one top-level declaration, return whatever the + /// action tells us to. This returns true if the EOF was encountered. + bool ParseTopLevelDecl(DeclTy*& Result); + + /// Finalize - Shut down the parser. + /// + void Finalize(); private: //===--------------------------------------------------------------------===// @@ -209,6 +218,13 @@ private: //===--------------------------------------------------------------------===// // C99 6.5: Expressions. + + struct ExprResult { + ExprTy *Val; + bool isInvalid; + + ExprResult(bool Invalid = false) : Val(0), isInvalid(Invalid) {} + }; ExprResult ParseExpression(); ExprResult ParseConstantExpression();