Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 97d205587cabb8c010bc42e2074e8faa6f74c26e (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2008, 2013 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.internal.debug.ui.model;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.tcf.debug.ui.ITCFPrettyExpressionProvider;
import org.eclipse.tcf.services.IExpressions;
import org.eclipse.tcf.services.ISymbols;
import org.eclipse.tcf.util.TCFDataCache;

public class TCFChildrenSubExpressions extends TCFChildren {

    private final int par_level;
    private final int par_offs;
    private final int par_size;

    TCFChildrenSubExpressions(TCFNode node, int par_level, int par_offs, int par_size) {
        super(node, 128);
        this.par_level = par_level;
        this.par_offs = par_offs;
        this.par_size = par_size;
    }

    void onSuspended(boolean func_call) {
        reset();
        for (TCFNode n : getNodes()) {
            if (n instanceof TCFNodeExpression) ((TCFNodeExpression)n).onSuspended(func_call);
            if (n instanceof TCFNodeArrayPartition) ((TCFNodeArrayPartition)n).onSuspended(func_call);
        }
    }

    void onValueChanged() {
        reset();
        for (TCFNode n : getNodes()) {
            if (n instanceof TCFNodeExpression) ((TCFNodeExpression)n).onValueChanged();
            if (n instanceof TCFNodeArrayPartition) ((TCFNodeArrayPartition)n).onValueChanged();
        }
    }

    void onRegisterValueChanged() {
        reset();
        for (TCFNode n : getNodes()) {
            if (n instanceof TCFNodeExpression) ((TCFNodeExpression)n).onRegisterValueChanged();
            if (n instanceof TCFNodeArrayPartition) ((TCFNodeArrayPartition)n).onRegisterValueChanged();
        }
    }

    void onMemoryChanged() {
        reset();
        for (TCFNode n : getNodes()) {
            if (n instanceof TCFNodeExpression) ((TCFNodeExpression)n).onMemoryChanged();
            if (n instanceof TCFNodeArrayPartition) ((TCFNodeArrayPartition)n).onMemoryChanged();
        }
    }

    void onMemoryMapChanged() {
        reset();
        for (TCFNode n : getNodes()) {
            if (n instanceof TCFNodeExpression) ((TCFNodeExpression)n).onMemoryMapChanged();
            if (n instanceof TCFNodeArrayPartition) ((TCFNodeArrayPartition)n).onMemoryMapChanged();
        }
    }

    void onCastToTypeChanged() {
        cancel();
        TCFNode a[] = getNodes().toArray(new TCFNode[getNodes().size()]);
        for (int i = 0; i < a.length; i++) a[i].dispose();
    }

    TCFNodeExpression getField(String field_id, boolean deref) {
        assert field_id != null;
        for (TCFNode n : getNodes()) {
            TCFNodeExpression e = (TCFNodeExpression)n;
            if (field_id.equals(e.getFieldID()) && e.isDeref() == deref) return e;
        }
        if (isValid()) return null;
        TCFNodeExpression e = new TCFNodeExpression(node, null, field_id, null, null, -1, deref);
        add(e);
        return e;
    }

    private boolean findFields(ISymbols.Symbol type, Map<String,TCFNode> map, boolean deref) {
        TCFDataCache<String[]> children_cache = node.model.getSymbolChildrenCache(type.getID());
        if (children_cache == null) return true;
        if (!children_cache.validate(this)) return false;
        String[] children = children_cache.getData();
        if (children == null) return true;
        TCFDataCache<?> pending = null;
        for (String id : children) {
            TCFDataCache<ISymbols.Symbol> sym_cache = node.model.getSymbolInfoCache(id);
            if (!sym_cache.validate()) {
                pending = sym_cache;
            }
            else {
                ISymbols.Symbol sym_data = sym_cache.getData();
                if (sym_data == null) continue;
                if (sym_data.getSymbolClass() != ISymbols.SymbolClass.reference) continue;
                if (sym_data.getFlag(ISymbols.SYM_FLAG_ARTIFICIAL)) continue;
                if (sym_data.getName() == null && !sym_data.getFlag(ISymbols.SYM_FLAG_INHERITANCE)) {
                    if (!findFields(sym_data, map, deref)) return false;
                }
                else {
                    TCFNodeExpression n = getField(id, deref);
                    n.setSortPosition(map.size());
                    map.put(n.id, n);
                }
            }
        }
        if (pending == null) return true;
        pending.wait(this);
        return false;
    }

    private TCFNodeExpression findReg(String reg_id) {
        assert reg_id != null;
        for (TCFNode n : getNodes()) {
            TCFNodeExpression e = (TCFNodeExpression)n;
            if (reg_id.equals(e.getRegisterID())) return e;
        }
        return null;
    }

    private boolean findRegs(TCFNodeRegister reg_node, Map<String,TCFNode> map) {
        TCFChildren reg_children = reg_node.getChildren();
        if (!reg_children.validate(this)) return false;
        for (TCFNode subnode : reg_children.toArray()) {
            TCFNodeExpression n = findReg(subnode.id);
            if (n == null) add(n = new TCFNodeExpression(node, null, null, null, subnode.id, -1, false));
            n.setSortPosition(map.size());
            map.put(n.id, n);
        }
        return true;
    }

    private TCFNodeExpression findIndex(int index, boolean deref) {
        assert index >= 0;
        for (TCFNode n : getNodes()) {
            TCFNodeExpression e = (TCFNodeExpression)n;
            if (e.getIndex() == index && e.isDeref() == deref) return e;
        }
        return null;
    }

    private TCFNodeArrayPartition findPartition(int offs, int size) {
        assert offs >= 0;
        for (TCFNode n : getNodes()) {
            TCFNodeArrayPartition e = (TCFNodeArrayPartition)n;
            if (e.getOffset() == offs && e.getSize() == size) return e;
        }
        return null;
    }

    private TCFNodeExpression findScript(String s) {
        // TODO: need faster search
        for (TCFNode n : getNodes()) {
            if (n instanceof TCFNodeExpression) {
                TCFNodeExpression e = (TCFNodeExpression)n;
                if (s.equals(e.getScript())) return e;
            }
        }
        return null;
    }

    @Override
    protected boolean startDataRetrieval() {
        assert !isDisposed();
        TCFNode exp = node;
        while (!(exp instanceof TCFNodeExpression)) exp = exp.parent;
        for (ITCFPrettyExpressionProvider p : TCFPrettyExpressionProvider.getProviders()) {
            TCFDataCache<String[]> c = p.getChildren(exp);
            if (c != null) {
                if (!c.validate(this)) return false;
                if (c.getError() == null && c.getData() != null) {
                    int i = 0;
                    HashMap<String,TCFNode> data = new HashMap<String,TCFNode>();
                    for (String s : c.getData()) {
                        TCFNodeExpression n = findScript(s);
                        if (n == null) n = new TCFNodeExpression(node, s, null, null, null, -1, false);
                        n.setSortPosition(i++);
                        data.put(n.id, n);
                    }
                    set(null, null, data);
                    return true;
                }
            }
        }
        TCFDataCache<ISymbols.Symbol> type_cache = ((TCFNodeExpression)exp).getType();
        if (!type_cache.validate(this)) return false;
        ISymbols.Symbol type_data = type_cache.getData();
        if (type_data == null) {
            HashMap<String,TCFNode> data = new HashMap<String,TCFNode>();
            TCFDataCache<IExpressions.Value> val_cache = ((TCFNodeExpression)exp).getValue();
            if (!val_cache.validate(this)) return false;
            IExpressions.Value val_data = val_cache.getData();
            if (val_data != null) {
                String reg_id = val_data.getRegisterID();
                if (reg_id != null) {
                    if (!node.model.createNode(reg_id, this)) return false;
                    if (isValid()) return true;
                    TCFNodeRegister reg_node = (TCFNodeRegister)node.model.getNode(reg_id);
                    if (!findRegs(reg_node, data)) return false;
                }
            }
            set(null, null, data);
            return true;
        }
        ISymbols.TypeClass type_class = type_data.getTypeClass();
        Map<String,TCFNode> data = new HashMap<String,TCFNode>();
        if (par_level > 0 && type_class != ISymbols.TypeClass.array) {
            // Nothing
        }
        else if (type_class == ISymbols.TypeClass.composite) {
            if (!findFields(type_data, data, false)) return false;
        }
        else if (type_class == ISymbols.TypeClass.array) {
            int offs = par_level > 0 ? par_offs : 0;
            int size = par_level > 0 ? par_size : type_data.getLength();
            if (size <= 100) {
                for (int i = offs; i < offs + size; i++) {
                    TCFNodeExpression n = findIndex(i, false);
                    if (n == null) n = new TCFNodeExpression(node, null, null, null, null, i, false);
                    n.setSortPosition(i);
                    data.put(n.id, n);
                }
            }
            else {
                int next_size = 100;
                while (size / next_size > 100) next_size *= 100;
                for (int i = offs; i < offs + size; i += next_size) {
                    int sz = next_size;
                    if (i + sz > offs + size) sz = offs + size - i;
                    TCFNodeArrayPartition n = findPartition(i, sz);
                    if (n == null) n = new TCFNodeArrayPartition(node, par_level + 1, i, sz);
                    data.put(n.id, n);
                }
            }
        }
        else if (type_class == ISymbols.TypeClass.pointer) {
            TCFDataCache<IExpressions.Value> val_cache = ((TCFNodeExpression)exp).getValue();
            if (!val_cache.validate(this)) return false;
            IExpressions.Value val_data = val_cache.getData();
            if (val_data != null && !isNull(val_data.getValue())) {
                TCFDataCache<ISymbols.Symbol> base_type_cache = node.model.getSymbolInfoCache(type_data.getBaseTypeID());
                if (base_type_cache != null) {
                    if (!base_type_cache.validate(this)) return false;
                    ISymbols.Symbol base_type_data = base_type_cache.getData();
                    if (base_type_data != null && base_type_data.getTypeClass() != ISymbols.TypeClass.function && base_type_data.getSize() > 0) {
                        if (base_type_data.getTypeClass() == ISymbols.TypeClass.composite) {
                            if (!findFields(base_type_data, data, true)) return false;
                        }
                        else {
                            TCFNodeExpression n = findIndex(0, true);
                            if (n == null) n = new TCFNodeExpression(node, null, null, null, null, 0, true);
                            n.setSortPosition(0);
                            data.put(n.id, n);
                        }
                    }
                }
            }
        }
        set(null, null, data);
        return true;
    }

    private boolean isNull(byte[] data) {
        if (data == null) return true;
        for (byte b : data) {
            if (b != 0) return false;
        }
        return true;
    }
}

Back to the top