Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 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 | |
Casey Dahlin | 98a544b | 2015-10-14 14:22:55 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 20 | #include <string> |
Christopher Wiley | cd8e897 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 21 | #include <vector> |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 22 | |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 23 | #include "aidl_language.h" |
Christopher Wiley | cd8e897 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 24 | #include "logging.h" |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 25 | |
| 26 | using android::base::StringPrintf; |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 27 | using std::string; |
Christopher Wiley | cd8e897 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 28 | using std::vector; |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | namespace aidl { |
| 32 | |
Christopher Wiley | 9f40372 | 2016-01-27 16:04:11 -0800 | [diff] [blame] | 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"; |
Christopher Wiley | 9f40372 | 2016-01-27 16:04:11 -0800 | [diff] [blame] | 36 | const char kUtf8InCppStringClass[] = "Utf8InCppString"; |
| 37 | |
| 38 | // These *must* match the package and class names above. |
Christopher Wiley | 9f40372 | 2016-01-27 16:04:11 -0800 | [diff] [blame] | 39 | const char kUtf8InCppStringCanonicalName[] = "aidl-internal.Utf8InCppString"; |
| 40 | |
Christopher Wiley | 5d9bc93 | 2016-02-01 13:23:16 -0800 | [diff] [blame] | 41 | const char kStringCanonicalName[] = "java.lang.String"; |
| 42 | |
Christopher Wiley | 9f40372 | 2016-01-27 16:04:11 -0800 | [diff] [blame] | 43 | const char kUtf8InCppAnnotation[] = "@utfInCpp"; |
| 44 | |
Casey Dahlin | 98a544b | 2015-10-14 14:22:55 -0700 | [diff] [blame] | 45 | namespace { |
| 46 | |
| 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 | |
Christopher Wiley | 09af469 | 2015-10-30 11:48:25 -0700 | [diff] [blame] | 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 | |
| 77 | 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 | |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 91 | const ValidatableType* TypeNamespace::GetReturnType(const AidlTypeSpecifier& raw_type, |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 92 | const AidlDefinedType& context) const { |
Christopher Wiley | e22b41a | 2016-01-25 14:51:17 -0800 | [diff] [blame] | 93 | string error_msg; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 94 | const ValidatableType* return_type = GetValidatableType(raw_type, &error_msg, context); |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 95 | if (return_type == nullptr) { |
Steven Moreland | 92c55f1 | 2018-07-31 14:08:37 -0700 | [diff] [blame] | 96 | AIDL_ERROR(raw_type) << "Return type " << raw_type.ToString() << ": " << error_msg; |
Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 97 | return nullptr; |
| 98 | } |
| 99 | |
| 100 | return return_type; |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 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 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 123 | const ValidatableType* TypeNamespace::GetArgType(const AidlArgument& a, int arg_index, |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 124 | const AidlDefinedType& context) const { |
Steven Moreland | 92c55f1 | 2018-07-31 14:08:37 -0700 | [diff] [blame] | 125 | string error_prefix = |
| 126 | StringPrintf("parameter %s (argument %d): ", a.GetName().c_str(), arg_index); |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 127 | |
| 128 | // check the arg type |
Christopher Wiley | e22b41a | 2016-01-25 14:51:17 -0800 | [diff] [blame] | 129 | string error_msg; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 130 | const ValidatableType* t = GetValidatableType(a.GetType(), &error_msg, context); |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 131 | if (t == nullptr) { |
Steven Moreland | 92c55f1 | 2018-07-31 14:08:37 -0700 | [diff] [blame] | 132 | AIDL_ERROR(a) << error_prefix << error_msg; |
Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 133 | return nullptr; |
| 134 | } |
| 135 | |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 136 | const bool can_be_out = typenames_.CanBeOutParameter(a.GetType()); |
| 137 | if (!a.DirectionWasSpecified() && can_be_out) { |
Steven Moreland | 92c55f1 | 2018-07-31 14:08:37 -0700 | [diff] [blame] | 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."; |
Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 140 | return nullptr; |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 143 | if (a.GetDirection() != AidlArgument::IN_DIR && !can_be_out) { |
Steven Moreland | 92c55f1 | 2018-07-31 14:08:37 -0700 | [diff] [blame] | 144 | AIDL_ERROR(a) << error_prefix << "'" << a.ToString() << "' can only be an in parameter."; |
Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 145 | return nullptr; |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 148 | // check that the name doesn't match a keyword |
Casey Dahlin | d127b50 | 2015-09-30 12:51:08 -0700 | [diff] [blame] | 149 | if (is_java_keyword(a.GetName().c_str())) { |
Steven Moreland | 92c55f1 | 2018-07-31 14:08:37 -0700 | [diff] [blame] | 150 | AIDL_ERROR(a) << error_prefix << "Argument name is a Java or aidl keyword"; |
Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 151 | return nullptr; |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // Reserve a namespace for internal use |
Casey Dahlin | d127b50 | 2015-09-30 12:51:08 -0700 | [diff] [blame] | 155 | if (a.GetName().substr(0, 5) == "_aidl") { |
Steven Moreland | 92c55f1 | 2018-07-31 14:08:37 -0700 | [diff] [blame] | 156 | AIDL_ERROR(a) << error_prefix << "Argument name cannot begin with '_aidl'"; |
Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 157 | return nullptr; |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 160 | return t; |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | } // namespace aidl |
| 164 | } // namespace android |