blob: cdf896738406a39922929d43b08a03e34c021603 [file] [log] [blame]
Mathieu Chartierc7853442015-03-27 14:35:38 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "art_field.h"
18
19#include "art_field-inl.h"
20#include "gc/accounting/card_table-inl.h"
21#include "mirror/object-inl.h"
22#include "mirror/object_array-inl.h"
23#include "runtime.h"
24#include "scoped_thread_state_change.h"
25#include "utils.h"
26#include "well_known_classes.h"
27
28namespace art {
29
30ArtField::ArtField() : access_flags_(0), field_dex_idx_(0), offset_(0) {
31 declaring_class_ = GcRoot<mirror::Class>(nullptr);
32}
33
34void ArtField::SetOffset(MemberOffset num_bytes) {
35 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
36 if (kIsDebugBuild && Runtime::Current()->IsAotCompiler() &&
37 Runtime::Current()->IsCompilingBootImage()) {
38 Primitive::Type type = GetTypeAsPrimitiveType();
39 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
40 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
41 }
42 }
43 // Not called within a transaction.
44 offset_ = num_bytes.Uint32Value();
45}
46
47void ArtField::VisitRoots(RootVisitor* visitor) {
48 declaring_class_.VisitRoot(visitor, RootInfo(kRootStickyClass));
49}
50
51ArtField* ArtField::FindInstanceFieldWithOffset(mirror::Class* klass, uint32_t field_offset) {
52 DCHECK(klass != nullptr);
53 auto* instance_fields = klass->GetIFields();
54 for (size_t i = 0, count = klass->NumInstanceFields(); i < count; ++i) {
55 if (instance_fields[i].GetOffset().Uint32Value() == field_offset) {
56 return &instance_fields[i];
57 }
58 }
59 // We did not find field in the class: look into superclass.
60 return (klass->GetSuperClass() != nullptr) ?
61 FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset) : nullptr;
62}
63
64} // namespace art