nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 34 | #ifndef GRPC_INTERNAL_COMPILER_CPP_GENERATOR_H |
| 35 | #define GRPC_INTERNAL_COMPILER_CPP_GENERATOR_H |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 36 | |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 37 | // cpp_generator.h/.cc do not directly depend on GRPC/ProtoBuf, such that they |
| 38 | // can be used to generate code for other serialization systems, such as |
| 39 | // FlatBuffers. |
| 40 | |
| 41 | #include <memory> |
| 42 | #include <vector> |
| 43 | |
yang-g | aea106e | 2016-08-30 10:44:06 -0700 | [diff] [blame] | 44 | #include "src/compiler/config.h" |
| 45 | |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 46 | #ifndef GRPC_CUSTOM_STRING |
| 47 | #include <string> |
| 48 | #define GRPC_CUSTOM_STRING std::string |
| 49 | #endif |
| 50 | |
| 51 | namespace grpc { |
| 52 | |
| 53 | typedef GRPC_CUSTOM_STRING string; |
| 54 | |
| 55 | } // namespace grpc |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 56 | |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 57 | namespace grpc_cpp_generator { |
| 58 | |
Nicolas "Pixel" Noble | 375a82b | 2015-03-24 02:33:18 +0100 | [diff] [blame] | 59 | // Contains all the parameters that are parsed from the command line. |
| 60 | struct Parameters { |
| 61 | // Puts the service into a namespace |
| 62 | grpc::string services_namespace; |
Nicolas "Pixel" Noble | 931bdce | 2016-01-12 03:08:11 +0100 | [diff] [blame] | 63 | // Use system includes (<>) or local includes ("") |
| 64 | bool use_system_headers; |
| 65 | // Prefix to any grpc include |
| 66 | grpc::string grpc_search_path; |
Nicolas "Pixel" Noble | 375a82b | 2015-03-24 02:33:18 +0100 | [diff] [blame] | 67 | }; |
| 68 | |
yang-g | 9efec8e | 2016-04-14 14:34:55 -0700 | [diff] [blame] | 69 | // A common interface for objects having comments in the source. |
yang-g | 25df28e | 2016-04-20 16:36:12 -0700 | [diff] [blame] | 70 | // Return formatted comments to be inserted in generated code. |
yang-g | 9efec8e | 2016-04-14 14:34:55 -0700 | [diff] [blame] | 71 | struct CommentHolder { |
| 72 | virtual ~CommentHolder() {} |
| 73 | virtual grpc::string GetLeadingComments() const = 0; |
| 74 | virtual grpc::string GetTrailingComments() const = 0; |
| 75 | }; |
| 76 | |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 77 | // An abstract interface representing a method. |
yang-g | 9efec8e | 2016-04-14 14:34:55 -0700 | [diff] [blame] | 78 | struct Method : public CommentHolder { |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 79 | virtual ~Method() {} |
| 80 | |
| 81 | virtual grpc::string name() const = 0; |
| 82 | |
| 83 | virtual grpc::string input_type_name() const = 0; |
| 84 | virtual grpc::string output_type_name() const = 0; |
| 85 | |
| 86 | virtual bool NoStreaming() const = 0; |
| 87 | virtual bool ClientOnlyStreaming() const = 0; |
| 88 | virtual bool ServerOnlyStreaming() const = 0; |
| 89 | virtual bool BidiStreaming() const = 0; |
| 90 | }; |
| 91 | |
| 92 | // An abstract interface representing a service. |
yang-g | 9efec8e | 2016-04-14 14:34:55 -0700 | [diff] [blame] | 93 | struct Service : public CommentHolder { |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 94 | virtual ~Service() {} |
| 95 | |
| 96 | virtual grpc::string name() const = 0; |
| 97 | |
| 98 | virtual int method_count() const = 0; |
| 99 | virtual std::unique_ptr<const Method> method(int i) const = 0; |
| 100 | }; |
| 101 | |
| 102 | struct Printer { |
| 103 | virtual ~Printer() {} |
| 104 | |
| 105 | virtual void Print(const std::map<grpc::string, grpc::string> &vars, |
| 106 | const char *template_string) = 0; |
| 107 | virtual void Print(const char *string) = 0; |
| 108 | virtual void Indent() = 0; |
| 109 | virtual void Outdent() = 0; |
| 110 | }; |
| 111 | |
| 112 | // An interface that allows the source generated to be output using various |
| 113 | // libraries/idls/serializers. |
yang-g | 9efec8e | 2016-04-14 14:34:55 -0700 | [diff] [blame] | 114 | struct File : public CommentHolder { |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 115 | virtual ~File() {} |
| 116 | |
| 117 | virtual grpc::string filename() const = 0; |
| 118 | virtual grpc::string filename_without_ext() const = 0; |
Wouter van Oortmerssen | b695b9b | 2016-04-13 18:18:08 -0700 | [diff] [blame] | 119 | virtual grpc::string message_header_ext() const = 0; |
| 120 | virtual grpc::string service_header_ext() const = 0; |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 121 | virtual grpc::string package() const = 0; |
| 122 | virtual std::vector<grpc::string> package_parts() const = 0; |
Wouter van Oortmerssen | b695b9b | 2016-04-13 18:18:08 -0700 | [diff] [blame] | 123 | virtual grpc::string additional_headers() const = 0; |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 124 | |
| 125 | virtual int service_count() const = 0; |
| 126 | virtual std::unique_ptr<const Service> service(int i) const = 0; |
| 127 | |
| 128 | virtual std::unique_ptr<Printer> CreatePrinter(grpc::string *str) const = 0; |
| 129 | }; |
| 130 | |
Nicolas "Pixel" Noble | 0caebbf | 2015-04-09 23:08:51 +0200 | [diff] [blame] | 131 | // Return the prologue of the generated header file. |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 132 | grpc::string GetHeaderPrologue(File *file, const Parameters ¶ms); |
Nicolas "Pixel" Noble | 0caebbf | 2015-04-09 23:08:51 +0200 | [diff] [blame] | 133 | |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 134 | // Return the includes needed for generated header file. |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 135 | grpc::string GetHeaderIncludes(File *file, const Parameters ¶ms); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 136 | |
| 137 | // Return the includes needed for generated source file. |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 138 | grpc::string GetSourceIncludes(File *file, const Parameters ¶ms); |
Nicolas "Pixel" Noble | 0caebbf | 2015-04-09 23:08:51 +0200 | [diff] [blame] | 139 | |
| 140 | // Return the epilogue of the generated header file. |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 141 | grpc::string GetHeaderEpilogue(File *file, const Parameters ¶ms); |
Nicolas "Pixel" Noble | 0caebbf | 2015-04-09 23:08:51 +0200 | [diff] [blame] | 142 | |
| 143 | // Return the prologue of the generated source file. |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 144 | grpc::string GetSourcePrologue(File *file, const Parameters ¶ms); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 145 | |
| 146 | // Return the services for generated header file. |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 147 | grpc::string GetHeaderServices(File *file, const Parameters ¶ms); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 148 | |
| 149 | // Return the services for generated source file. |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 150 | grpc::string GetSourceServices(File *file, const Parameters ¶ms); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 151 | |
Nicolas "Pixel" Noble | 0caebbf | 2015-04-09 23:08:51 +0200 | [diff] [blame] | 152 | // Return the epilogue of the generated source file. |
Wouter van Oortmerssen | af09a73 | 2016-03-09 17:03:21 -0800 | [diff] [blame] | 153 | grpc::string GetSourceEpilogue(File *file, const Parameters ¶ms); |
Nicolas "Pixel" Noble | 0caebbf | 2015-04-09 23:08:51 +0200 | [diff] [blame] | 154 | |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 155 | } // namespace grpc_cpp_generator |
| 156 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 157 | #endif // GRPC_INTERNAL_COMPILER_CPP_GENERATOR_H |