Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f2f582b3f0964c7da6edb86dba63807d1a0b4ceb (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
/*******************************************************************************
 * Copyright (c) 2008, 2014 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.pdom.dom;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.index.IIndexFile;
import org.eclipse.cdt.core.index.IIndexMacro;
import org.eclipse.cdt.core.index.IIndexMacroContainer;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.index.IIndexBindingConstants;
import org.eclipse.cdt.internal.core.index.IIndexFragment;
import org.eclipse.cdt.internal.core.index.IIndexScope;
import org.eclipse.cdt.internal.core.pdom.db.Database;
import org.eclipse.core.runtime.CoreException;

/**
 * A container collecting definitions and references for macros.
 * @since 5.0
 */
public class PDOMMacroContainer extends PDOMNamedNode implements IIndexMacroContainer, IPDOMBinding {
	private static final int FIRST_DEF_OFFSET = PDOMNamedNode.RECORD_SIZE + 0; // size 4
	private static final int FIRST_REF_OFFSET = PDOMNamedNode.RECORD_SIZE + 4; // size 4

	@SuppressWarnings("hiding")
	protected static final int RECORD_SIZE = PDOMNamedNode.RECORD_SIZE + 8;

	public PDOMMacroContainer(PDOMLinkage linkage, char[] name) throws CoreException {
		super(linkage, linkage, name);
	}

	PDOMMacroContainer(PDOMLinkage linkage, long record) {
		super(linkage, record);
	}

	@Override
	public int getNodeType() {
		return IIndexBindingConstants.MACRO_CONTAINER;
	}

	@Override
	protected int getRecordSize() {
		return RECORD_SIZE;
	}

	public boolean isOrphaned() throws CoreException {
		Database db = getDB();
		return db.getRecPtr(record + FIRST_DEF_OFFSET) == 0
				&& db.getRecPtr(record + FIRST_REF_OFFSET) == 0;
	}

	public void addDefinition(PDOMMacro name) throws CoreException {
		PDOMMacro first = getFirstDefinition();
		if (first != null) {
			first.setPrevInContainer(name);
			name.setNextInContainer(first);
		}
		setFirstDefinition(name);
	}

	public void addReference(PDOMMacroReferenceName name) throws CoreException {
		PDOMMacroReferenceName first = getFirstReference();
		if (first != null) {
			first.setPrevInContainer(name);
			name.setNextInContainer(first);
		}
		setFirstReference(name);
	}

	public PDOMMacro getFirstDefinition() throws CoreException {
		long namerec = getDB().getRecPtr(record + FIRST_DEF_OFFSET);
		return namerec != 0 ? new PDOMMacro(getLinkage(), namerec) : null;
	}

	void setFirstDefinition(PDOMMacro macro) throws CoreException {
		long namerec = macro != null ? macro.getRecord() : 0;
		getDB().putRecPtr(record + FIRST_DEF_OFFSET, namerec);
	}

	public PDOMMacroReferenceName getFirstReference() throws CoreException {
		long namerec = getDB().getRecPtr(record + FIRST_REF_OFFSET);
		return namerec != 0 ? new PDOMMacroReferenceName(getLinkage(), namerec) : null;
	}

	void setFirstReference(PDOMMacroReferenceName nextName) throws CoreException {
		long namerec = nextName != null ? nextName.getRecord() : 0;
		getDB().putRecPtr(record + FIRST_REF_OFFSET, namerec);
	}

	public IIndexMacro[] getDefinitions() throws CoreException {
		List<PDOMMacro> macros= new ArrayList<>();
		for (PDOMMacro macro= getFirstDefinition(); macro != null; macro= macro.getNextInContainer()) {
			macros.add(macro);
		}
		return macros.toArray(new IIndexMacro[macros.size()]);
	}

	@Override
	public void delete(PDOMLinkage linkage) throws CoreException {
		if (linkage != null) {
			linkage.removeMacroContainer(this);
		}
		super.delete(linkage);
	}

	@Override
	public int getBindingConstant() {
		return IIndexBindingConstants.MACRO_CONTAINER;
	}

	@Override
	public IIndexFragment getFragment() {
		return getPDOM();
	}

	@Override
	public IIndexScope getScope() {
		return null;
	}

	@Override
	public boolean hasDeclaration() throws CoreException {
		return false;
	}

	@Override
	public boolean hasDefinition() throws CoreException {
		return getDB().getRecPtr(record + FIRST_DEF_OFFSET) != 0;
	}

	@Override
	public IIndexFile getLocalToFile() throws CoreException {
		return null;
	}

	@Override
	public String[] getQualifiedName() {
		return new String[] { getName() };
	}

	@Override
	public boolean isFileLocal() throws CoreException {
		return false;
	}

	@Override
	public char[] getNameCharArray() {
		try {
			return super.getNameCharArray();
		} catch (CoreException e) {
			CCorePlugin.log(e);
		}
		return CharArrayUtils.EMPTY;
	}

	@Override
	public String getName() {
		return new String(getNameCharArray());
	}

	@Override
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Object getAdapter(Class adapter) {
		if (adapter.isAssignableFrom(PDOMMacroContainer.class)) {
			return this;
		}
		return null;
	}
}

Back to the top