Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 08262e57d240a9014fc92c691450c450dd887693 (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
/*******************************************************************************
 * Copyright (c) 2008, 2011 Institute for Software, HSR Hochschule fuer Technik  
 * Rapperswil, University of applied sciences 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: 
 *     Institute for Software - initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.changegenerator;

import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpressionList;
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind;
import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ExpressionWriter;
import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.MacroExpansionHandler;
import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.Scribe;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;

public class ModifiedASTExpressionWriter extends ExpressionWriter {
	private final ASTModificationHelper modificationHelper;

	public ModifiedASTExpressionWriter(Scribe scribe, ASTVisitor visitor, MacroExpansionHandler macroHandler,
			ModificationScopeStack stack, NodeCommentMap commentMap) {
		super(scribe, visitor, macroHandler, commentMap);
		this.modificationHelper = new ASTModificationHelper(stack);
	}

	@Override
	protected void writeExpressions(IASTExpressionList expList, IASTExpression[] expressions) {
		IASTExpression[] modifiedExpressions = modificationHelper.createModifiedChildArray(expList,
				expressions, IASTExpression.class, commentMap);
		super.writeExpressions(expList, modifiedExpressions);
	}

	@Override
	protected IASTInitializer getNewInitializer(ICPPASTNewExpression newExp) {
		IASTInitializer initializer = newExp.getInitializer();
		if (initializer != null) {
			for (ASTModification childModification : modificationHelper.modificationsForNode(initializer)) {
				switch (childModification.getKind()) {
				case REPLACE:
					if (childModification.getNewNode() instanceof IASTInitializer) {
						return (IASTInitializer) childModification.getNewNode();
					}
					break;
				case INSERT_BEFORE:
					throw new UnhandledASTModificationException(childModification);

				case APPEND_CHILD:
					throw new UnhandledASTModificationException(childModification);
				}
			}
		} else {
			for (ASTModification parentModification : modificationHelper.modificationsForNode(newExp)) {
				if (parentModification.getKind() == ModificationKind.APPEND_CHILD) {
					IASTNode newNode = parentModification.getNewNode();
					if (newNode instanceof IASTInitializer) {
						return (IASTInitializer) newNode;
					}
				}
			}
		}
		return initializer;
	}
}

Back to the top