• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "type_namespace.h"
18 
19 #include <algorithm>
20 #include <string>
21 #include <vector>
22 
23 #include "aidl_language.h"
24 #include "logging.h"
25 
26 using android::base::StringPrintf;
27 using std::string;
28 using std::vector;
29 
30 namespace android {
31 namespace aidl {
32 
33 // Since packages cannot contain '-' normally, we cannot be asked
34 // to create a type that conflicts with these strings.
35 const char kAidlReservedTypePackage[] = "aidl-internal";
36 const char kUtf8InCppStringClass[] = "Utf8InCppString";
37 
38 // These *must* match the package and class names above.
39 const char kUtf8InCppStringCanonicalName[] = "aidl-internal.Utf8InCppString";
40 
41 const char kStringCanonicalName[] = "java.lang.String";
42 
43 const char kUtf8InCppAnnotation[] = "@utfInCpp";
44 
45 namespace {
46 
is_java_keyword(const char * str)47 bool is_java_keyword(const char* str) {
48   static const std::vector<std::string> kJavaKeywords{
49       "abstract",   "assert",       "boolean",   "break",      "byte",
50       "case",       "catch",        "char",      "class",      "const",
51       "continue",   "default",      "do",        "double",     "else",
52       "enum",       "extends",      "final",     "finally",    "float",
53       "for",        "goto",         "if",        "implements", "import",
54       "instanceof", "int",          "interface", "long",       "native",
55       "new",        "package",      "private",   "protected",  "public",
56       "return",     "short",        "static",    "strictfp",   "super",
57       "switch",     "synchronized", "this",      "throw",      "throws",
58       "transient",  "try",          "void",      "volatile",   "while",
59       "true",       "false",        "null",
60   };
61   return std::find(kJavaKeywords.begin(), kJavaKeywords.end(), str) !=
62       kJavaKeywords.end();
63 }
64 
65 } // namespace
66 
ValidatableType(int kind,const string & package,const string & type_name,const string & decl_file,int decl_line)67 ValidatableType::ValidatableType(
68     int kind, const string& package, const string& type_name,
69     const string& decl_file, int decl_line)
70     : kind_(kind),
71       type_name_(type_name),
72       canonical_name_((package.empty()) ? type_name
73                                         : package + "." + type_name),
74       origin_file_(decl_file),
75       origin_line_(decl_line) {}
76 
HumanReadableKind() const77 string ValidatableType::HumanReadableKind() const {
78   switch (Kind()) {
79     case ValidatableType::KIND_BUILT_IN:
80       return "a built in";
81     case ValidatableType::KIND_PARCELABLE:
82       return "a parcelable";
83     case ValidatableType::KIND_INTERFACE:
84       return "an interface";
85     case ValidatableType::KIND_GENERATED:
86       return "a generated";
87   }
88   return "unknown";
89 }
90 
GetReturnType(const AidlTypeSpecifier & raw_type,const AidlDefinedType & context) const91 const ValidatableType* TypeNamespace::GetReturnType(const AidlTypeSpecifier& raw_type,
92                                                     const AidlDefinedType& context) const {
93   string error_msg;
94   const ValidatableType* return_type = GetValidatableType(raw_type, &error_msg, context);
95   if (return_type == nullptr) {
96     AIDL_ERROR(raw_type) << "Return type " << raw_type.ToString() << ": " << error_msg;
97     return nullptr;
98   }
99 
100   return return_type;
101 }
102 
AddDefinedTypes(vector<AidlDefinedType * > & types,const string & filename)103 bool TypeNamespace::AddDefinedTypes(vector<AidlDefinedType*>& types, const string& filename) {
104   bool success = true;
105   for (const auto type : types) {
106     const AidlInterface* interface = type->AsInterface();
107     if (interface != nullptr) {
108       success &= AddBinderType(*interface, filename);
109       continue;
110     }
111 
112     const AidlParcelable* parcelable = type->AsParcelable();
113     if (parcelable != nullptr) {
114       success &= AddParcelableType(*parcelable, filename);
115       continue;
116     }
117 
118     CHECK(false) << "aidl internal error: unrecognized type";
119   }
120   return success;
121 }
122 
GetArgType(const AidlArgument & a,int arg_index,const AidlDefinedType & context) const123 const ValidatableType* TypeNamespace::GetArgType(const AidlArgument& a, int arg_index,
124                                                  const AidlDefinedType& context) const {
125   string error_prefix =
126       StringPrintf("parameter %s (argument %d): ", a.GetName().c_str(), arg_index);
127 
128   // check the arg type
129   string error_msg;
130   const ValidatableType* t = GetValidatableType(a.GetType(), &error_msg, context);
131   if (t == nullptr) {
132     AIDL_ERROR(a) << error_prefix << error_msg;
133     return nullptr;
134   }
135 
136   const bool can_be_out = typenames_.CanBeOutParameter(a.GetType());
137   if (!a.DirectionWasSpecified() && can_be_out) {
138     AIDL_ERROR(a) << error_prefix << "'" << a.GetType().ToString()
139                   << "' can be an out type, so you must declare it as in, out, or inout.";
140     return nullptr;
141   }
142 
143   if (a.GetDirection() != AidlArgument::IN_DIR && !can_be_out) {
144     AIDL_ERROR(a) << error_prefix << "'" << a.ToString() << "' can only be an in parameter.";
145     return nullptr;
146   }
147 
148   // check that the name doesn't match a keyword
149   if (is_java_keyword(a.GetName().c_str())) {
150     AIDL_ERROR(a) << error_prefix << "Argument name is a Java or aidl keyword";
151     return nullptr;
152   }
153 
154   // Reserve a namespace for internal use
155   if (a.GetName().substr(0, 5)  == "_aidl") {
156     AIDL_ERROR(a) << error_prefix << "Argument name cannot begin with '_aidl'";
157     return nullptr;
158   }
159 
160   return t;
161 }
162 
163 }  // namespace aidl
164 }  // namespace android
165