Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 94ed69b75ab670ee4e8a4d284f21c08488eb634b (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
/*******************************************************************************
 * Copyright (c) 2009, 2012 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.commands;

import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.tcf.internal.debug.ui.Activator;
import org.eclipse.tcf.internal.debug.ui.ImageCache;
import org.eclipse.tcf.internal.debug.ui.model.ICastToType;
import org.eclipse.tcf.internal.debug.ui.model.TCFNode;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.services.ISymbols;
import org.eclipse.tcf.util.TCFDataCache;
import org.eclipse.ui.IWorkbenchWindow;

public class CastToArrayCommand extends AbstractActionDelegate {

    private static class CastToTypeInputValidator implements IInputValidator {

        public CastToTypeInputValidator() {
        }

        public String isValid(String new_text) {
            try {
                if (new_text.length() == 0) return "";
                int i = Integer.parseInt(new_text);
                if (i < 1) return "Array length must be >= 1";
            }
            catch (Exception x) {
                return "Invalid number";
            }
            return null;
        }
    }

    private static class CastToTypeDialog extends InputDialog {

        public CastToTypeDialog(Shell shell, String initial_value) {
            super(shell, "Cast To Array", "Enter array length",
                    initial_value, new CastToTypeInputValidator() );
        }

        @Override
        protected void configureShell(Shell shell) {
            super.configureShell(shell);
            shell.setImage(ImageCache.getImage(ImageCache.IMG_TCF));
        }
    }

    private static class GetBaseTypeName implements Runnable {
        final TCFNode node;
        String base_type_name;
        String cur_length;

        GetBaseTypeName(TCFNode n) {
            node = n instanceof ICastToType ? n : null;
            if (node == null) done();
            else Protocol.invokeLater(this);
        }

        public void run() {
            cur_length = null;
            String cast = node.getModel().getCastToType(node.getID());
            if (cast != null) {
                if (cast.endsWith("]")) {
                    int i = cast.lastIndexOf('[');
                    if (i > 0) {
                        cur_length = cast.substring(i + 1, cast.length() - 1);
                        done(cast.substring(0, i));
                        return;
                    }
                }
                done(null);
            }
            TCFDataCache<ISymbols.Symbol> type_cache = ((ICastToType)node).getType();
            if (!type_cache.validate(this)) return;
            ISymbols.Symbol type_data = type_cache.getData();
            if (type_data == null || type_data.getTypeClass() != ISymbols.TypeClass.pointer) {
                done(null);
            }
            else {
                for (int i = 0;; i++) {
                    TCFDataCache<ISymbols.Symbol> base_type_cache = node.getModel().getSymbolInfoCache(type_data.getBaseTypeID());
                    if (!base_type_cache.validate(this)) return;
                    ISymbols.Symbol base_type_data = base_type_cache.getData();
                    if (base_type_data == null) {
                        done(null);
                        return;
                    }
                    else {
                        if (base_type_data.getName() != null) {
                            done(makePtrTypeName(base_type_data.getName(), i));
                            return;
                        }
                        else if (base_type_data.getTypeClass() == ISymbols.TypeClass.pointer) {
                            type_data = base_type_data;
                        }
                        else if (!base_type_data.getID().equals(base_type_data.getTypeID())) {
                            // modified type without name, like "volatile int"
                            base_type_cache = node.getModel().getSymbolInfoCache(base_type_data.getTypeID());
                            if (!base_type_cache.validate(this)) return;
                            base_type_data = base_type_cache.getData();
                            if (base_type_data != null && base_type_data.getName() != null) {
                                done(makePtrTypeName(base_type_data.getName(), i));
                            }
                            else {
                                done(null);
                            }
                            return;
                        }
                        else {
                            done(null);
                            return;
                        }
                    }
                }
            }
        }

        private String makePtrTypeName(String base, int cnt) {
            StringBuffer bf = new StringBuffer();
            bf.append(base);
            if (cnt > 0) {
                bf.append(' ');
                while (cnt > 0) {
                    bf.append('*');
                    cnt--;
                }
            }
            return bf.toString();
        }

        private void done(String base_type_name) {
            this.base_type_name = base_type_name;
            Display.getDefault().asyncExec(new Runnable() {
                public void run() {
                    done();
                }
            });
        }

        void done() {
        }
    }

    @Override
    protected void run() {
        new GetBaseTypeName(getSelectedNode()) {
            void done() {
                if (base_type_name == null) return;
                final IWorkbenchWindow window = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow();
                if (window == null) return;
                CastToTypeDialog dialog = new CastToTypeDialog(window.getShell(), cur_length);
                if (dialog.open() != Window.OK) return;
                final String new_type = dialog.getValue().trim();
                Protocol.invokeLater(new Runnable() {
                    public void run() {
                        node.getModel().setCastToType(node.getID(), base_type_name + "[" + new_type + "]");
                    }
                });
            }
        };
    }

    @Override
    protected void selectionChanged() {
        new GetBaseTypeName(getSelectedNode()) {
            void done() {
                getAction().setEnabled(base_type_name != null);
            }
        };
    }
}

Back to the top