summaryrefslogtreecommitdiffstats
path: root/type_namespace.cpp
blob: e6dc81b97a140fd676f08cb90d5b5a64884c8eb7 (plain)
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
/*
 * 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 <iostream>
#include <string>

#include <base/stringprintf.h>

#include "aidl_language.h"
#include "parse_helpers.h"

using android::base::StringPrintf;
using std::cerr;
using std::endl;
using std::string;

namespace android {
namespace aidl {

bool TypeNamespace::HasType(const string& type_name) const {
  return GetValidatableType(type_name) != nullptr;
}

bool TypeNamespace::IsValidReturnType(const AidlType& raw_type,
                                      const string& filename) const {
  const string error_prefix = StringPrintf(
      "In file %s line %d return type %s:\n    ",
      filename.c_str(), raw_type.GetLine(), raw_type.ToString().c_str());

  const ValidatableType* return_type = GetValidatableType(raw_type.GetName());
  if (return_type == nullptr) {
    cerr << error_prefix << "unknown return type" << endl;
    return false;
  }

  if (!return_type->CanWriteToParcel()) {
    cerr << error_prefix << "return type cannot be marshalled" << endl;
    return false;
  }

  if (raw_type.IsArray() && !return_type->CanBeArray()) {
    cerr << error_prefix << "return type cannot be an array" << endl;
    return false;
  }

  return true;
}

bool TypeNamespace::IsValidArg(const AidlArgument& a,
                               int arg_index,
                               const string& filename) const {
  string error_prefix = StringPrintf(
      "In file %s line %d parameter %s (%d):\n    ",
      filename.c_str(), a.GetLine(), a.GetName().c_str(), arg_index);

  // check the arg type
  const ValidatableType* t = GetValidatableType(a.GetType().GetName());
  if (t == nullptr) {
    cerr << error_prefix << "unknown type " << a.GetType().GetName().c_str() << endl;
    return false;
  }

  if (!t->CanWriteToParcel()) {
    cerr << error_prefix
         << StringPrintf("'%s %s' can't be marshalled.",
                         a.GetType().GetName().c_str(), a.GetName().c_str()) << endl;
    return false;
  }

  if (!a.DirectionWasSpecified() &&
      (a.GetType().IsArray() || t->CanBeOutParameter())) {
    cerr << error_prefix << StringPrintf(
        "'%s %s' can be an out parameter, so you must declare it as in,"
        " out or inout.", a.GetType().GetName().c_str(), a.GetName().c_str()) << endl;
    return false;
  }

  if (a.GetDirection() != AidlArgument::IN_DIR &&
      !t->CanBeOutParameter() &&
      !a.GetType().IsArray()) {
    cerr << error_prefix << StringPrintf(
        "'%s' can only be an in parameter.",
        a.ToString().c_str()) << endl;
    return false;
  }

  if (a.GetType().IsArray() && !t->CanBeArray()) {
    cerr << error_prefix << StringPrintf(
        "'%s' cannot be an array.",
        a.ToString().c_str()) << endl;
    return false;
  }

  // check that the name doesn't match a keyword
  if (is_java_keyword(a.GetName().c_str())) {
    cerr << error_prefix << "Argument name is a C++, Java, or aidl keyword"
         << endl;
    return false;
  }

  // Reserve a namespace for internal use
  if (a.GetName().substr(0, 5)  == "_aidl") {
    cerr << error_prefix << "Argument name cannot begin with '_aidl'"
         << endl;
    return false;
  }

  return true;
}

}  // namespace aidl
}  // namespace android