Debian Codegraph @

native/common/jp_methoddispatch.cpp

raw symbols SCIP
1 /*****************************************************************************
2 Licensed under the Apache License, Version 2.0 (the "License");
3 you may not use this file except in compliance with the License.
4 You may obtain a copy of the License at
5
6 http://www.apache.org/licenses/LICENSE-2.0
7
8 Unless required by applicable law or agreed to in writing, software
9 distributed under the License is distributed on an "AS IS" BASIS,
10 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 See the License for the specific language governing permissions and
12 limitations under the License.
13
14 See NOTICE file for details.
15 *****************************************************************************/
16 #include <algorithm>
17 #include <utility>
18 #include "jpype.h"
19 #include "jp_method.h"
20 #include "jp_methoddispatch.h"
21
22 JPMethodDispatch::JPMethodDispatch(JPClass* clazz,
23 const string& name,
24 JPMethodList& overloads,
25 jint modifiers)
26 : m_Name(name)
27 {
28 m_Class = clazz;
29 m_Overloads = overloads;
30 m_Modifiers = modifiers;
31 m_LastCache.m_Hash = -1;
32 }
33
34 JPMethodDispatch::~JPMethodDispatch()
35 = default;
36
37 const string& JPMethodDispatch::getName() const
38 {
39 return m_Name;
40 }
41
42 bool JPMethodDispatch::findOverload(JPJavaFrame& frame, JPMethodMatch &bestMatch, JPPyObjectVector& arg,
43 bool callInstance, bool raise)
44 {
45 JP_TRACE_IN("JPMethodDispatch::findOverload");
46 JP_TRACE("Checking overload", m_Name);
47 JP_TRACE("Got overloads to check", m_Overloads.size());
48 JPMethodList ambiguous;
49
50 // Check cache to see if we already resolved this overload.
51 // First we need to see if the hash matches for the last set of arguments.
52 // Then make sure we don't hit the rare case that the hash was -1 by chance.
53 // Then make sure it isn't variadic list match, as the hash of an opaque list
54 // element can't be resolved without going through the resolution process.
55 if (m_LastCache.m_Hash == bestMatch.m_Hash && m_LastCache.m_Overload != nullptr
56 && !m_LastCache.m_Overload->isVarArgs())
57 {
58 bestMatch.m_Overload = m_LastCache.m_Overload;
59 bestMatch.m_Overload->matches(frame, bestMatch, callInstance, arg);
60
61 // Anything better than explicit constitutes a hit on the cache
62 if (bestMatch.m_Type > JPMatch::_explicit)
63 return true;
64 else
65 // bad match so forget the overload.
66 bestMatch.m_Overload = nullptr;
67 }
68
69 // We need two copies of the match. One to hold the best match we have
70 // found, and one to hold the test of the next overload.
71 JPMethodMatch match = bestMatch;
72
73 // Check each overload in order (the TypeManager orders them by priority
74 // according to Java overload rules).
75 for (auto & current : m_Overloads)
76 {
77 JP_TRACE("Trying to match", current->toString());
78 current->matches(frame, match, callInstance, arg);
79
80 JP_TRACE(" match ended", match.m_Type);
81 if (match.m_Type == JPMatch::_exact)
82 {
83 // We can bypass the process here as there is no better match than exact.
84 bestMatch = match;
85 m_LastCache = (JPMethodCache&) match; // lgtm [cpp/slicing]
86 return true;
87 }
88 if (match.m_Type < JPMatch::_implicit)
89 continue;
90
91 // If this is the first match then make it the best.
92 if (bestMatch.m_Overload == nullptr)
93 {
94 bestMatch = match;
95 continue;
96 }
97
98 // If the best does not hide the other, than we have ambiguity.
99 if (!(bestMatch.m_Overload->checkMoreSpecificThan(current)))
100 {
101 // See if we can match based on instance
102 if (callInstance == !current->isStatic())
103 {
104 // if current matches instance and best does not switch
105 if (callInstance == bestMatch.m_Overload->isStatic())
106 {
107 bestMatch = match;
108 continue;
109 }
110 } else
111 {
112 // if best matches instance and current does not, no ambiguity
113 if (callInstance == !bestMatch.m_Overload->isStatic())
114 {
115 continue;
116 }
117 }
118
119 // If the best match is worse than current, switch
120 if (bestMatch.m_Type < match.m_Type)
121 {
122 bestMatch = match;
123 // Remove any prior ambiguity
124 ambiguous.clear();
125 continue;
126 }
127
128 // If the best match is still better than current, keep it
129 if (bestMatch.m_Type > match.m_Type)
130 {
131 continue;
132 }
133
134 JP_TRACE("Adding to ambiguous list");
135
136 // Keep trace of ambiguous overloads for the error report.
137 ambiguous.push_back(current);
138 }
139 }
140
141 // If we have an ambiguous overload report an error.
142 if (!ambiguous.empty())
143 {
144 if (!raise)
145 return false;
146 ambiguous.push_back(bestMatch.m_Overload);
147
148 // We have two possible overloads so we declare an error
149 std::stringstream ss;
150 if (JPModifier::isConstructor(m_Modifiers))
151 ss << "Ambiguous overloads found for constructor " << m_Class->getCanonicalName() << "(";
152 else
153 ss << "Ambiguous overloads found for " << m_Class->getCanonicalName() << "." << getName() << "(";
154 size_t start = callInstance ? 1 : 0;
155 for (size_t i = start; i < arg.size(); ++i)
156 {
157 if (i != start)
158 ss << ",";
159 ss << Py_TYPE(arg[i])->tp_name;
160 }
161 ss << ")" << " between:" << std::endl;
162 for (auto & ambiguous_mthd : ambiguous)
163 {
164 ss << "\t" << ambiguous_mthd->toString() << std::endl;
165 }
166 JP_RAISE(PyExc_TypeError, ss.str());
167 JP_TRACE(ss.str());
168 }
169
170 // If we can't find a matching overload throw an error.
171 if (!bestMatch.m_Overload)
172 {
173 if (!raise)
174 return false;
175 std::stringstream ss;
176 if (JPModifier::isConstructor(m_Modifiers))
177 ss << "No matching overloads found for constructor " << m_Class->getCanonicalName() << "(";
178 else
179 {
180 ss << "No matching overloads found for ";
181 if (!callInstance)
182 ss << "*static* ";
183 ss << m_Class->getCanonicalName() << "." << getName() << "(";
184 }
185 size_t start = callInstance ? 1 : 0;
186 for (size_t i = start; i < arg.size(); ++i)
187 {
188 if (i != start)
189 ss << ",";
190 ss << Py_TYPE(arg[i])->tp_name;
191 }
192 ss << ")" << ", options are:" << std::endl;
193 for (auto current : m_Overloads)
194 {
195 ss << "\t" << current->toString();
196 ss << std::endl;
197 }
198 JP_RAISE(PyExc_TypeError, ss.str());
199 }
200
201 // Set up a cache to bypass repeated calls.
202 if (bestMatch.m_Type == JPMatch::_implicit)
203 {
204 m_LastCache = (JPMethodCache&) bestMatch; // lgtm [cpp/slicing]
205 }
206
207 JP_TRACE("Best match", bestMatch.m_Overload->toString());
208 return true;
209 JP_TRACE_OUT;
210 }
211
212 JPPyObject JPMethodDispatch::invoke(JPJavaFrame& frame, JPPyObjectVector& args, bool instance)
213 {
214 JP_TRACE_IN("JPMethodDispatch::invoke");
215 JPMethodMatch match(frame, args, instance);
216 findOverload(frame, match, args, instance, true);
217 return match.m_Overload->invoke(frame, match, args, instance);
218 JP_TRACE_OUT;
219 }
220
221 JPValue JPMethodDispatch::invokeConstructor(JPJavaFrame& frame, JPPyObjectVector& args)
222 {
223 JP_TRACE_IN("JPMethodDispatch::invokeConstructor");
224 JPMethodMatch match(frame, args, false);
225 findOverload(frame, match, args, false, true);
226 return match.m_Overload->invokeConstructor(frame, match, args);
227 JP_TRACE_OUT;
228 }
229
230 bool JPMethodDispatch::matches(JPJavaFrame& frame, JPPyObjectVector& args, bool instance)
231 {
232 JP_TRACE_IN("JPMethodDispatch::invoke");
233 JPMethodMatch match(frame, args, instance);
234 return findOverload(frame, match, args, instance, false);
235 JP_TRACE_OUT; // GCOVR_EXCL_LINE
236 }
237
238 string JPMethodDispatch::matchReport(JPPyObjectVector& args)
239 {
240 std::stringstream res;
241 res << "Match report for method " << m_Name << ", has " << m_Overloads.size() << " overloads." << std::endl;
242
243 for (auto current : m_Overloads)
244 {
245 res << " " << current->matchReport(args);
246 }
247 return res.str();
248 }
249