1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 #include "google/protobuf/compiler/php/names.h"
9
10 #include <algorithm>
11 #include <string>
12
13 #include "absl/strings/ascii.h"
14 #include "absl/strings/string_view.h"
15 #include "google/protobuf/descriptor.h"
16 #include "google/protobuf/descriptor.pb.h"
17
18 const char* const kReservedNames[] = {
19 "abstract", "and", "array", "as", "break",
20 "callable", "case", "catch", "class", "clone",
21 "const", "continue", "declare", "default", "die",
22 "do", "echo", "else", "elseif", "empty",
23 "enddeclare", "endfor", "endforeach", "endif", "endswitch",
24 "endwhile", "eval", "exit", "extends", "final",
25 "finally", "fn", "for", "foreach", "function",
26 "global", "goto", "if", "implements", "include",
27 "include_once", "instanceof", "insteadof", "interface", "isset",
28 "list", "match", "namespace", "new", "or",
29 "parent", "print", "private", "protected", "public",
30 "readonly", "require", "require_once", "return", "self",
31 "static", "switch", "throw", "trait", "try",
32 "unset", "use", "var", "while", "xor",
33 "yield", "int", "float", "bool", "string",
34 "true", "false", "null", "void", "iterable"};
35 const int kReservedNamesSize = 80;
36
37 namespace google {
38 namespace protobuf {
39 namespace compiler {
40 namespace php {
41
IsReservedName(absl::string_view name)42 bool IsReservedName(absl::string_view name) {
43 std::string lower = absl::AsciiStrToLower(name);
44 for (int i = 0; i < kReservedNamesSize; i++) {
45 if (lower == kReservedNames[i]) {
46 return true;
47 }
48 }
49 return false;
50 }
51
ReservedNamePrefix(const absl::string_view classname,const FileDescriptor * file)52 std::string ReservedNamePrefix(const absl::string_view classname,
53 const FileDescriptor* file) {
54 if (IsReservedName(classname)) {
55 if (file->package() == "google.protobuf") {
56 return "GPB";
57 } else {
58 return "PB";
59 }
60 }
61
62 return "";
63 }
64
65 namespace {
66
67 template <typename DescriptorType>
ClassNamePrefixImpl(const absl::string_view classname,const DescriptorType * desc)68 std::string ClassNamePrefixImpl(const absl::string_view classname,
69 const DescriptorType* desc) {
70 const std::string& prefix = (desc->file()->options()).php_class_prefix();
71 if (!prefix.empty()) {
72 return prefix;
73 }
74
75 return ReservedNamePrefix(classname, desc->file());
76 }
77
78 template <typename DescriptorType>
GeneratedClassNameImpl(const DescriptorType * desc)79 std::string GeneratedClassNameImpl(const DescriptorType* desc) {
80 std::string classname =
81 absl::StrCat(ClassNamePrefixImpl(desc->name(), desc), desc->name());
82 const Descriptor* containing = desc->containing_type();
83 while (containing != nullptr) {
84 classname = absl::StrCat(ClassNamePrefixImpl(containing->name(), desc),
85 containing->name(), "\\", classname);
86 containing = containing->containing_type();
87 }
88 return classname;
89 }
90
GeneratedClassNameImpl(const ServiceDescriptor * desc)91 std::string GeneratedClassNameImpl(const ServiceDescriptor* desc) {
92 const absl::string_view classname = desc->name();
93 return absl::StrCat(ClassNamePrefixImpl(classname, desc), classname);
94 }
95
96 } // namespace
97
ClassNamePrefix(const std::string & classname,const Descriptor * desc)98 std::string ClassNamePrefix(const std::string& classname,
99 const Descriptor* desc) {
100 return ClassNamePrefixImpl(classname, desc);
101 }
ClassNamePrefix(const std::string & classname,const EnumDescriptor * desc)102 std::string ClassNamePrefix(const std::string& classname,
103 const EnumDescriptor* desc) {
104 return ClassNamePrefixImpl(classname, desc);
105 }
106
GeneratedClassName(const Descriptor * desc)107 std::string GeneratedClassName(const Descriptor* desc) {
108 return GeneratedClassNameImpl(desc);
109 }
110
GeneratedClassName(const EnumDescriptor * desc)111 std::string GeneratedClassName(const EnumDescriptor* desc) {
112 return GeneratedClassNameImpl(desc);
113 }
114
GeneratedClassName(const ServiceDescriptor * desc)115 std::string GeneratedClassName(const ServiceDescriptor* desc) {
116 return GeneratedClassNameImpl(desc);
117 }
118
119 } // namespace php
120 } // namespace compiler
121 } // namespace protobuf
122 } // namespace google
123