Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2242208f96d647b8e07c302b45b9bf7a84a038c2 (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
/*******************************************************************************
 * Copyright (c) 2008, 2013 Wind River Systems, Inc. and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Markus Schorn - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp;

import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCatchHandler;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorChainInitializer;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionWithTryBlock;
import org.eclipse.cdt.core.parser.util.ArrayUtil;

/**
 * Represents a function definition contained in a try block.
 * @see ICPPASTFunctionWithTryBlock
 */
public class CPPASTFunctionWithTryBlock extends CPPASTFunctionDefinition implements ICPPASTFunctionWithTryBlock {
    private ICPPASTCatchHandler[] catchHandlers;
    private int catchHandlersPos= -1;

    public CPPASTFunctionWithTryBlock() {
	}

	public CPPASTFunctionWithTryBlock(IASTDeclSpecifier declSpecifier,
			IASTFunctionDeclarator declarator, IASTStatement bodyStatement) {
		super(declSpecifier, declarator, bodyStatement);
	}

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

	@Override
	public CPPASTFunctionWithTryBlock copy(CopyStyle style) {
		IASTDeclSpecifier declSpecifier = getDeclSpecifier();
		IASTFunctionDeclarator declarator = getDeclarator();
		IASTStatement bodyStatement = getBody();

		CPPASTFunctionWithTryBlock copy = new CPPASTFunctionWithTryBlock();
		copy.setDeclSpecifier(declSpecifier == null ? null : declSpecifier.copy(style));
		copy.setDeclarator(declarator == null ? null : declarator.copy(style));
		copy.setBody(bodyStatement == null ? null : bodyStatement.copy(style));

		for (ICPPASTConstructorChainInitializer initializer : getMemberInitializers()) {
			copy.addMemberInitializer(initializer == null ? null : initializer.copy(style));
		}
		for (ICPPASTCatchHandler handler : getCatchHandlers()) {
			copy.addCatchHandler(handler == null ? null : handler.copy(style));
		}

		return copy(copy, style);
	}

	@Override
	public void addCatchHandler(ICPPASTCatchHandler statement) {
        assertNotFrozen();
    	if (statement != null) {
    		catchHandlers = ArrayUtil.appendAt(ICPPASTCatchHandler.class, catchHandlers, ++catchHandlersPos, statement);
    		statement.setParent(this);
			statement.setPropertyInParent(CATCH_HANDLER);
    	}
    }

    @Override
	public ICPPASTCatchHandler[] getCatchHandlers() {
        if (catchHandlers == null) return ICPPASTCatchHandler.EMPTY_CATCHHANDLER_ARRAY;
        catchHandlers = ArrayUtil.trimAt(ICPPASTCatchHandler.class, catchHandlers, catchHandlersPos);
        return catchHandlers;
    }

    @Override
	protected boolean acceptCatchHandlers(ASTVisitor action) {
    	final ICPPASTCatchHandler[] handlers = getCatchHandlers();
        for (int i= 0; i < handlers.length; i++) {
            if (!handlers[i].accept(action))
            	return false;
        }
        return true;
    }
}

Back to the top