blob: e8e5a29d88cdd03204e0c0d5481e96cee90d451d [file] [log] [blame]
Christopher Wileyfb4b22d2015-09-25 15:16:13 -07001/*
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
Casey Dahlin98a544b2015-10-14 14:22:55 -070019#include <algorithm>
Christopher Wileyfb4b22d2015-09-25 15:16:13 -070020#include <string>
Christopher Wileycd8e8972015-10-26 10:24:35 -070021#include <vector>
Christopher Wileyfb4b22d2015-09-25 15:16:13 -070022
Christopher Wileyfb4b22d2015-09-25 15:16:13 -070023#include "aidl_language.h"
Christopher Wileycd8e8972015-10-26 10:24:35 -070024#include "logging.h"
Christopher Wileyfb4b22d2015-09-25 15:16:13 -070025
26using android::base::StringPrintf;
Christopher Wileyfb4b22d2015-09-25 15:16:13 -070027using std::string;
Christopher Wileycd8e8972015-10-26 10:24:35 -070028using std::vector;
Christopher Wileyfb4b22d2015-09-25 15:16:13 -070029
30namespace android {
31namespace aidl {
32
Christopher Wiley9f403722016-01-27 16:04:11 -080033// Since packages cannot contain '-' normally, we cannot be asked
34// to create a type that conflicts with these strings.
35const char kAidlReservedTypePackage[] = "aidl-internal";
Christopher Wiley9f403722016-01-27 16:04:11 -080036const char kUtf8InCppStringClass[] = "Utf8InCppString";
37
38// These *must* match the package and class names above.
Christopher Wiley9f403722016-01-27 16:04:11 -080039const char kUtf8InCppStringCanonicalName[] = "aidl-internal.Utf8InCppString";
40
Christopher Wiley5d9bc932016-02-01 13:23:16 -080041const char kStringCanonicalName[] = "java.lang.String";
42
Christopher Wiley9f403722016-01-27 16:04:11 -080043const char kUtf8InCppAnnotation[] = "@utfInCpp";
44
Casey Dahlin98a544b2015-10-14 14:22:55 -070045namespace {
46
47bool 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
Christopher Wiley09af4692015-10-30 11:48:25 -070067ValidatableType::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
77string 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
Jiyong Parkd59a10d2018-07-18 11:12:55 +090091const ValidatableType* TypeNamespace::GetReturnType(const AidlTypeSpecifier& raw_type,
Steven Moreland5557f1c2018-07-02 13:50:23 -070092 const AidlDefinedType& context) const {
Christopher Wileye22b41a2016-01-25 14:51:17 -080093 string error_msg;
Steven Moreland5557f1c2018-07-02 13:50:23 -070094 const ValidatableType* return_type = GetValidatableType(raw_type, &error_msg, context);
Christopher Wileyfb4b22d2015-09-25 15:16:13 -070095 if (return_type == nullptr) {
Steven Moreland92c55f12018-07-31 14:08:37 -070096 AIDL_ERROR(raw_type) << "Return type " << raw_type.ToString() << ": " << error_msg;
Casey Dahlin57dbe242015-12-04 11:44:02 -080097 return nullptr;
98 }
99
100 return return_type;
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700101}
102
Jiyong Parkb034bf02018-07-30 17:44:33 +0900103bool 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
Steven Moreland5557f1c2018-07-02 13:50:23 -0700123const ValidatableType* TypeNamespace::GetArgType(const AidlArgument& a, int arg_index,
Steven Moreland5557f1c2018-07-02 13:50:23 -0700124 const AidlDefinedType& context) const {
Steven Moreland92c55f12018-07-31 14:08:37 -0700125 string error_prefix =
126 StringPrintf("parameter %s (argument %d): ", a.GetName().c_str(), arg_index);
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700127
128 // check the arg type
Christopher Wileye22b41a2016-01-25 14:51:17 -0800129 string error_msg;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700130 const ValidatableType* t = GetValidatableType(a.GetType(), &error_msg, context);
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700131 if (t == nullptr) {
Steven Moreland92c55f12018-07-31 14:08:37 -0700132 AIDL_ERROR(a) << error_prefix << error_msg;
Casey Dahlin57dbe242015-12-04 11:44:02 -0800133 return nullptr;
134 }
135
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900136 const bool can_be_out = typenames_.CanBeOutParameter(a.GetType());
137 if (!a.DirectionWasSpecified() && can_be_out) {
Steven Moreland92c55f12018-07-31 14:08:37 -0700138 AIDL_ERROR(a) << error_prefix << "'" << a.GetType().ToString()
139 << "' can be an out type, so you must declare it as in, out, or inout.";
Casey Dahlin57dbe242015-12-04 11:44:02 -0800140 return nullptr;
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700141 }
142
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900143 if (a.GetDirection() != AidlArgument::IN_DIR && !can_be_out) {
Steven Moreland92c55f12018-07-31 14:08:37 -0700144 AIDL_ERROR(a) << error_prefix << "'" << a.ToString() << "' can only be an in parameter.";
Casey Dahlin57dbe242015-12-04 11:44:02 -0800145 return nullptr;
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700146 }
147
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700148 // check that the name doesn't match a keyword
Casey Dahlind127b502015-09-30 12:51:08 -0700149 if (is_java_keyword(a.GetName().c_str())) {
Steven Moreland92c55f12018-07-31 14:08:37 -0700150 AIDL_ERROR(a) << error_prefix << "Argument name is a Java or aidl keyword";
Casey Dahlin57dbe242015-12-04 11:44:02 -0800151 return nullptr;
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700152 }
153
154 // Reserve a namespace for internal use
Casey Dahlind127b502015-09-30 12:51:08 -0700155 if (a.GetName().substr(0, 5) == "_aidl") {
Steven Moreland92c55f12018-07-31 14:08:37 -0700156 AIDL_ERROR(a) << error_prefix << "Argument name cannot begin with '_aidl'";
Casey Dahlin57dbe242015-12-04 11:44:02 -0800157 return nullptr;
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700158 }
159
Casey Dahlin57dbe242015-12-04 11:44:02 -0800160 return t;
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700161}
162
163} // namespace aidl
164} // namespace android