Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b0a7cd33792f119020d759ba09e27be470ad0140 (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
/*******************************************************************************
 * Copyright (c) 2007, 2013 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:
 *     Markus Schorn - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.core.parser.scanner;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit.IDependencyTree.IASTInclusionNode;

/**
 * Various location contexts which are suitable for interpreting local offsets. These offsets are
 * converted in a global sequence-number to make all AST nodes comparable with each other.
 * @since 5.0
 */
abstract class LocationCtx implements ILocationCtx {
	final LocationCtxContainer fParent;
	/**
	 * The first sequence number used by this context.
	 */
	final int fSequenceNumber;
	/**
	 * The offset of the denotation of this context in the parent's source. This is no sequence
	 * number.
	 */
	final int fOffsetInParent;
	/**
	 * The end-offset of the denotation of this context in the parent's source. This is no sequence
	 * number.
	 */
	final int fEndOffsetInParent;

	public LocationCtx(LocationCtxContainer parent, int parentOffset, int parentEndOffset, int sequenceNumber) {
		fParent= parent;
		fOffsetInParent= parentOffset;
		fEndOffsetInParent= parentEndOffset;
		fSequenceNumber= sequenceNumber;
		if (parent != null) {
			parent.addChild(this);
		}
	}

	@Override
	public String getFilePath() {
		return fParent.getFilePath();
	}

	@Override
	final public ILocationCtx getParent() {
		return fParent;
	}

	/**
	 * Returns the amount of sequence numbers occupied by this context including its children.
	 */
	public abstract int getSequenceLength();

	/**
	 * Converts an offset within this context to the sequence number. In case there are
	 * child-contexts behind the given offset, you need to set checkChildren to <code>true</code>.
	 */
	public int getSequenceNumberForOffset(int offset, boolean checkChildren) {
		return fSequenceNumber + offset;
	}

	/**
	 * When a child-context is finished it reports its total sequence length, such that offsets in
	 * this context can be converted to sequence numbers.
	 */
	public void addChildSequenceLength(int childLength) {
		assert false;
	}

	/**
	 * Returns the line number for an offset within this context. Not all contexts support line
	 * numbers, so this may return 0.
	 */
	public int getLineNumber(int offset) {
		return 0;
	}

	/**
	 * Returns the minimal context containing the specified range, assuming that it is contained in
	 * this context.
	 */
	public LocationCtx findSurroundingContext(int sequenceNumber, int length) {
		return this;
	}

	/**
	 * Returns the macro-expansion surrounding or augmenting the given range, or <code>null</code>.
	 */
	public LocationCtxMacroExpansion findEnclosingMacroExpansion(int sequenceNumber, int length) {
		return null;
	}

	public int convertToSequenceEndNumber(int sequenceNumber) {
		// if the sequence number is the beginning of this context, skip the denotation of this
		// context in the parent.
		if (sequenceNumber == fSequenceNumber)
			return sequenceNumber - fEndOffsetInParent + fOffsetInParent;
		return sequenceNumber;
	}

	/**
	 * Returns the minimal file location containing the specified sequence number range, assuming
	 * that it is contained in this context.
	 */
	public ASTFileLocation findMappedFileLocation(int sequenceNumber, int length) {
		return fParent.createMappedFileLocation(fOffsetInParent, fEndOffsetInParent - fOffsetInParent);
	}

	/**
	 * Returns the file location containing the specified offset range in this context.
	 */
	public ASTFileLocation createMappedFileLocation(int offset, int length) {
		return fParent.createMappedFileLocation(fOffsetInParent, fEndOffsetInParent - fOffsetInParent);
	}

	/**
	 * Returns the sequence of file locations spanning the given range.
	 * Assumes that the range starts within this context.
	 */
	public abstract void collectLocations(int sequenceNumber, int length,
			ArrayList<IASTNodeLocation> sofar);

	/**
	 * Support for the dependency tree, add inclusion statements found in this context.
	 */
	public void getInclusions(ArrayList<IASTInclusionNode> result) {
	}

	/**
	 * Support for the dependency tree, returns inclusion statement that created this context,
	 * or <code>null</code>.
	 */
	@Override
	public ASTInclusionStatement getInclusionStatement() {
		return null;
	}

	public Collection<LocationCtx> getChildren() {
		return Collections.emptySet();
	}

	public boolean isSourceFile() {
		if (fParent == null)
			return false;
		return fParent.isSourceFile();
	}
}

Back to the top