blob: d0343e9978b358e874512ba5d2bff8bb5755ad7d [file] [log] [blame]
nnobleebebb7e2014-12-10 16:31:01 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
nnobleebebb7e2014-12-10 16:31:01 -08004 * 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" Noble1ff52d52015-03-01 05:24:36 +010034#ifndef GRPC_INTERNAL_COMPILER_CPP_GENERATOR_H
35#define GRPC_INTERNAL_COMPILER_CPP_GENERATOR_H
nnobleebebb7e2014-12-10 16:31:01 -080036
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -080037// 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-gaea106e2016-08-30 10:44:06 -070044#include "src/compiler/config.h"
45
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -080046#ifndef GRPC_CUSTOM_STRING
47#include <string>
48#define GRPC_CUSTOM_STRING std::string
49#endif
50
51namespace grpc {
52
53typedef GRPC_CUSTOM_STRING string;
54
55} // namespace grpc
nnobleebebb7e2014-12-10 16:31:01 -080056
nnobleebebb7e2014-12-10 16:31:01 -080057namespace grpc_cpp_generator {
58
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +010059// Contains all the parameters that are parsed from the command line.
60struct Parameters {
61 // Puts the service into a namespace
62 grpc::string services_namespace;
Nicolas "Pixel" Noble931bdce2016-01-12 03:08:11 +010063 // 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" Noble375a82b2015-03-24 02:33:18 +010067};
68
yang-g9efec8e2016-04-14 14:34:55 -070069// A common interface for objects having comments in the source.
yang-g25df28e2016-04-20 16:36:12 -070070// Return formatted comments to be inserted in generated code.
yang-g9efec8e2016-04-14 14:34:55 -070071struct CommentHolder {
72 virtual ~CommentHolder() {}
73 virtual grpc::string GetLeadingComments() const = 0;
74 virtual grpc::string GetTrailingComments() const = 0;
75};
76
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -080077// An abstract interface representing a method.
yang-g9efec8e2016-04-14 14:34:55 -070078struct Method : public CommentHolder {
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -080079 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-g9efec8e2016-04-14 14:34:55 -070093struct Service : public CommentHolder {
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -080094 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
102struct 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-g9efec8e2016-04-14 14:34:55 -0700114struct File : public CommentHolder {
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800115 virtual ~File() {}
116
117 virtual grpc::string filename() const = 0;
118 virtual grpc::string filename_without_ext() const = 0;
Wouter van Oortmerssenb695b9b2016-04-13 18:18:08 -0700119 virtual grpc::string message_header_ext() const = 0;
120 virtual grpc::string service_header_ext() const = 0;
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800121 virtual grpc::string package() const = 0;
122 virtual std::vector<grpc::string> package_parts() const = 0;
Wouter van Oortmerssenb695b9b2016-04-13 18:18:08 -0700123 virtual grpc::string additional_headers() const = 0;
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800124
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" Noble0caebbf2015-04-09 23:08:51 +0200131// Return the prologue of the generated header file.
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800132grpc::string GetHeaderPrologue(File *file, const Parameters &params);
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200133
nnobleebebb7e2014-12-10 16:31:01 -0800134// Return the includes needed for generated header file.
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800135grpc::string GetHeaderIncludes(File *file, const Parameters &params);
nnobleebebb7e2014-12-10 16:31:01 -0800136
137// Return the includes needed for generated source file.
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800138grpc::string GetSourceIncludes(File *file, const Parameters &params);
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200139
140// Return the epilogue of the generated header file.
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800141grpc::string GetHeaderEpilogue(File *file, const Parameters &params);
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200142
143// Return the prologue of the generated source file.
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800144grpc::string GetSourcePrologue(File *file, const Parameters &params);
nnobleebebb7e2014-12-10 16:31:01 -0800145
146// Return the services for generated header file.
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800147grpc::string GetHeaderServices(File *file, const Parameters &params);
nnobleebebb7e2014-12-10 16:31:01 -0800148
149// Return the services for generated source file.
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800150grpc::string GetSourceServices(File *file, const Parameters &params);
nnobleebebb7e2014-12-10 16:31:01 -0800151
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200152// Return the epilogue of the generated source file.
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800153grpc::string GetSourceEpilogue(File *file, const Parameters &params);
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200154
nnobleebebb7e2014-12-10 16:31:01 -0800155} // namespace grpc_cpp_generator
156
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +0100157#endif // GRPC_INTERNAL_COMPILER_CPP_GENERATOR_H