Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8677dd028405e0859e21cbc8f714667319d0f409 (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
/*******************************************************************************
 * Copyright (c) 2000, 2004 QNX Software Systems and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     QNX Software Systems - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.debug.internal.core.model;

import org.eclipse.cdt.debug.core.CDebugCorePlugin;
import org.eclipse.cdt.debug.core.ICDebugConstants;
import org.eclipse.cdt.debug.core.cdi.CDIException;
import org.eclipse.cdt.debug.core.cdi.event.ICDIEvent;
import org.eclipse.cdt.debug.core.cdi.event.ICDIResumedEvent;
import org.eclipse.cdt.debug.core.cdi.model.ICDIExpression;
import org.eclipse.cdt.debug.core.cdi.model.ICDIObject;
import org.eclipse.cdt.debug.core.cdi.model.ICDITarget;
import org.eclipse.cdt.debug.core.cdi.model.ICDIValue;
import org.eclipse.cdt.debug.core.cdi.model.ICDIVariableDescriptor;
import org.eclipse.cdt.debug.core.cdi.model.type.ICDIArrayValue;
import org.eclipse.cdt.debug.core.model.CVariableFormat;
import org.eclipse.cdt.debug.core.model.ICStackFrame;
import org.eclipse.cdt.debug.core.model.ICType;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.IExpression;
import org.eclipse.debug.core.model.IValue;

/**
 * Represents an expression in the CDI model.
 */
public class CExpression extends CVariable implements IExpression {

	private ICDIExpression fCDIExpression;
	
	private CStackFrame fStackFrame;

	private IValue fValue;

	private ICType fType;

	/**
	 * Constructor for CExpression.
	 */
	public CExpression( CStackFrame frame, ICDIExpression cdiExpression, ICDIVariableDescriptor varObject ) {
		super( frame, varObject );
		setFormat( CVariableFormat.getFormat( CDebugCorePlugin.getDefault().getPluginPreferences().getInt( ICDebugConstants.PREF_DEFAULT_EXPRESSION_FORMAT ))) ;
		fCDIExpression = cdiExpression;
		fStackFrame = frame;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IExpression#getExpressionText()
	 */
	public String getExpressionText() {
		return fCDIExpression.getExpressionText();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.cdi.event.ICDIEventListener#handleDebugEvents(org.eclipse.cdt.debug.core.cdi.event.ICDIEvent[])
	 */
	public void handleDebugEvents( ICDIEvent[] events ) {
		for( int i = 0; i < events.length; i++ ) {
			ICDIEvent event = events[i];
			if ( event instanceof ICDIResumedEvent ) {
				ICDIObject source = event.getSource();
				if ( source != null ) {
					ICDITarget cdiTarget = source.getTarget();
					if (  getCDITarget().equals( cdiTarget ) ) {
						setChanged( false );
						resetValue();
					}
				}
			}
		}
		super.handleDebugEvents( events );
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.model.ICVariable#isEnabled()
	 */
	public boolean isEnabled() {
		return true;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.model.ICVariable#canEnableDisable()
	 */
	public boolean canEnableDisable() {
		return true;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.internal.core.model.CVariable#isBookkeepingEnabled()
	 */
	protected boolean isBookkeepingEnabled() {
		return false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IExpression#getValue()
	 */
	public IValue getValue() {
		CStackFrame frame = (CStackFrame)getStackFrame();
		try {
			return getValue( frame );
		}
		catch( DebugException e ) {
		}
		return null;
	}

	protected synchronized IValue getValue( CStackFrame context ) throws DebugException {
		if ( fValue == null ) {
			if ( context.isSuspended() ) {
				try {
					ICDIValue value = fCDIExpression.getValue( context.getCDIStackFrame() );
					if ( value != null ) {
						if ( value instanceof ICDIArrayValue ) {
							ICType type = null;
							try {
								type = new CType( value.getType() );
							}
							catch( CDIException e ) {
								// ignore and use default type
							}
							if ( type != null && type.isArray() ) {
								int[] dims = type.getArrayDimensions();
								if ( dims.length > 0 && dims[0] > 0 )
									fValue = CValueFactory.createIndexedValue( this, (ICDIArrayValue)value, 0, dims[0] );
							}
						}
						else {
							fValue = CValueFactory.createValue( this, value );
						}
					}
				}
				catch( CDIException e ) {
					targetRequestFailed( e.getMessage(), null );
				}
			}
		}
		return fValue;
	}

	protected ICStackFrame getStackFrame() {
		return fStackFrame;
	}

	protected void resetValue() {
		if ( fValue instanceof AbstractCValue ) {
			((AbstractCValue)fValue).reset();
		}
		fValue = null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.internal.core.model.AbstractCVariable#getExpressionString()
	 */
	public String getExpressionString() throws DebugException {
		return getExpressionText();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.internal.core.model.AbstractCVariable#dispose()
	 */
	public void dispose() {
		if ( fCDIExpression != null ) {
			try {
				fCDIExpression.dispose();
				fCDIExpression = null;
			}
			catch( CDIException e ) {
			}
		}
		if ( fValue instanceof AbstractCValue ) {
			((AbstractCValue)fValue).dispose();
			fValue = null;
		}
		internalDispose( true );
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.model.ICVariable#getType()
	 */
	public ICType getType() throws DebugException {
		if ( fType == null ) {
			if ( fValue != null ) {
				synchronized( this ) {
					if ( fType == null ) {
						fType = ((AbstractCValue)fValue).getType();
					}
				}
			}
		}
		return fType;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IVariable#getReferenceTypeName()
	 */
	public String getReferenceTypeName() throws DebugException {
		ICType type = getType();
		return ( type != null ) ? type.getName() : ""; //$NON-NLS-1$
	}
}

Back to the top