Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5e91fcc7148cd172c50a84526773eb71653b71a8 (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
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
/*******************************************************************************
 * Copyright (c) 2004, 2011 IBM Corporation 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:
 *    John Camelon (IBM) - Initial API and implementation
 *    Markus Schorn (Wind River Systems)
 *    Mike Kucera (IBM)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp;

import static org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory.PRVALUE;

import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTArrayDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTArrayModifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpressionList;
import org.eclipse.cdt.core.dom.ast.IASTImplicitName;
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTInitializerClause;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IArrayType;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorInitializer;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.dom.parser.ASTQueries;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguityParent;
import org.eclipse.cdt.internal.core.dom.parser.Value;
import org.eclipse.cdt.internal.core.dom.parser.c.CASTExpressionList;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPSemantics;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.EvalFixed;
import org.eclipse.core.runtime.Assert;


public class CPPASTNewExpression extends ASTNode implements ICPPASTNewExpression, IASTAmbiguityParent {
    private IASTInitializerClause[] placement;
    private IASTTypeId typeId;
    private IASTInitializer initializer;
    private IASTImplicitName[] implicitNames = null;
    private boolean isGlobal;
    private boolean isNewTypeId;
	
    private IASTExpression[] cachedArraySizes;
	private ICPPEvaluation fEvaluation;
    
    public CPPASTNewExpression() {
	}

	public CPPASTNewExpression(IASTInitializerClause[] placement, IASTInitializer initializer, IASTTypeId typeId) {
		setPlacementArguments(placement);
		setTypeId(typeId);
		setInitializer(initializer);
	}

	@Override
	public CPPASTNewExpression copy() {
		return copy(CopyStyle.withoutLocations);
	}

	@Override
	public CPPASTNewExpression copy(CopyStyle style) {
		CPPASTNewExpression copy = new CPPASTNewExpression();
		copy.setIsGlobal(isGlobal);
		copy.setIsNewTypeId(isNewTypeId);
		if (placement != null) {
			IASTInitializerClause[] plcmt = new IASTInitializerClause[placement.length];
			for (int i = 0; i < placement.length; i++) {
				plcmt[i] = placement[i].copy(style);
			}
			copy.setPlacementArguments(plcmt);
		}
		copy.setTypeId(typeId == null ? null : typeId.copy(style));
		copy.setInitializer(initializer == null ? null : initializer.copy(style));
		copy.setOffsetAndLength(this);
		if (style == CopyStyle.withLocations) {
			copy.setCopyLocation(this);
		}
		return copy;
	}

	@Override
	public boolean isGlobal() {
        return isGlobal;
    }

    @Override
	public void setIsGlobal(boolean value) {
        assertNotFrozen();
        isGlobal = value;
    }

    @Override
	public IASTInitializerClause[] getPlacementArguments() {
    	return placement;
    }
    
    @Override
	public void setPlacementArguments(IASTInitializerClause[] args) {
        assertNotFrozen();
        placement = args;
        if (args != null) {
        	for (IASTInitializerClause arg : args) {
				arg.setParent(this);
				arg.setPropertyInParent(NEW_PLACEMENT);
			}
		}
    }

    @Override
	public IASTInitializer getInitializer() {
        return initializer;
    }

    @Override
	public void setInitializer(IASTInitializer expression) {
        assertNotFrozen();
        initializer = expression;
        if (expression != null) {
			expression.setParent(this);
			expression.setPropertyInParent(NEW_INITIALIZER);
		}
    }

    @Override
	public IASTTypeId getTypeId() {
        return typeId;
    }

    @Override
	public void setTypeId(IASTTypeId typeId) {
        assertNotFrozen();
        this.typeId = typeId;
        if (typeId != null) {
			typeId.setParent(this);
			typeId.setPropertyInParent(TYPE_ID);
		}
    }

    @Override
	public boolean isNewTypeId() {
        return isNewTypeId;
    }

    @Override
	public void setIsNewTypeId(boolean value) {
        assertNotFrozen();
        isNewTypeId = value;
    }
    
    /**
     * @see org.eclipse.cdt.core.dom.ast.IASTImplicitNameOwner#getImplicitNames()
     */
    @Override
	public IASTImplicitName[] getImplicitNames() {
    	if (implicitNames == null) {
			ICPPFunction operatorFunction = CPPSemantics.findOverloadedOperator(this);
			if (operatorFunction == null || operatorFunction instanceof CPPImplicitFunction) {
				implicitNames = IASTImplicitName.EMPTY_NAME_ARRAY;
			} else {
				CPPASTImplicitName operatorName = new CPPASTImplicitName(operatorFunction.getNameCharArray(), this);
				operatorName.setOperator(true);
				operatorName.setBinding(operatorFunction);
				operatorName.setOffsetAndLength(getOffset(), 3);
				implicitNames = new IASTImplicitName[] { operatorName };
			}
    	}
    	
    	return implicitNames;  
    }

    /**
	 * Returns true if this expression is allocating an array.
	 * @since 5.1
	 */
	@Override
	public boolean isArrayAllocation() {
		IASTTypeId typeId= getTypeId();
		if (typeId != null) {
			IASTDeclarator dtor= typeId.getAbstractDeclarator();
			if (dtor != null) {
				dtor= ASTQueries.findTypeRelevantDeclarator(dtor);
				return dtor instanceof IASTArrayDeclarator;
			}
		}
		return false;
	}

    @Override
	public boolean accept(ASTVisitor action) {
        if (action.shouldVisitExpressions) {
		    switch (action.visit(this)) {
	            case ASTVisitor.PROCESS_ABORT: return false;
	            case ASTVisitor.PROCESS_SKIP:  return true;
	            default: break;
	        }
		}
        
        if (action.shouldVisitImplicitNames) { 
        	for (IASTImplicitName name : getImplicitNames()) {
        		if (!name.accept(action)) return false;
        	}
        }
        
		if (placement != null) {
			for (IASTInitializerClause arg : placement) {
				if (!arg.accept(action))
					return false;
			}
		}
		if (typeId != null && !typeId.accept(action))
			return false;

		if (initializer != null && !initializer.accept(action))
			return false;       
        
        if (action.shouldVisitExpressions) {
		    switch (action.leave(this)) {
	            case ASTVisitor.PROCESS_ABORT: return false;
	            case ASTVisitor.PROCESS_SKIP:  return true;
	            default: break;
	        }
		}
        return true;
    }

	@Override
	public void replace(IASTNode child, IASTNode other) {
		if (placement != null) {
			for (int i = 0; i < placement.length; ++i) {
				if (child == placement[i]) {
					other.setPropertyInParent(child.getPropertyInParent());
					other.setParent(child.getParent());
					placement[i] = (IASTExpression) other;
				}
			}
		}
	}
    
	@Override
	public ICPPEvaluation getEvaluation() {
		if (fEvaluation == null) {
			IType t= CPPVisitor.createType(getTypeId());
			if (t instanceof IArrayType) {
				t= ((IArrayType) t).getType();
			}
			fEvaluation= new EvalFixed(new CPPPointerType(t), PRVALUE, Value.UNKNOWN);
		}
		return fEvaluation;
	}
	
    @Override
	public IType getExpressionType() {
    	return getEvaluation().getTypeOrFunctionSet(this);
    }

	@Override
	public boolean isLValue() {
		return false;
	}
	
	@Override
	public ValueCategory getValueCategory() {
		return PRVALUE;
	}

	@Override
	@Deprecated
	public IASTExpression[] getNewTypeIdArrayExpressions() {
		if (cachedArraySizes == null) {
			if (typeId != null) {
				IASTDeclarator dtor = ASTQueries.findInnermostDeclarator(typeId.getAbstractDeclarator());
				if (dtor instanceof IASTArrayDeclarator) {
					IASTArrayDeclarator ad = (IASTArrayDeclarator) dtor;
					IASTArrayModifier[] ams = ad.getArrayModifiers();
					cachedArraySizes = new IASTExpression[ams.length];
					for (int i = 0; i < ams.length; i++) {
						IASTArrayModifier am = ams[i];
						cachedArraySizes[i] = am.getConstantExpression();
					}
					return cachedArraySizes;
				}
			}
			cachedArraySizes = IASTExpression.EMPTY_EXPRESSION_ARRAY;
		}
		return cachedArraySizes;
	}

    @Override
	@Deprecated
    public void addNewTypeIdArrayExpression(IASTExpression expression) {
        assertNotFrozen();
    	Assert.isNotNull(typeId);
    	IASTDeclarator dtor= ASTQueries.findInnermostDeclarator(typeId.getAbstractDeclarator());
    	if (dtor instanceof IASTArrayDeclarator == false) {
    		Assert.isNotNull(dtor);
    		Assert.isTrue(dtor.getParent() == typeId);
    		IASTArrayDeclarator adtor= new CPPASTArrayDeclarator(dtor.getName());
    		IASTPointerOperator[] ptrOps= dtor.getPointerOperators();
    		for (IASTPointerOperator ptr : ptrOps) {
        		adtor.addPointerOperator(ptr);				
			}
    		typeId.setAbstractDeclarator(adtor);
    		dtor= adtor;
    	}
    	IASTArrayModifier mod= new CPPASTArrayModifier(expression);
    	((ASTNode) mod).setOffsetAndLength((ASTNode)expression);
    	((IASTArrayDeclarator) dtor).addArrayModifier(mod);
    }

	@Override
	@Deprecated
    public IASTExpression getNewPlacement() {
    	if (placement == null || placement.length == 0)
    		return null;
    	if (placement.length == 1) {
    		if (placement[0] instanceof IASTExpression)
    			return (IASTExpression) placement[0];
    		return null;
    	}
    		
    	CASTExpressionList result= new CASTExpressionList();
    	for (IASTInitializerClause arg : placement) {
    		if (arg instanceof IASTExpression) {
    			result.addExpression(((IASTExpression) arg).copy());
    		}
    	}
    	result.setParent(this);
    	result.setPropertyInParent(NEW_PLACEMENT);
        return result;
    }
	
	@Override
	@Deprecated
    public void setNewPlacement(IASTExpression expression) {
        assertNotFrozen();
        if (expression == null) {
        	setPlacementArguments(null);
        } else if (expression instanceof IASTExpressionList) {
        	setPlacementArguments(((IASTExpressionList) expression).getExpressions());
        } else {
        	setPlacementArguments(new IASTExpression[] {expression});
        }
    }
    
	@Override
	@Deprecated
    public IASTExpression getNewInitializer() {
        if (initializer == null || initializer instanceof IASTExpression) {
        	return (IASTExpression) initializer;
        }
        if (initializer instanceof ICPPASTConstructorInitializer) {
       		IASTExpression expr= ((ICPPASTConstructorInitializer) initializer).getExpression();
       		if (expr == null) {
       			expr= new CPPASTExpressionList();
       		} else {
       			expr= expr.copy();
       		}
       		expr.setParent(this);
       		expr.setPropertyInParent(NEW_INITIALIZER);
       		return expr;
        }
        return null;
    }

	@Override
	@Deprecated
    public void setNewInitializer(IASTExpression expression) {
        assertNotFrozen();
        if (expression == null) {
        	setInitializer(null);
        } else if (expression instanceof IASTInitializer) {
        	setInitializer((IASTInitializer) expression);
        } else {
        	CPPASTConstructorInitializer ctorInit= new CPPASTConstructorInitializer();
        	ctorInit.setExpression(expression);
        	ctorInit.setOffsetAndLength((ASTNode) expression);
        	setInitializer(ctorInit);
        }
    }
}

Back to the top