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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "BindingOperations.h"
#include <clang/AST/Attr.h>
#include <clang/AST/Expr.h>
#include <clang/AST/RecursiveASTVisitor.h>
#include <algorithm>
#include <array>
#include <cuchar>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#ifdef __cpp_lib_optional
#include <optional>
template<typename T> using optional = std::optional<T>;
#else
#include <llvm/ADT/Optional.h>
template<typename T> using optional = clang::Optional<T>;
#endif
using namespace clang;
namespace {
template<typename InputIt>
bool hasReverseQualifiedName(InputIt first, InputIt last, const NamedDecl &tag)
{
const NamedDecl *currentDecl = &tag;
InputIt currentName;
for (currentName = first; currentName != last; currentName++) {
if (!currentDecl || !currentDecl->getIdentifier() || currentDecl->getName() != *currentName)
return false;
currentDecl = dyn_cast<NamedDecl>(currentDecl->getDeclContext());
}
if (currentName != last)
return false;
if (currentDecl != nullptr)
return false;
return true;
}
bool isMozillaJniObjectBase(const CXXRecordDecl &klass)
{
const auto qualifiedName = std::array<StringRef, 3>{"mozilla", "jni", "ObjectBase"};
return hasReverseQualifiedName(qualifiedName.crbegin(), qualifiedName.crend(), klass);
}
bool isMozillaJniNativeImpl(const CXXRecordDecl &klass)
{
const auto qualifiedName = std::array<StringRef, 3>{"mozilla", "jni", "NativeImpl"};
return hasReverseQualifiedName(qualifiedName.crbegin(), qualifiedName.crend(), klass);
}
const NamedDecl *fieldNamed(StringRef name, const RecordDecl &strukt)
{
for (const auto *decl : strukt.decls()) {
const auto *namedDecl = dyn_cast<VarDecl>(decl);
if (!namedDecl)
continue;
if (!namedDecl->getIdentifier() || namedDecl->getName() != name)
continue;
return namedDecl;
}
return {};
}
optional<StringRef> nameFieldValue(const RecordDecl &strukt)
{
const auto *nameField = dyn_cast_or_null<VarDecl>(fieldNamed("name", strukt));
if (!nameField)
return {};
const auto *def = nameField->getDefinition();
if (!def)
return {};
const auto *name = dyn_cast_or_null<StringLiteral>(def->getInit());
if (!name)
return {};
return name->getString();
}
struct AbstractBinding {
// Subset of tools/analysis/BindingSlotLang
enum class Lang {
Cpp,
Jvm,
};
static constexpr size_t LangLength = 2;
static constexpr std::array<StringRef, LangLength> langNames = {
"cpp",
"jvm",
};
static optional<Lang> langFromString(StringRef langName)
{
const auto it = std::find(langNames.begin(), langNames.end(), langName);
if (it == langNames.end())
return {};
return Lang(it - langNames.begin());
}
static StringRef stringFromLang(Lang lang)
{
return langNames[size_t(lang)];
}
// Subset of tools/analysis/BindingSlotKind
enum class Kind {
Class,
Method,
Getter,
Setter,
Const,
};
static constexpr size_t KindLength = 5;
static constexpr std::array<StringRef, KindLength> kindNames = {
"class",
"method",
"getter",
"setter",
"const",
};
static optional<Kind> kindFromString(StringRef kindName)
{
const auto it = std::find(kindNames.begin(), kindNames.end(), kindName);
if (it == kindNames.end())
return {};
return Kind(it - kindNames.begin());
}
static StringRef stringFromKind(Kind kind)
{
return kindNames[size_t(kind)];
}
Lang lang;
Kind kind;
StringRef symbol;
};
constexpr size_t AbstractBinding::KindLength;
constexpr std::array<StringRef, AbstractBinding::KindLength> AbstractBinding::kindNames;
constexpr size_t AbstractBinding::LangLength;
constexpr std::array<StringRef, AbstractBinding::LangLength> AbstractBinding::langNames;
struct BindingTo : public AbstractBinding {
BindingTo(AbstractBinding b) : AbstractBinding(std::move(b)) {}
static constexpr StringRef ANNOTATION = "binding_to";
};
constexpr StringRef BindingTo::ANNOTATION;
struct BoundAs : public AbstractBinding {
BoundAs(AbstractBinding b) : AbstractBinding(std::move(b)) {}
static constexpr StringRef ANNOTATION = "bound_as";
};
constexpr StringRef BoundAs::ANNOTATION;
template<typename B>
void setBindingAttr(ASTContext &C, Decl &decl, B binding)
{
// recent LLVM: CreateImplicit then setDelayedArgs
Expr *langExpr = StringLiteral::Create(C, AbstractBinding::stringFromLang(binding.lang), StringLiteral::UTF8, false, {}, {});
Expr *kindExpr = StringLiteral::Create(C, AbstractBinding::stringFromKind(binding.kind), StringLiteral::UTF8, false, {}, {});
Expr *symbolExpr = StringLiteral::Create(C, binding.symbol, StringLiteral::UTF8, false, {}, {});
auto **args = new (C, 16) Expr *[3]{langExpr, kindExpr, symbolExpr};
auto *attr = AnnotateAttr::CreateImplicit(C, B::ANNOTATION, args, 3);
decl.addAttr(attr);
}
optional<AbstractBinding> readBinding(const AnnotateAttr &attr)
{
if (attr.args_size() != 3)
return {};
const auto *langExpr = attr.args().begin()[0];
const auto *kindExpr = attr.args().begin()[1];
const auto *symbolExpr = attr.args().begin()[2];
if (!langExpr || !kindExpr || !symbolExpr)
return {};
const auto *langName = dyn_cast<StringLiteral>(langExpr->IgnoreUnlessSpelledInSource());
const auto *kindName = dyn_cast<StringLiteral>(kindExpr->IgnoreUnlessSpelledInSource());
const auto *symbol = dyn_cast<StringLiteral>(symbolExpr->IgnoreUnlessSpelledInSource());
if (!langName || !kindName || !symbol)
return {};
const auto lang = AbstractBinding::langFromString(langName->getString());
const auto kind = AbstractBinding::kindFromString(kindName->getString());
if (!lang || !kind)
return {};
return AbstractBinding {
.lang = *lang,
.kind = *kind,
.symbol = symbol->getString(),
};
}
optional<BindingTo> getBindingTo(const Decl &decl)
{
for (const auto *attr : decl.specific_attrs<AnnotateAttr>()) {
if (attr->getAnnotation() != BindingTo::ANNOTATION)
continue;
const auto binding = readBinding(*attr);
if (!binding)
continue;
return BindingTo{*binding};
}
return {};
}
// C++23: turn into generator
std::vector<BoundAs> getBoundAs(const Decl &decl)
{
std::vector<BoundAs> found;
for (const auto *attr : decl.specific_attrs<AnnotateAttr>()) {
if (attr->getAnnotation() != BoundAs::ANNOTATION)
continue;
const auto binding = readBinding(*attr);
if (!binding)
continue;
found.push_back(BoundAs{*binding});
}
return found;
}
class FindCallCall : private RecursiveASTVisitor<FindCallCall>
{
public:
struct Result {
using Kind = AbstractBinding::Kind;
Kind kind;
StringRef name;
};
static optional<Result> search(Stmt *statement)
{
FindCallCall finder;
finder.TraverseStmt(statement);
return finder.result;
}
private:
optional<Result> result;
friend RecursiveASTVisitor<FindCallCall>;
optional<Result> tryParseCallCall(CallExpr *callExpr){
const auto *callee = dyn_cast_or_null<CXXMethodDecl>(callExpr->getDirectCallee());
if (!callee)
return {};
if (!callee->getIdentifier())
return {};
const auto action = callee->getIdentifier()->getName();
if (action != "Call" && action != "Get" && action != "Set")
return {};
const auto *parentClass = dyn_cast_or_null<ClassTemplateSpecializationDecl>(callee->getParent());
if (!parentClass)
return {};
const auto *parentTemplate = parentClass->getTemplateInstantiationPattern();
if (!parentTemplate || !parentTemplate->getIdentifier())
return {};
const auto parentName = parentTemplate->getIdentifier()->getName();
AbstractBinding::Kind kind;
if (action == "Call") {
if (parentName == "Constructor" || parentName == "Method") {
kind = AbstractBinding::Kind::Method;
} else {
return {};
}
} else if (parentName == "Field") {
if (action == "Get") {
kind = AbstractBinding::Kind::Getter;
} else if (action == "Set") {
kind = AbstractBinding::Kind::Setter;
} else {
return {};
}
} else {
return {};
}
const auto *templateArg = parentClass->getTemplateArgs().get(0).getAsType()->getAsRecordDecl();
if (!templateArg)
return {};
const auto name = nameFieldValue(*templateArg);
if (!name)
return {};
return Result {
.kind = kind,
.name = *name,
};
return {};
}
bool VisitCallExpr(CallExpr *callExpr)
{
return !(result = tryParseCallCall(callExpr));
}
};
constexpr StringRef JVM_SCIP_SYMBOL_PREFIX = "S_jvm_";
std::string javaScipSymbol(StringRef prefix, StringRef name, AbstractBinding::Kind kind)
{
auto symbol = (prefix + name).str();
switch (kind) {
case AbstractBinding::Kind::Class:
std::replace(symbol.begin(), symbol.end(), '$', '#');
symbol += "#";
break;
case AbstractBinding::Kind::Method:
symbol += "().";
break;
case AbstractBinding::Kind::Const:
case AbstractBinding::Kind::Getter:
case AbstractBinding::Kind::Setter:
symbol += ".";
break;
}
return symbol;
}
void addSlotOwnerAttribute(llvm::json::OStream &J, const Decl &decl)
{
if (const auto bindingTo = getBindingTo(decl)) {
J.attributeBegin("slotOwner");
J.objectBegin();
J.attribute("slotKind", AbstractBinding::stringFromKind(bindingTo->kind));
J.attribute("slotLang", "cpp");
J.attribute("ownerLang", AbstractBinding::stringFromLang(bindingTo->lang));
J.attribute("sym", bindingTo->symbol);
J.objectEnd();
J.attributeEnd();
}
}
void addBindingSlotsAttribute(llvm::json::OStream &J, const Decl &decl)
{
const auto allBoundAs = getBoundAs(decl);
if (!allBoundAs.empty()) {
J.attributeBegin("bindingSlots");
J.arrayBegin();
for (const auto boundAs : allBoundAs) {
J.objectBegin();
J.attribute("slotKind", AbstractBinding::stringFromKind(boundAs.kind));
J.attribute("slotLang", AbstractBinding::stringFromLang(boundAs.lang));
J.attribute("ownerLang", "cpp");
J.attribute("sym", boundAs.symbol);
J.objectEnd();
}
J.arrayEnd();
J.attributeEnd();
}
}
// The mangling scheme is documented at https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/design.html
// The main takeaways are:
// - _0xxxx is the utf16 code unit xxxx
// - _1 is _
// - _2 is ;
// - _3 is [
// - __ is the separator between function name and overload specification
// - _ is otherwise the separator between packages/classes/methods
//
// This method takes a StringRef & and mutates it and can be called twice on a Jnicall function name to get
// first the demangled name
// second the demangled overload specification
// But we don't use the later for now because we have no way to map that to how SCIP resolves overloads.
optional<std::string> demangleJnicallPart(StringRef &remainder)
{
std::string demangled;
std::mbstate_t ps = {};
while (!remainder.empty()) {
switch (remainder[0]) {
case '0': {
remainder = remainder.drop_front(1);
uint16_t codeUnit;
const auto ok = remainder.substr(1, 4).getAsInteger(16, codeUnit);
remainder = remainder.drop_front(4);
if (!ok) // failed reading xxxx as hexadecimal from _0xxxx
return {};
std::array<char, MB_LEN_MAX> codePoint;
const auto mbLen = std::c16rtomb(codePoint.data(), codeUnit, &ps);
if (mbLen == -1) // failed converting utf16 to utf8
return {};
demangled += StringRef(codePoint.begin(), mbLen);
break;
}
case '1':
remainder = remainder.drop_front(1);
ps = {};
demangled += '_';
break;
case '2':
remainder = remainder.drop_front(1);
ps = {};
demangled += ';';
break;
case '3':
remainder = remainder.drop_front(1);
ps = {};
demangled += '[';
break;
case '_':
remainder = remainder.drop_front(1);
ps = {};
if (remainder.empty()) // the string ends with _
return {};
switch (remainder[0]) {
case '0':
case '1':
case '2':
case '3':
demangled += '.';
break;
default:
// either:
// * the string began with _[^0-3], which is not supposed to happen; or
// * we reached __[^0-3] meaning we finished the first part of the name and remainder holds the overload specification
return demangled;
}
default:
ps = {};
demangled += '.';
break;
}
StringRef token;
std::tie(token, remainder) = remainder.split('_');
demangled += token;
}
return demangled;
}
optional<std::string> scipSymbolFromJnicallFunctionName(StringRef functionName)
{
if (!functionName.consume_front("Java_"))
return {};
const auto demangledName = demangleJnicallPart(functionName);
if (!demangledName || demangledName->empty())
return {};
// demangleJavaName returns something like .some.package.Class$InnerClass.method
// - prepend S_jvm_
// - remove the leading dot
// - replace the last dot with a #
// - replace the other dots with /
// - replace $ with #
// - add the ([+overloadNumber]). suffix
auto symbol = JVM_SCIP_SYMBOL_PREFIX.str();
symbol += demangledName->substr(1);
const auto lastDot = symbol.rfind('.');
if (lastDot != std::string::npos)
symbol[lastDot] = '#';
std::replace(symbol.begin(), symbol.end(), '.', '/');
std::replace(symbol.begin(), symbol.end(), '$', '#');
// Keep track of how many times we have seen this method, to build the ([+overloadNumber]). suffix.
// This assumes this function is called on C function definitions in the same order the matching overloads are declared in Java.
static std::unordered_map<std::string, uint> jnicallFunctions;
auto &overloadNumber = jnicallFunctions[symbol];
symbol += '(';
if (overloadNumber) {
symbol += '+';
symbol += overloadNumber;
overloadNumber++;
}
symbol += ").";
return symbol;
};
} // anonymous namespace
// class [wrapper] : public mozilla::jni::ObjectBase<[wrapper]>
// {
// static constexpr char name[] = "[nameFieldValue]";
// }
void findBindingToJavaClass(ASTContext &C, CXXRecordDecl &klass)
{
for (const auto &baseSpecifier : klass.bases()) {
const auto *base = baseSpecifier.getType()->getAsCXXRecordDecl();
if (!base)
continue;
if (!isMozillaJniObjectBase(*base))
continue;
const auto name = nameFieldValue(klass);
if (!name)
continue;
const auto symbol = javaScipSymbol(JVM_SCIP_SYMBOL_PREFIX, *name, BindingTo::Kind::Class);
const auto binding = BindingTo {{
.lang = BindingTo::Lang::Jvm,
.kind = BindingTo::Kind::Class,
.symbol = symbol,
}};
setBindingAttr(C, klass, binding);
return;
}
}
// When a Java method is marked as native, the JRE looks by default for a function
// named Java_<mangled method name>[__<mangled overload specification>].
void findBindingToJavaFunction(ASTContext &C, FunctionDecl &function)
{
const auto *identifier = function.getIdentifier();
if (!identifier)
return;
const auto name = identifier->getName();
const auto symbol = scipSymbolFromJnicallFunctionName(name);
if (!symbol)
return;
const auto binding = BoundAs {{
.lang = BindingTo::Lang::Jvm,
.kind = BindingTo::Kind::Method,
.symbol = *symbol,
}};
setBindingAttr(C, function, binding);
}
// class [parent]
// {
// struct [methodStruct] {
// static constexpr char name[] = "[methodNameFieldValue]";
// }
// [method]
// {
// ...
// mozilla::jni::{Method,Constructor,Field}<[methodStruct]>::{Call,Get,Set}(...)
// ...
// }
// }
void findBindingToJavaMember(ASTContext &C, CXXMethodDecl &method)
{
const auto *parent = method.getParent();
if (!parent)
return;
const auto classBinding = getBindingTo(*parent);
if (!classBinding)
return;
auto *body = method.getBody();
if (!body)
return;
const auto found = FindCallCall::search(body);
if (!found)
return;
const auto symbol = javaScipSymbol(classBinding->symbol, found->name, found->kind);
const auto binding = BindingTo {{
.lang = BindingTo::Lang::Jvm,
.kind = found->kind,
.symbol = symbol,
}};
setBindingAttr(C, method, binding);
}
// class [parent]
// {
// struct [methodStruct] {
// static constexpr char name[] = "[methodNameFieldValue]";
// }
// [method]
// {
// ...
// mozilla::jni::{Method,Constructor,Field}<[methodStruct]>::{Call,Get,Set}(...)
// ...
// }
// }
void findBindingToJavaConstant(ASTContext &C, VarDecl &field)
{
const auto *parent = dyn_cast_or_null<CXXRecordDecl>(field.getDeclContext());
if (!parent)
return;
const auto classBinding = getBindingTo(*parent);
if (!classBinding)
return;
const auto symbol = javaScipSymbol(classBinding->symbol, field.getName(), BindingTo::Kind::Const);
const auto binding = BindingTo {{
.lang = BindingTo::Lang::Jvm,
.kind = BindingTo::Kind::Const,
.symbol = symbol,
}};
setBindingAttr(C, field, binding);
}
// class [klass] : public [wrapper]::Natives<[klass]> {...}
// class [wrapper] : public mozilla::jni::ObjectBase<[wrapper]>
// {
// static constexpr char name[] = "[nameFieldValue]";
//
// struct [methodStruct] {
// static constexpr char name[] = "[methodNameFieldValue]";
// }
//
// template<typename T>
// class [wrapper]::Natives : public mozilla::jni::NativeImpl<[wrapper], T> {
// static const JNINativeMethod methods[] = {
// mozilla::jni::MakeNativeMethod<[wrapper]::[methodStruct]>(
// mozilla::jni::NativeStub<[wrapper]::[methodStruct], Impl>
// ::template Wrap<&Impl::[method]>),
// }
// }
// }
void findBoundAsJavaClasses(ASTContext &C, CXXRecordDecl &klass)
{
for (const auto &baseSpecifier : klass.bases()) {
const auto *base = baseSpecifier.getType()->getAsCXXRecordDecl();
if (!base)
continue;
for (const auto &baseBaseSpecifier : base->bases()) {
const auto *baseBase = dyn_cast_or_null<ClassTemplateSpecializationDecl>(baseBaseSpecifier.getType()->getAsCXXRecordDecl());
if (!baseBase)
continue;
if (!isMozillaJniNativeImpl(*baseBase))
continue;
const auto *wrapper = baseBase->getTemplateArgs().get(0).getAsType()->getAsCXXRecordDecl();
if (!wrapper)
continue;
const auto name = nameFieldValue(*wrapper);
if (!name)
continue;
const auto javaClassSymbol = javaScipSymbol(JVM_SCIP_SYMBOL_PREFIX, *name, BoundAs::Kind::Class);
const auto classBinding = BoundAs {{
.lang = BoundAs::Lang::Jvm,
.kind = BoundAs::Kind::Class,
.symbol = javaClassSymbol,
}};
setBindingAttr(C, klass, classBinding);
const auto *methodsDecl = dyn_cast_or_null<VarDecl>(fieldNamed("methods", *base));
if (!methodsDecl)
continue;
const auto *methodsDef = methodsDecl->getDefinition();
if (!methodsDef)
continue;
const auto *inits = dyn_cast_or_null<InitListExpr>(methodsDef->getInit());
if (!inits)
continue;
std::set<const CXXMethodDecl *> alreadyBound;
for (const auto *init : inits->inits()) {
const auto *call = dyn_cast<CallExpr>(init->IgnoreUnlessSpelledInSource());
if (!call)
continue;
const auto *funcDecl = call->getDirectCallee();
if (!funcDecl)
continue;
const auto *templateArgs = funcDecl->getTemplateSpecializationArgs();
if (!templateArgs)
continue;
const auto *strukt = dyn_cast_or_null<RecordDecl>(templateArgs->get(0).getAsType()->getAsRecordDecl());
if (!strukt)
continue;
const auto *wrapperRef = dyn_cast_or_null<DeclRefExpr>(call->getArg(0)->IgnoreUnlessSpelledInSource());
if (!wrapperRef)
continue;
const auto *boundRef = dyn_cast_or_null<UnaryOperator>(wrapperRef->template_arguments().front().getArgument().getAsExpr());
if (!boundRef)
continue;
auto addToBound = [&](CXXMethodDecl &boundDecl, uint overloadNum) {
const auto methodName = nameFieldValue(*strukt);
if (!methodName)
return;
auto javaMethodSymbol = javaClassSymbol;
javaMethodSymbol += *methodName;
javaMethodSymbol += '(';
if (overloadNum > 0) {
javaMethodSymbol += '+';
javaMethodSymbol += std::to_string(overloadNum);
}
javaMethodSymbol += ").";
const auto binding = BoundAs {{
.lang = BoundAs::Lang::Jvm,
.kind = BoundAs::Kind::Method,
.symbol = javaMethodSymbol,
}};
setBindingAttr(C, boundDecl, binding);
};
if (auto *bound = dyn_cast_or_null<DeclRefExpr>(boundRef->getSubExpr())) {
auto *method = dyn_cast_or_null<CXXMethodDecl>(bound->getDecl());
if (!method)
continue;
addToBound(*method, 0);
} else if (const auto *bound = dyn_cast_or_null<UnresolvedLookupExpr>(boundRef->getSubExpr())) {
// XXX This is hackish
// In case of overloads it's not obvious which one we should use
// this expects the declaration order between C++ and Java to match
auto declarations = std::vector<Decl*>(bound->decls_begin(), bound->decls_end());
auto byLocation = [](Decl *a, Decl *b){ return a->getLocation() < b->getLocation(); };
std::sort(declarations.begin(), declarations.end(), byLocation);
uint i = 0;
for (auto *decl : declarations) {
auto *method = dyn_cast<CXXMethodDecl>(decl);
if (!method)
continue;
if (alreadyBound.find(method) == alreadyBound.end()) {
addToBound(*method, i);
alreadyBound.insert(method);
break;
}
i++;
}
}
}
}
}
}
void emitBindingAttributes(llvm::json::OStream &J, const Decl &decl)
{
addSlotOwnerAttribute(J, decl);
addBindingSlotsAttribute(J, decl);
}
|