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