forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassfile.cc
1815 lines (1559 loc) · 50.4 KB
/
classfile.cc
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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015 The Bazel Authors. All rights reserved.
//
// 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.
//
// classfile.cc -- classfile parsing and stripping.
//
// TODO(adonovan) don't pass pointers by reference; this is not
// compatible with Google C++ style.
// See README.txt for details.
//
// For definition of JVM class file format, see:
// Java SE 8 Edition:
// http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4
#define __STDC_FORMAT_MACROS 1
#define __STDC_LIMIT_MACROS 1
#include <inttypes.h> // for PRIx32
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include "third_party/ijar/common.h"
namespace {
// Converts a value to string.
// Workaround for mingw where std::to_string is not implemented.
// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52015.
template <typename T>
std::string ToString(const T& value) {
std::ostringstream oss;
oss << value;
return oss.str();
}
} // namespace
namespace devtools_ijar {
// See Table 4.3 in JVM Spec.
enum CONSTANT {
CONSTANT_Class = 7,
CONSTANT_FieldRef = 9,
CONSTANT_Methodref = 10,
CONSTANT_Interfacemethodref = 11,
CONSTANT_String = 8,
CONSTANT_Integer = 3,
CONSTANT_Float = 4,
CONSTANT_Long = 5,
CONSTANT_Double = 6,
CONSTANT_NameAndType = 12,
CONSTANT_Utf8 = 1,
CONSTANT_MethodHandle = 15,
CONSTANT_MethodType = 16,
CONSTANT_InvokeDynamic = 18
};
// See Tables 4.1, 4.4, 4.5 in JVM Spec.
enum ACCESS {
ACC_PUBLIC = 0x0001,
ACC_PRIVATE = 0x0002,
ACC_PROTECTED = 0x0004,
ACC_STATIC = 0x0008,
ACC_FINAL = 0x0010,
ACC_SYNCHRONIZED = 0x0020,
ACC_BRIDGE = 0x0040,
ACC_VOLATILE = 0x0040,
ACC_TRANSIENT = 0x0080,
ACC_INTERFACE = 0x0200,
ACC_ABSTRACT = 0x0400,
ACC_SYNTHETIC = 0x1000
};
// See Table 4.7.20-A in Java 8 JVM Spec.
enum TARGET_TYPE {
// Targets for type parameter declarations (ElementType.TYPE_PARAMETER):
CLASS_TYPE_PARAMETER = 0x00,
METHOD_TYPE_PARAMETER = 0x01,
// Targets for type uses that may be externally visible in classes and members
// (ElementType.TYPE_USE):
CLASS_EXTENDS = 0x10,
CLASS_TYPE_PARAMETER_BOUND = 0x11,
METHOD_TYPE_PARAMETER_BOUND = 0x12,
FIELD = 0x13,
METHOD_RETURN = 0x14,
METHOD_RECEIVER = 0x15,
METHOD_FORMAL_PARAMETER = 0x16,
THROWS = 0x17,
// TARGET_TYPE >= 0x40 is reserved for type uses that occur only within code
// blocks. Ijar doesn't need to know about these.
};
struct Constant;
// TODO(adonovan) these globals are unfortunate
static std::vector<Constant*> const_pool_in; // input constant pool
static std::vector<Constant*> const_pool_out; // output constant_pool
static std::set<std::string> used_class_names;
static Constant * class_name;
// Returns the Constant object, given an index into the input constant pool.
// Note: constant(0) == NULL; this invariant is exploited by the
// InnerClassesAttribute, inter alia.
inline Constant *constant(int idx) {
if (idx < 0 || (unsigned)idx >= const_pool_in.size()) {
fprintf(stderr, "Illegal constant pool index: %d\n", idx);
abort();
}
return const_pool_in[idx];
}
/**********************************************************************
* *
* Constants *
* *
**********************************************************************/
// See sec.4.4 of JVM spec.
struct Constant {
Constant(u1 tag) :
slot_(0),
tag_(tag) {}
virtual ~Constant() {}
// For UTF-8 string constants, returns the encoded string.
// Otherwise, returns an undefined string value suitable for debugging.
virtual std::string Display() = 0;
virtual void Write(u1 *&p) = 0;
// Called by slot() when a constant has been identified as required
// in the output classfile's constant pool. This is a hook allowing
// constants to register their dependency on other constants, by
// calling slot() on them in turn.
virtual void Keep() {}
bool Kept() {
return slot_ != 0;
}
// Returns the index of this constant in the output class's constant
// pool, assigning a slot if not already done.
u2 slot() {
if (slot_ == 0) {
Keep();
slot_ = const_pool_out.size(); // BugBot's "narrowing" warning
// is bogus. The number of
// output constants can't exceed
// the number of input constants.
if (slot_ == 0) {
fprintf(stderr, "Constant::slot() called before output phase.\n");
abort();
}
const_pool_out.push_back(this);
if (tag_ == CONSTANT_Long || tag_ == CONSTANT_Double) {
const_pool_out.push_back(NULL);
}
}
return slot_;
}
u2 slot_; // zero => "this constant is unreachable garbage"
u1 tag_;
};
// Extracts class names from a signature and puts them into the global
// variable used_class_names.
//
// desc: the descriptor class names should be extracted from.
// p: the position where the extraction should tart.
void ExtractClassNames(const std::string& desc, size_t* p);
// See sec.4.4.1 of JVM spec.
struct Constant_Class : Constant
{
Constant_Class(u2 name_index) :
Constant(CONSTANT_Class),
name_index_(name_index) {}
void Write(u1 *&p) {
put_u1(p, tag_);
put_u2be(p, constant(name_index_)->slot());
}
std::string Display() {
return constant(name_index_)->Display();
}
void Keep() { constant(name_index_)->slot(); }
u2 name_index_;
};
// See sec.4.4.2 of JVM spec.
struct Constant_FMIref : Constant
{
Constant_FMIref(u1 tag,
u2 class_index,
u2 name_type_index) :
Constant(tag),
class_index_(class_index),
name_type_index_(name_type_index) {}
void Write(u1 *&p) {
put_u1(p, tag_);
put_u2be(p, constant(class_index_)->slot());
put_u2be(p, constant(name_type_index_)->slot());
}
std::string Display() {
return constant(class_index_)->Display() + "::" +
constant(name_type_index_)->Display();
}
void Keep() {
constant(class_index_)->slot();
constant(name_type_index_)->slot();
}
u2 class_index_;
u2 name_type_index_;
};
// See sec.4.4.3 of JVM spec.
struct Constant_String : Constant
{
Constant_String(u2 string_index) :
Constant(CONSTANT_String),
string_index_(string_index) {}
void Write(u1 *&p) {
put_u1(p, tag_);
put_u2be(p, constant(string_index_)->slot());
}
std::string Display() {
return "\"" + constant(string_index_)->Display() + "\"";
}
void Keep() { constant(string_index_)->slot(); }
u2 string_index_;
};
// See sec.4.4.4 of JVM spec.
struct Constant_IntegerOrFloat : Constant
{
Constant_IntegerOrFloat(u1 tag, u4 bytes) :
Constant(tag),
bytes_(bytes) {}
void Write(u1 *&p) {
put_u1(p, tag_);
put_u4be(p, bytes_);
}
std::string Display() { return "int/float"; }
u4 bytes_;
};
// See sec.4.4.5 of JVM spec.
struct Constant_LongOrDouble : Constant_IntegerOrFloat
{
Constant_LongOrDouble(u1 tag, u4 high_bytes, u4 low_bytes) :
Constant_IntegerOrFloat(tag, high_bytes),
low_bytes_(low_bytes) {}
void Write(u1 *&p) {
put_u1(p, tag_);
put_u4be(p, bytes_);
put_u4be(p, low_bytes_);
}
std::string Display() { return "long/double"; }
u4 low_bytes_;
};
// See sec.4.4.6 of JVM spec.
struct Constant_NameAndType : Constant
{
Constant_NameAndType(u2 name_index, u2 descr_index) :
Constant(CONSTANT_NameAndType),
name_index_(name_index),
descr_index_(descr_index) {}
void Write(u1 *&p) {
put_u1(p, tag_);
put_u2be(p, constant(name_index_)->slot());
put_u2be(p, constant(descr_index_)->slot());
}
std::string Display() {
return constant(name_index_)->Display() + "::" +
constant(descr_index_)->Display();
}
void Keep() {
constant(name_index_)->slot();
constant(descr_index_)->slot();
}
u2 name_index_;
u2 descr_index_;
};
// See sec.4.4.7 of JVM spec.
struct Constant_Utf8 : Constant
{
Constant_Utf8(u4 length, const u1 *utf8) :
Constant(CONSTANT_Utf8),
length_(length),
utf8_(utf8) {}
void Write(u1 *&p) {
put_u1(p, tag_);
put_u2be(p, length_);
put_n(p, utf8_, length_);
}
std::string Display() {
return std::string((const char*) utf8_, length_);
}
u4 length_;
const u1 *utf8_;
};
// See sec.4.4.8 of JVM spec.
struct Constant_MethodHandle : Constant
{
Constant_MethodHandle(u1 reference_kind, u2 reference_index) :
Constant(CONSTANT_MethodHandle),
reference_kind_(reference_kind),
reference_index_(reference_index) {}
void Write(u1 *&p) {
put_u1(p, tag_);
put_u1(p, reference_kind_);
put_u2be(p, reference_index_);
}
std::string Display() {
return "Constant_MethodHandle::" + ToString(reference_kind_) + "::"
+ constant(reference_index_)->Display();
}
u1 reference_kind_;
u2 reference_index_;
};
// See sec.4.4.9 of JVM spec.
struct Constant_MethodType : Constant
{
Constant_MethodType(u2 descriptor_index) :
Constant(CONSTANT_MethodType),
descriptor_index_(descriptor_index) {}
void Write(u1 *&p) {
put_u1(p, tag_);
put_u2be(p, descriptor_index_);
}
std::string Display() {
return "Constant_MethodType::" + constant(descriptor_index_)->Display();
}
u2 descriptor_index_;
};
// See sec.4.4.10 of JVM spec.
struct Constant_InvokeDynamic : Constant
{
Constant_InvokeDynamic(u2 bootstrap_method_attr_index, u2 name_and_type_index) :
Constant(CONSTANT_InvokeDynamic),
bootstrap_method_attr_index_(bootstrap_method_attr_index),
name_and_type_index_(name_and_type_index) {}
void Write(u1 *&p) {
put_u1(p, tag_);
put_u2be(p, bootstrap_method_attr_index_);
put_u2be(p, name_and_type_index_);
}
std::string Display() {
return "Constant_InvokeDynamic::"
+ ToString(bootstrap_method_attr_index_) + "::"
+ constant(name_and_type_index_)->Display();
}
u2 bootstrap_method_attr_index_;
u2 name_and_type_index_;
};
/**********************************************************************
* *
* Attributes *
* *
**********************************************************************/
// See sec.4.7 of JVM spec.
struct Attribute {
virtual ~Attribute() {}
virtual void Write(u1 *&p) = 0;
virtual void ExtractClassNames() {}
void WriteProlog(u1 *&p, u2 length) {
put_u2be(p, attribute_name_->slot());
put_u4be(p, length);
}
Constant *attribute_name_;
};
// See sec.4.7.5 of JVM spec.
struct ExceptionsAttribute : Attribute {
static ExceptionsAttribute* Read(const u1 *&p, Constant *attribute_name) {
ExceptionsAttribute *attr = new ExceptionsAttribute;
attr->attribute_name_ = attribute_name;
u2 number_of_exceptions = get_u2be(p);
for (int ii = 0; ii < number_of_exceptions; ++ii) {
attr->exceptions_.push_back(constant(get_u2be(p)));
}
return attr;
}
void Write(u1 *&p) {
WriteProlog(p, exceptions_.size() * 2 + 2);
put_u2be(p, exceptions_.size());
for (size_t ii = 0; ii < exceptions_.size(); ++ii) {
put_u2be(p, exceptions_[ii]->slot());
}
}
std::vector<Constant*> exceptions_;
};
// See sec.4.7.6 of JVM spec.
struct InnerClassesAttribute : Attribute {
struct Entry {
Constant *inner_class_info;
Constant *outer_class_info;
Constant *inner_name;
u2 inner_class_access_flags;
};
virtual ~InnerClassesAttribute() {
for (size_t i = 0; i < entries_.size(); i++) {
delete entries_[i];
}
}
static InnerClassesAttribute* Read(const u1 *&p, Constant *attribute_name) {
InnerClassesAttribute *attr = new InnerClassesAttribute;
attr->attribute_name_ = attribute_name;
u2 number_of_classes = get_u2be(p);
for (int ii = 0; ii < number_of_classes; ++ii) {
Entry *entry = new Entry;
entry->inner_class_info = constant(get_u2be(p));
entry->outer_class_info = constant(get_u2be(p));
entry->inner_name = constant(get_u2be(p));
entry->inner_class_access_flags = get_u2be(p);
attr->entries_.push_back(entry);
}
return attr;
}
void Write(u1 *&p) {
std::set<int> kept_entries;
// We keep an entry if the constant referring to the inner class is already
// kept. Then we mark its outer class and its class name as kept, too, then
// iterate until a fixed point is reached.
int entry_count;
int iteration = 0;
do {
entry_count = kept_entries.size();
for (int i_entry = 0; i_entry < static_cast<int>(entries_.size());
++i_entry) {
Entry* entry = entries_[i_entry];
if (entry->inner_class_info->Kept() ||
used_class_names.find(entry->inner_class_info->Display()) !=
used_class_names.end() ||
entry->outer_class_info == class_name) {
if (entry->inner_name == NULL) {
// JVMS 4.7.6: inner_name_index is zero iff the class is anonymous
continue;
}
kept_entries.insert(i_entry);
// JVMS 4.7.6: outer_class_info_index is zero for top-level classes
if (entry->outer_class_info != NULL) {
entry->outer_class_info->slot();
}
entry->inner_name->slot();
}
}
iteration += 1;
} while (entry_count != static_cast<int>(kept_entries.size()));
if (kept_entries.size() == 0) {
return;
}
WriteProlog(p, 2 + kept_entries.size() * 8);
put_u2be(p, kept_entries.size());
for (std::set<int>::iterator it = kept_entries.begin();
it != kept_entries.end();
++it) {
Entry *entry = entries_[*it];
put_u2be(p, entry->inner_class_info == NULL
? 0
: entry->inner_class_info->slot());
put_u2be(p, entry->outer_class_info == NULL
? 0
: entry->outer_class_info->slot());
put_u2be(p, entry->inner_name == NULL
? 0
: entry->inner_name->slot());
put_u2be(p, entry->inner_class_access_flags);
}
}
std::vector<Entry*> entries_;
};
// See sec.4.7.7 of JVM spec.
// We preserve EnclosingMethod attributes to be able to identify local and
// anonymous classes. These classes will be stripped of most content, as they
// represent implementation details that shoudn't leak into the ijars. Omitting
// EnclosingMethod attributes can lead to type-checking failures in the presence
// of generics (see b/9070939).
struct EnclosingMethodAttribute : Attribute {
static EnclosingMethodAttribute* Read(const u1 *&p,
Constant *attribute_name) {
EnclosingMethodAttribute *attr = new EnclosingMethodAttribute;
attr->attribute_name_ = attribute_name;
attr->class_ = constant(get_u2be(p));
attr->method_ = constant(get_u2be(p));
return attr;
}
void Write(u1 *&p) {
WriteProlog(p, 4);
put_u2be(p, class_->slot());
put_u2be(p, method_ == NULL ? 0 : method_->slot());
}
Constant *class_;
Constant *method_;
};
// See sec.4.7.16.1 of JVM spec.
// Used by AnnotationDefault and other attributes.
struct ElementValue {
virtual ~ElementValue() {}
virtual void Write(u1 *&p) = 0;
virtual void ExtractClassNames() {}
static ElementValue* Read(const u1 *&p);
u1 tag_;
u4 length_;
};
struct BaseTypeElementValue : ElementValue {
void Write(u1 *&p) {
put_u1(p, tag_);
put_u2be(p, const_value_->slot());
}
static BaseTypeElementValue *Read(const u1 *&p) {
BaseTypeElementValue *value = new BaseTypeElementValue;
value->const_value_ = constant(get_u2be(p));
return value;
}
Constant *const_value_;
};
struct EnumTypeElementValue : ElementValue {
void Write(u1 *&p) {
put_u1(p, tag_);
put_u2be(p, type_name_->slot());
put_u2be(p, const_name_->slot());
}
static EnumTypeElementValue *Read(const u1 *&p) {
EnumTypeElementValue *value = new EnumTypeElementValue;
value->type_name_ = constant(get_u2be(p));
value->const_name_ = constant(get_u2be(p));
return value;
}
Constant *type_name_;
Constant *const_name_;
};
struct ClassTypeElementValue : ElementValue {
void Write(u1 *&p) {
put_u1(p, tag_);
put_u2be(p, class_info_->slot());
}
virtual void ExtractClassNames() {
size_t idx = 0;
devtools_ijar::ExtractClassNames(class_info_->Display(), &idx);
}
static ClassTypeElementValue *Read(const u1 *&p) {
ClassTypeElementValue *value = new ClassTypeElementValue;
value->class_info_ = constant(get_u2be(p));
return value;
}
Constant *class_info_;
};
struct ArrayTypeElementValue : ElementValue {
virtual ~ArrayTypeElementValue() {
for (const auto *value : values_) {
delete value;
}
}
virtual void ExtractClassNames() {
for (auto *value : values_) {
value->ExtractClassNames();
}
}
void Write(u1 *&p) {
put_u1(p, tag_);
put_u2be(p, values_.size());
for (auto *value : values_) {
value->Write(p);
}
}
static ArrayTypeElementValue *Read(const u1 *&p) {
ArrayTypeElementValue *value = new ArrayTypeElementValue;
u2 num_values = get_u2be(p);
for (int ii = 0; ii < num_values; ++ii) {
value->values_.push_back(ElementValue::Read(p));
}
return value;
}
std::vector<ElementValue*> values_;
};
// See sec.4.7.16 of JVM spec.
struct Annotation {
virtual ~Annotation() {
for (size_t i = 0; i < element_value_pairs_.size(); i++) {
delete element_value_pairs_[i]->element_value_;
delete element_value_pairs_[i];
}
}
void ExtractClassNames() {
for (size_t i = 0; i < element_value_pairs_.size(); i++) {
element_value_pairs_[i]->element_value_->ExtractClassNames();
}
}
void Write(u1 *&p) {
put_u2be(p, type_->slot());
put_u2be(p, element_value_pairs_.size());
for (size_t ii = 0; ii < element_value_pairs_.size(); ++ii) {
put_u2be(p, element_value_pairs_[ii]->element_name_->slot());
element_value_pairs_[ii]->element_value_->Write(p);
}
}
static Annotation *Read(const u1 *&p) {
Annotation *value = new Annotation;
value->type_ = constant(get_u2be(p));
u2 num_element_value_pairs = get_u2be(p);
for (int ii = 0; ii < num_element_value_pairs; ++ii) {
ElementValuePair *pair = new ElementValuePair;
pair->element_name_ = constant(get_u2be(p));
pair->element_value_ = ElementValue::Read(p);
value->element_value_pairs_.push_back(pair);
}
return value;
}
Constant *type_;
struct ElementValuePair {
Constant *element_name_;
ElementValue *element_value_;
};
std::vector<ElementValuePair*> element_value_pairs_;
};
// See sec 4.7.20 of Java 8 JVM Spec
//
// Each entry in the annotations table represents a single run-time visible
// annotation on a type used in a declaration or expression. The type_annotation
// structure has the following format:
//
// type_annotation {
// u1 target_type;
// union {
// type_parameter_target;
// supertype_target;
// type_parameter_bound_target;
// empty_target;
// method_formal_parameter_target;
// throws_target;
// localvar_target;
// catch_target;
// offset_target;
// type_argument_target;
// } target_info;
// type_path target_path;
// u2 type_index;
// u2 num_element_value_pairs;
// {
// u2 element_name_index;
// element_value value;
// }
// element_value_pairs[num_element_value_pairs];
// }
//
struct TypeAnnotation {
virtual ~TypeAnnotation() {
delete target_info_;
delete type_path_;
delete annotation_;
}
void ExtractClassNames() {
annotation_->ExtractClassNames();
}
void Write(u1 *&p) {
put_u1(p, target_type_);
target_info_->Write(p);
type_path_->Write(p);
annotation_->Write(p);
}
static TypeAnnotation *Read(const u1 *&p) {
TypeAnnotation *value = new TypeAnnotation;
value->target_type_ = get_u1(p);
value->target_info_ = ReadTargetInfo(p, value->target_type_);
value->type_path_ = TypePath::Read(p);
value->annotation_ = Annotation::Read(p);
return value;
}
struct TargetInfo {
virtual ~TargetInfo() {}
virtual void Write(u1 *&p) = 0;
};
struct TypeParameterTargetInfo : TargetInfo {
void Write(u1 *&p) {
put_u1(p, type_parameter_index_);
}
static TypeParameterTargetInfo *Read(const u1 *&p) {
TypeParameterTargetInfo *value = new TypeParameterTargetInfo;
value->type_parameter_index_ = get_u1(p);
return value;
}
u1 type_parameter_index_;
};
struct ClassExtendsInfo : TargetInfo {
void Write(u1 *&p) {
put_u2be(p, supertype_index_);
}
static ClassExtendsInfo *Read(const u1 *&p) {
ClassExtendsInfo *value = new ClassExtendsInfo;
value->supertype_index_ = get_u2be(p);
return value;
}
u2 supertype_index_;
};
struct TypeParameterBoundInfo : TargetInfo {
void Write(u1 *&p) {
put_u1(p, type_parameter_index_);
put_u1(p, bound_index_);
}
static TypeParameterBoundInfo *Read(const u1 *&p) {
TypeParameterBoundInfo *value = new TypeParameterBoundInfo;
value->type_parameter_index_ = get_u1(p);
value->bound_index_ = get_u1(p);
return value;
}
u1 type_parameter_index_;
u1 bound_index_;
};
struct EmptyInfo : TargetInfo {
void Write(u1 *&p) {}
static EmptyInfo *Read(const u1 *&p) {
return new EmptyInfo;
}
};
struct MethodFormalParameterInfo : TargetInfo {
void Write(u1 *&p) {
put_u1(p, method_formal_parameter_index_);
}
static MethodFormalParameterInfo *Read(const u1 *&p) {
MethodFormalParameterInfo *value = new MethodFormalParameterInfo;
value->method_formal_parameter_index_ = get_u1(p);
return value;
}
u1 method_formal_parameter_index_;
};
struct ThrowsTypeInfo : TargetInfo {
void Write(u1 *&p) {
put_u2be(p, throws_type_index_);
}
static ThrowsTypeInfo *Read(const u1 *&p) {
ThrowsTypeInfo *value = new ThrowsTypeInfo;
value->throws_type_index_ = get_u2be(p);
return value;
}
u2 throws_type_index_;
};
static TargetInfo *ReadTargetInfo(const u1 *&p, u1 target_type) {
switch (target_type) {
case CLASS_TYPE_PARAMETER:
case METHOD_TYPE_PARAMETER:
return TypeParameterTargetInfo::Read(p);
case CLASS_EXTENDS:
return ClassExtendsInfo::Read(p);
case CLASS_TYPE_PARAMETER_BOUND:
case METHOD_TYPE_PARAMETER_BOUND:
return TypeParameterBoundInfo::Read(p);
case FIELD:
case METHOD_RETURN:
case METHOD_RECEIVER:
return new EmptyInfo;
case METHOD_FORMAL_PARAMETER:
return MethodFormalParameterInfo::Read(p);
case THROWS:
return ThrowsTypeInfo::Read(p);
default:
fprintf(stderr, "Illegal type annotation target type: %d\n",
target_type);
abort();
}
}
struct TypePath {
void Write(u1 *&p) {
put_u1(p, path_.size());
for (TypePathEntry entry : path_) {
put_u1(p, entry.type_path_kind_);
put_u1(p, entry.type_argument_index_);
}
}
static TypePath *Read(const u1 *&p) {
TypePath *value = new TypePath;
u1 path_length = get_u1(p);
for (int ii = 0; ii < path_length; ++ii) {
TypePathEntry entry;
entry.type_path_kind_ = get_u1(p);
entry.type_argument_index_ = get_u1(p);
value->path_.push_back(entry);
}
return value;
}
struct TypePathEntry {
u1 type_path_kind_;
u1 type_argument_index_;
};
std::vector<TypePathEntry> path_;
};
u1 target_type_;
TargetInfo *target_info_;
TypePath *type_path_;
Annotation *annotation_;
};
struct AnnotationTypeElementValue : ElementValue {
virtual ~AnnotationTypeElementValue() {
delete annotation_;
}
void Write(u1 *&p) {
put_u1(p, tag_);
annotation_->Write(p);
}
static AnnotationTypeElementValue *Read(const u1 *&p) {
AnnotationTypeElementValue *value = new AnnotationTypeElementValue;
value->annotation_ = Annotation::Read(p);
return value;
}
Annotation *annotation_;
};
ElementValue* ElementValue::Read(const u1 *&p) {
const u1* start = p;
ElementValue *result;
u1 tag = get_u1(p);
if (tag != 0 && strchr("BCDFIJSZs", (char) tag) != NULL) {
result = BaseTypeElementValue::Read(p);
} else if ((char) tag == 'e') {
result = EnumTypeElementValue::Read(p);
} else if ((char) tag == 'c') {
result = ClassTypeElementValue::Read(p);
} else if ((char) tag == '[') {
result = ArrayTypeElementValue::Read(p);
} else if ((char) tag == '@') {
result = AnnotationTypeElementValue::Read(p);
} else {
fprintf(stderr, "Illegal element_value::tag: %d\n", tag);
abort();
}
result->tag_ = tag;
result->length_ = p - start;
return result;
}
// See sec.4.7.20 of JVM spec.
// We preserve AnnotationDefault attributes because they are required
// in order to make use of an annotation in new code.
struct AnnotationDefaultAttribute : Attribute {
virtual ~AnnotationDefaultAttribute() {
delete default_value_;
}
static AnnotationDefaultAttribute* Read(const u1 *&p,
Constant *attribute_name) {
AnnotationDefaultAttribute *attr = new AnnotationDefaultAttribute;
attr->attribute_name_ = attribute_name;
attr->default_value_ = ElementValue::Read(p);
return attr;
}
void Write(u1 *&p) {
WriteProlog(p, default_value_->length_);
default_value_->Write(p);
}
virtual void ExtractClassNames() {
default_value_->ExtractClassNames();
}
ElementValue *default_value_;
};
// See sec.4.7.2 of JVM spec.
// We preserve ConstantValue attributes because they are required for
// compile-time constant propagation.
struct ConstantValueAttribute : Attribute {
static ConstantValueAttribute* Read(const u1 *&p, Constant *attribute_name) {
ConstantValueAttribute *attr = new ConstantValueAttribute;
attr->attribute_name_ = attribute_name;
attr->constantvalue_ = constant(get_u2be(p));
return attr;
}
void Write(u1 *&p) {
WriteProlog(p, 2);
put_u2be(p, constantvalue_->slot());
}
Constant *constantvalue_;
};
// See sec.4.7.9 of JVM spec.
// We preserve Signature attributes because they are required by the
// compiler for type-checking of generics.
struct SignatureAttribute : Attribute {
static SignatureAttribute* Read(const u1 *&p, Constant *attribute_name) {