BWAPI
Undermind/proxy/cpp/include/google/protobuf/compiler/importer.h
Go to the documentation of this file.
00001 // Protocol Buffers - Google's data interchange format
00002 // Copyright 2008 Google Inc.  All rights reserved.
00003 // http://code.google.com/p/protobuf/
00004 //
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are
00007 // met:
00008 //
00009 //     * Redistributions of source code must retain the above copyright
00010 // notice, this list of conditions and the following disclaimer.
00011 //     * Redistributions in binary form must reproduce the above
00012 // copyright notice, this list of conditions and the following disclaimer
00013 // in the documentation and/or other materials provided with the
00014 // distribution.
00015 //     * Neither the name of Google Inc. nor the names of its
00016 // contributors may be used to endorse or promote products derived from
00017 // this software without specific prior written permission.
00018 //
00019 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00022 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00023 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00025 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00026 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00027 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00029 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 
00031 // Author: kenton@google.com (Kenton Varda)
00032 //  Based on original Protocol Buffers design by
00033 //  Sanjay Ghemawat, Jeff Dean, and others.
00034 //
00035 // This file is the public interface to the .proto file parser.
00036 
00037 #ifndef GOOGLE_PROTOBUF_COMPILER_IMPORTER_H__
00038 #define GOOGLE_PROTOBUF_COMPILER_IMPORTER_H__
00039 
00040 #include <string>
00041 #include <vector>
00042 #include <set>
00043 #include <utility>
00044 #include <google/protobuf/descriptor.h>
00045 #include <google/protobuf/descriptor_database.h>
00046 #include <google/protobuf/compiler/parser.h>
00047 
00048 namespace google {
00049 namespace protobuf {
00050 
00051 namespace io { class ZeroCopyInputStream; }
00052 
00053 namespace compiler {
00054 
00055 // Defined in this file.
00056 class Importer;
00057 class MultiFileErrorCollector;
00058 class SourceTree;
00059 class DiskSourceTree;
00060 
00061 // TODO(kenton):  Move all SourceTree stuff to a separate file?
00062 
00063 // An implementation of DescriptorDatabase which loads files from a SourceTree
00064 // and parses them.
00065 //
00066 // Note:  This class is not thread-safe since it maintains a table of source
00067 //   code locations for error reporting.  However, when a DescriptorPool wraps
00068 //   a DescriptorDatabase, it uses mutex locking to make sure only one method
00069 //   of the database is called at a time, even if the DescriptorPool is used
00070 //   from multiple threads.  Therefore, there is only a problem if you create
00071 //   multiple DescriptorPools wrapping the same SourceTreeDescriptorDatabase
00072 //   and use them from multiple threads.
00073 //
00074 // Note:  This class does not implement FindFileContainingSymbol() or
00075 //   FindFileContainingExtension(); these will always return false.
00076 class LIBPROTOBUF_EXPORT SourceTreeDescriptorDatabase : public DescriptorDatabase {
00077  public:
00078   SourceTreeDescriptorDatabase(SourceTree* source_tree);
00079   ~SourceTreeDescriptorDatabase();
00080 
00081   // Instructs the SourceTreeDescriptorDatabase to report any parse errors
00082   // to the given MultiFileErrorCollector.  This should be called before
00083   // parsing.  error_collector must remain valid until either this method
00084   // is called again or the SourceTreeDescriptorDatabase is destroyed.
00085   void RecordErrorsTo(MultiFileErrorCollector* error_collector) {
00086     error_collector_ = error_collector;
00087   }
00088 
00089   // Gets a DescriptorPool::ErrorCollector which records errors to the
00090   // MultiFileErrorCollector specified with RecordErrorsTo().  This collector
00091   // has the ability to determine exact line and column numbers of errors
00092   // from the information given to it by the DescriptorPool.
00093   DescriptorPool::ErrorCollector* GetValidationErrorCollector() {
00094     using_validation_error_collector_ = true;
00095     return &validation_error_collector_;
00096   }
00097 
00098   // implements DescriptorDatabase -----------------------------------
00099   bool FindFileByName(const string& filename, FileDescriptorProto* output);
00100   bool FindFileContainingSymbol(const string& symbol_name,
00101                                 FileDescriptorProto* output);
00102   bool FindFileContainingExtension(const string& containing_type,
00103                                    int field_number,
00104                                    FileDescriptorProto* output);
00105 
00106  private:
00107   class SingleFileErrorCollector;
00108 
00109   SourceTree* source_tree_;
00110   MultiFileErrorCollector* error_collector_;
00111 
00112   class LIBPROTOBUF_EXPORT ValidationErrorCollector : public DescriptorPool::ErrorCollector {
00113    public:
00114     ValidationErrorCollector(SourceTreeDescriptorDatabase* owner);
00115     ~ValidationErrorCollector();
00116 
00117     // implements ErrorCollector ---------------------------------------
00118     void AddError(const string& filename,
00119                   const string& element_name,
00120                   const Message* descriptor,
00121                   ErrorLocation location,
00122                   const string& message);
00123 
00124    private:
00125     SourceTreeDescriptorDatabase* owner_;
00126   };
00127   friend class ValidationErrorCollector;
00128 
00129   bool using_validation_error_collector_;
00130   SourceLocationTable source_locations_;
00131   ValidationErrorCollector validation_error_collector_;
00132 };
00133 
00134 // Simple interface for parsing .proto files.  This wraps the process
00135 // of opening the file, parsing it with a Parser, recursively parsing all its
00136 // imports, and then cross-linking the results to produce a FileDescriptor.
00137 //
00138 // This is really just a thin wrapper around SourceTreeDescriptorDatabase.
00139 // You may find that SourceTreeDescriptorDatabase is more flexible.
00140 //
00141 // TODO(kenton):  I feel like this class is not well-named.
00142 class LIBPROTOBUF_EXPORT Importer {
00143  public:
00144   Importer(SourceTree* source_tree,
00145            MultiFileErrorCollector* error_collector);
00146   ~Importer();
00147 
00148   // Import the given file and build a FileDescriptor representing it.  If
00149   // the file is already in the DescriptorPool, the existing FileDescriptor
00150   // will be returned.  The FileDescriptor is property of the DescriptorPool,
00151   // and will remain valid until it is destroyed.  If any errors occur, they
00152   // will be reported using the error collector and Import() will return NULL.
00153   //
00154   // A particular Importer object will only report errors for a particular
00155   // file once.  All future attempts to import the same file will return NULL
00156   // without reporting any errors.  The idea is that you might want to import
00157   // a lot of files without seeing the same errors over and over again.  If
00158   // you want to see errors for the same files repeatedly, you can use a
00159   // separate Importer object to import each one (but use the same
00160   // DescriptorPool so that they can be cross-linked).
00161   const FileDescriptor* Import(const string& filename);
00162 
00163   // The DescriptorPool in which all imported FileDescriptors and their
00164   // contents are stored.
00165   inline const DescriptorPool* pool() const {
00166     return &pool_;
00167   }
00168 
00169  private:
00170   SourceTreeDescriptorDatabase database_;
00171   DescriptorPool pool_;
00172 
00173   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Importer);
00174 };
00175 
00176 // If the importer encounters problems while trying to import the proto files,
00177 // it reports them to a MultiFileErrorCollector.
00178 class LIBPROTOBUF_EXPORT MultiFileErrorCollector {
00179  public:
00180   inline MultiFileErrorCollector() {}
00181   virtual ~MultiFileErrorCollector();
00182 
00183   // Line and column numbers are zero-based.  A line number of -1 indicates
00184   // an error with the entire file (e.g. "not found").
00185   virtual void AddError(const string& filename, int line, int column,
00186                         const string& message) = 0;
00187 
00188  private:
00189   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MultiFileErrorCollector);
00190 };
00191 
00192 // Abstract interface which represents a directory tree containing proto files.
00193 // Used by the default implementation of Importer to resolve import statements
00194 // Most users will probably want to use the DiskSourceTree implementation,
00195 // below.
00196 class LIBPROTOBUF_EXPORT SourceTree {
00197  public:
00198   inline SourceTree() {}
00199   virtual ~SourceTree();
00200 
00201   // Open the given file and return a stream that reads it, or NULL if not
00202   // found.  The caller takes ownership of the returned object.  The filename
00203   // must be a path relative to the root of the source tree and must not
00204   // contain "." or ".." components.
00205   virtual io::ZeroCopyInputStream* Open(const string& filename) = 0;
00206 
00207  private:
00208   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SourceTree);
00209 };
00210 
00211 // An implementation of SourceTree which loads files from locations on disk.
00212 // Multiple mappings can be set up to map locations in the DiskSourceTree to
00213 // locations in the physical filesystem.
00214 class LIBPROTOBUF_EXPORT DiskSourceTree : public SourceTree {
00215  public:
00216   DiskSourceTree();
00217   ~DiskSourceTree();
00218 
00219   // Map a path on disk to a location in the SourceTree.  The path may be
00220   // either a file or a directory.  If it is a directory, the entire tree
00221   // under it will be mapped to the given virtual location.  To map a directory
00222   // to the root of the source tree, pass an empty string for virtual_path.
00223   //
00224   // If multiple mapped paths apply when opening a file, they will be searched
00225   // in order.  For example, if you do:
00226   //   MapPath("bar", "foo/bar");
00227   //   MapPath("", "baz");
00228   // and then you do:
00229   //   Open("bar/qux");
00230   // the DiskSourceTree will first try to open foo/bar/qux, then baz/bar/qux,
00231   // returning the first one that opens successfuly.
00232   //
00233   // disk_path may be an absolute path or relative to the current directory,
00234   // just like a path you'd pass to open().
00235   void MapPath(const string& virtual_path, const string& disk_path);
00236 
00237   // Return type for DiskFileToVirtualFile().
00238   enum DiskFileToVirtualFileResult {
00239     SUCCESS,
00240     SHADOWED,
00241     CANNOT_OPEN,
00242     NO_MAPPING
00243   };
00244 
00245   // Given a path to a file on disk, find a virtual path mapping to that
00246   // file.  The first mapping created with MapPath() whose disk_path contains
00247   // the filename is used.  However, that virtual path may not actually be
00248   // usable to open the given file.  Possible return values are:
00249   // * SUCCESS: The mapping was found.  *virtual_file is filled in so that
00250   //   calling Open(*virtual_file) will open the file named by disk_file.
00251   // * SHADOWED: A mapping was found, but using Open() to open this virtual
00252   //   path will end up returning some different file.  This is because some
00253   //   other mapping with a higher precedence also matches this virtual path
00254   //   and maps it to a different file that exists on disk.  *virtual_file
00255   //   is filled in as it would be in the SUCCESS case.  *shadowing_disk_file
00256   //   is filled in with the disk path of the file which would be opened if
00257   //   you were to call Open(*virtual_file).
00258   // * CANNOT_OPEN: The mapping was found and was not shadowed, but the
00259   //   file specified cannot be opened.  When this value is returned,
00260   //   errno will indicate the reason the file cannot be opened.  *virtual_file
00261   //   will be set to the virtual path as in the SUCCESS case, even though
00262   //   it is not useful.
00263   // * NO_MAPPING: Indicates that no mapping was found which contains this
00264   //   file.
00265   DiskFileToVirtualFileResult
00266     DiskFileToVirtualFile(const string& disk_file,
00267                           string* virtual_file,
00268                           string* shadowing_disk_file);
00269 
00270   // Given a virtual path, find the path to the file on disk.
00271   // Return true and update disk_file with the on-disk path if the file exists.
00272   // Return false and leave disk_file untouched if the file doesn't exist.
00273   bool VirtualFileToDiskFile(const string& virtual_file, string* disk_file);
00274 
00275   // implements SourceTree -------------------------------------------
00276   io::ZeroCopyInputStream* Open(const string& filename);
00277 
00278  private:
00279   struct Mapping {
00280     string virtual_path;
00281     string disk_path;
00282 
00283     inline Mapping(const string& virtual_path, const string& disk_path)
00284       : virtual_path(virtual_path), disk_path(disk_path) {}
00285   };
00286   vector<Mapping> mappings_;
00287 
00288   // Like Open(), but returns the on-disk path in disk_file if disk_file is
00289   // non-NULL and the file could be successfully opened.
00290   io::ZeroCopyInputStream* OpenVirtualFile(const string& virtual_file,
00291                                            string* disk_file);
00292 
00293   // Like Open() but given the actual on-disk path.
00294   io::ZeroCopyInputStream* OpenDiskFile(const string& filename);
00295 
00296   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DiskSourceTree);
00297 };
00298 
00299 }  // namespace compiler
00300 }  // namespace protobuf
00301 
00302 }  // namespace google
00303 #endif  // GOOGLE_PROTOBUF_COMPILER_IMPORTER_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines