[cfe-commits] r69595 - in /cfe/trunk: include/clang/AST/ASTConsumer.h include/clang/Frontend/PCHWriter.h include/clang/Sema/SemaConsumer.h lib/Frontend/PCHWriter.cpp lib/Sema/ParseAST.cpp tools/clang-cc/GeneratePCH.cpp

Douglas Gregor dgregor at apple.com
Mon Apr 20 08:54:03 PDT 2009


Author: dgregor
Date: Mon Apr 20 10:53:59 2009
New Revision: 69595

URL: http://llvm.org/viewvc/llvm-project?rev=69595&view=rev
Log:
Introduce the notion of a SemaConsumer, which is an ASTConsumer that
also gets access to the Sema object performing semantic analysis. This
will be used by the PCH writer to serialize Sema state.

No functionality change.

Added:
    cfe/trunk/include/clang/Sema/SemaConsumer.h   (with props)
Modified:
    cfe/trunk/include/clang/AST/ASTConsumer.h
    cfe/trunk/include/clang/Frontend/PCHWriter.h
    cfe/trunk/lib/Frontend/PCHWriter.cpp
    cfe/trunk/lib/Sema/ParseAST.cpp
    cfe/trunk/tools/clang-cc/GeneratePCH.cpp

Modified: cfe/trunk/include/clang/AST/ASTConsumer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTConsumer.h?rev=69595&r1=69594&r2=69595&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/ASTConsumer.h (original)
+++ cfe/trunk/include/clang/AST/ASTConsumer.h Mon Apr 20 10:53:59 2009
@@ -19,16 +19,25 @@
   class DeclGroupRef;
   class TagDecl;
   class HandleTagDeclDefinition;
-  
+  class SemaConsumer; // layering violation required for safe SemaConsumer
+
 /// ASTConsumer - This is an abstract interface that should be implemented by
 /// clients that read ASTs.  This abstraction layer allows the client to be
 /// independent of the AST producer (e.g. parser vs AST dump file reader, etc).
 class ASTConsumer {
+  /// \brief Whether this AST consumer also requires information about
+  /// semantic analysis.
+  bool SemaConsumer;
+
+  friend class SemaConsumer;
+
 public:
+  ASTConsumer() : SemaConsumer(false) { }
+
   virtual ~ASTConsumer() {}
   
   /// Initialize - This is called to initialize the consumer, providing the
-  /// ASTContext.
+  /// ASTContext and the Action.
   virtual void Initialize(ASTContext &Context) {}
   
   /// HandleTopLevelDecl - Handle the specified top-level declaration.  This is
@@ -50,6 +59,9 @@
   /// PrintStats - If desired, print any statistics.
   virtual void PrintStats() {
   }
+
+  // Support isa/cast/dyn_cast
+  static bool classof(const ASTConsumer *) { return true; }
 };
 
 } // end namespace clang.

Modified: cfe/trunk/include/clang/Frontend/PCHWriter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHWriter.h?rev=69595&r1=69594&r2=69595&view=diff

==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHWriter.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHWriter.h Mon Apr 20 10:53:59 2009
@@ -34,6 +34,7 @@
 class ASTContext;
 class LabelStmt;
 class Preprocessor;
+class Sema;
 class SourceManager;
 class SwitchCase;
 class TargetInfo;
@@ -137,8 +138,8 @@
   /// the given bitstream.
   PCHWriter(llvm::BitstreamWriter &Stream);
   
-  /// \brief Write a precompiled header for the given AST context.
-  void WritePCH(ASTContext &Context, const Preprocessor &PP);
+  /// \brief Write a precompiled header for the given semantic analysis.
+  void WritePCH(Sema &SemaRef);
 
   /// \brief Emit a source location.
   void AddSourceLocation(SourceLocation Loc, RecordData &Record);

Added: cfe/trunk/include/clang/Sema/SemaConsumer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/SemaConsumer.h?rev=69595&view=auto

==============================================================================
--- cfe/trunk/include/clang/Sema/SemaConsumer.h (added)
+++ cfe/trunk/include/clang/Sema/SemaConsumer.h Mon Apr 20 10:53:59 2009
@@ -0,0 +1,45 @@
+//===--- SemaConsumer.h - Abstract interface for AST semantics --*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file defines the SemaConsumer class, a subclass of
+//  ASTConsumer that is used by AST clients that also require
+//  additional semantic analysis.
+//
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_CLANG_SEMA_SEMACONSUMER_H
+#define LLVM_CLANG_SEMA_SEMACONSUMER_H
+
+#include "clang/AST/ASTConsumer.h"
+
+namespace clang {
+  class Sema;
+
+  /// \brief An abstract interface that should be implemented by
+  /// clients that read ASTs and then require further semantic
+  /// analysis of the entities in those ASTs.
+  class SemaConsumer : public ASTConsumer {
+  public:
+    explicit SemaConsumer() {
+      ASTConsumer::SemaConsumer = true;
+    }
+
+    /// \brief Initialize the semantic consumer with the Sema instance
+    /// being used to perform semantic analysis on the abstract syntax
+    /// tree.
+    virtual void InitializeSema(Sema &S) {}
+
+    // isa/cast/dyn_cast support
+    static bool classof(const ASTConsumer *Consumer) { 
+      return Consumer->SemaConsumer; 
+    }
+    static bool classof(const SemaConsumer *) { return true; }
+  };
+}
+
+#endif

Propchange: cfe/trunk/include/clang/Sema/SemaConsumer.h

------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/include/clang/Sema/SemaConsumer.h

------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/include/clang/Sema/SemaConsumer.h

------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: cfe/trunk/lib/Frontend/PCHWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHWriter.cpp?rev=69595&r1=69594&r2=69595&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriter.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriter.cpp Mon Apr 20 10:53:59 2009
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/Frontend/PCHWriter.h"
+#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Decl.h"
 #include "clang/AST/DeclContextInternals.h"
@@ -1760,7 +1761,10 @@
 PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream) 
   : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS), NumStatements(0) { }
 
-void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) {
+void PCHWriter::WritePCH(Sema &SemaRef) {
+  ASTContext &Context = SemaRef.Context;
+  Preprocessor &PP = SemaRef.PP;
+
   // Emit the file header.
   Stream.Emit((unsigned)'C', 8);
   Stream.Emit((unsigned)'P', 8);

Modified: cfe/trunk/lib/Sema/ParseAST.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/ParseAST.cpp?rev=69595&r1=69594&r2=69595&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/ParseAST.cpp (original)
+++ cfe/trunk/lib/Sema/ParseAST.cpp Mon Apr 20 10:53:59 2009
@@ -13,6 +13,7 @@
 
 #include "clang/Sema/ParseAST.h"
 #include "Sema.h"
+#include "clang/Sema/SemaConsumer.h"
 #include "clang/AST/ASTConsumer.h"
 #include "clang/AST/ExternalASTSource.h"
 #include "clang/AST/Stmt.h"
@@ -46,6 +47,9 @@
   
   Consumer->Initialize(Ctx);
   
+  if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer))
+    SC->InitializeSema(S);
+
   if (Ctx.getExternalSource())
     Ctx.getExternalSource()->StartTranslationUnit(Consumer);
 

Modified: cfe/trunk/tools/clang-cc/GeneratePCH.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-cc/GeneratePCH.cpp?rev=69595&r1=69594&r2=69595&view=diff

==============================================================================
--- cfe/trunk/tools/clang-cc/GeneratePCH.cpp (original)
+++ cfe/trunk/tools/clang-cc/GeneratePCH.cpp Mon Apr 20 10:53:59 2009
@@ -14,6 +14,7 @@
 
 #include "ASTConsumers.h"
 #include "clang/Frontend/PCHWriter.h"
+#include "clang/Sema/SemaConsumer.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/ASTConsumer.h"
 #include "clang/Lex/Preprocessor.h"
@@ -28,14 +29,16 @@
 using namespace llvm;
 
 namespace {
-  class VISIBILITY_HIDDEN PCHGenerator : public ASTConsumer {
+  class VISIBILITY_HIDDEN PCHGenerator : public SemaConsumer {
     const Preprocessor &PP;
     std::string OutFile;
+    Sema *SemaPtr;
 
   public:
     explicit PCHGenerator(const Preprocessor &PP, const std::string &OutFile)
-      : PP(PP), OutFile(OutFile) { }
+      : PP(PP), OutFile(OutFile), SemaPtr(0) { }
 
+    virtual void InitializeSema(Sema &S) { SemaPtr = &S; }
     virtual void HandleTranslationUnit(ASTContext &Ctx);
   };
 }
@@ -50,7 +53,8 @@
   PCHWriter Writer(Stream);
 
   // Emit the PCH file
-  Writer.WritePCH(Ctx, PP);
+  assert(SemaPtr && "No Sema?");
+  Writer.WritePCH(*SemaPtr);
 
   // Open up the PCH file.
   std::string ErrMsg;





More information about the cfe-commits mailing list