Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 11215ca953a35df2e87d6399da5da43aa8e7ebd5 (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
/*******************************************************************************
 * Copyright (c) 2006, 2014 QNX Software Systems 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:
 *     QNX - Initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.pdom.dom.cpp;

import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.TDEF;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.getNestedType;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IName;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ISemanticProblem;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBase;
import org.eclipse.cdt.internal.core.dom.parser.ProblemType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBase;
import org.eclipse.cdt.internal.core.pdom.db.Database;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMLinkage;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMName;
import org.eclipse.core.runtime.CoreException;

/**
 * @author Doug Schaefer
 */
class PDOMCPPBase implements ICPPBase, ICPPInternalBase {
	static final int CLASS_DEFINITION = 0;
	private static final int BASECLASS_TYPE = CLASS_DEFINITION + Database.PTR_SIZE;
	private static final int NEXTBASE = BASECLASS_TYPE + Database.TYPE_SIZE;
	private static final int FLAGS = NEXTBASE + Database.PTR_SIZE;
	
	protected static final int RECORD_SIZE = FLAGS + 1;
	
	private static final int FLAGS_VISIBILITY_MASK = 0x03;
	private static final int FLAGS_VIRTUAL = 0x04;
	private static final int FLAGS_INHERITED_CONSTRUCTORS_SOURCE = 0x08;
	
	private final PDOMLinkage linkage;
	private final long record;
	
	private IType fCachedBaseClass;
	
	public PDOMCPPBase(PDOMLinkage linkage, long record) {
		this.linkage = linkage;
		this.record = record;
	}
	
	public PDOMCPPBase(PDOMLinkage linkage, ICPPBase base, PDOMName classDefName) throws CoreException {
		Database db = linkage.getDB();
		this.linkage = linkage;
		this.record = db.malloc(RECORD_SIZE);
		db.putRecPtr(record + CLASS_DEFINITION, classDefName.getRecord());
		linkage.storeType(record + BASECLASS_TYPE, base.getBaseClassType());
		
		byte flags = (byte) (base.getVisibility() | (base.isVirtual() ? FLAGS_VIRTUAL : 0)
				| (base.isInheritedConstructorsSource() ? FLAGS_INHERITED_CONSTRUCTORS_SOURCE : 0));
		db.putByte(record + FLAGS, flags);
	}

	private Database getDB() {
		return linkage.getDB();
	}

	public long getRecord() {
		return record;
	}
	
	public void setNextBase(PDOMCPPBase nextBase) throws CoreException {
		long rec = nextBase != null ? nextBase.getRecord() : 0;
		getDB().putRecPtr(record + NEXTBASE, rec);
	}
	
	public PDOMCPPBase getNextBase() throws CoreException {
		long rec = getDB().getRecPtr(record + NEXTBASE);
		return rec != 0 ? new PDOMCPPBase(linkage, rec) : null;
	}
	
	private int getFlags() throws CoreException {
		return getDB().getByte(record + FLAGS);
	}

	@Override
	public PDOMName getClassDefinitionName() {
		try {
			long rec = getDB().getRecPtr(record + CLASS_DEFINITION);
			if (rec != 0) {
				return new PDOMName(linkage, rec);
			}
		} catch (CoreException e) {
			CCorePlugin.log(e);
		}
		return null;
	}
	
	@Override
	public IType getBaseClassType() {
		if (fCachedBaseClass == null) {
			try {
				fCachedBaseClass= linkage.loadType(record + BASECLASS_TYPE);
			} catch (CoreException e) {
				fCachedBaseClass= new ProblemType(ISemanticProblem.TYPE_NOT_PERSISTED);
			}
		}
		return fCachedBaseClass;
	}

	@Override
	public IBinding getBaseClass() {
		IType type= getBaseClassType();
		type = getNestedType(type, TDEF);
		if (type instanceof IBinding)
			return (IBinding) type;
		return null;
	}

	@Override
	public int getVisibility() {
		try {
			return getFlags() & FLAGS_VISIBILITY_MASK;
		} catch (CoreException e) {
			CCorePlugin.log(e);
			return 0;
		}
	}

	@Override
	public boolean isVirtual() {
		try {
			return (getFlags() & FLAGS_VIRTUAL) != 0;
		} catch (CoreException e) {
			CCorePlugin.log(e);
			return false;
		}
	}

	@Override
	public boolean isInheritedConstructorsSource() {
		try {
			return (getFlags() & FLAGS_INHERITED_CONSTRUCTORS_SOURCE) != 0;
		} catch (CoreException e) {
			CCorePlugin.log(e);
			return false;
		}
	}

	public void delete() throws CoreException {
		getDB().free(record);
	}
	
	@Override
	public void setBaseClass(IBinding binding) {
		throw new UnsupportedOperationException(); 
	}
	@Override
	public void setBaseClass(IType binding) {
		throw new UnsupportedOperationException(); 
	}
	
	@Override
	public ICPPBase clone() {
		return new PDOMCPPBaseClone(this);
	}
	
	private static class PDOMCPPBaseClone implements ICPPBase, ICPPInternalBase {
		private final ICPPBase base;
		private IType baseClass;
		
		public PDOMCPPBaseClone(ICPPBase base) {
			this.base = base;
		}

		@Override
		public IBinding getBaseClass() {
			IType type= getBaseClassType();
			type = getNestedType(type, TDEF);
			if (type instanceof IBinding)
				return (IBinding) type;
			return null;
		}

		@Override
		public IType getBaseClassType() {
			if (baseClass == null) {
				baseClass=  base.getBaseClassType();
			}
			return baseClass;
		}
		
		@Override
		public IName getClassDefinitionName() {
			return base.getClassDefinitionName();
		}
		
		@Override
		public int getVisibility() {
			return base.getVisibility();
		}

		@Override
		public boolean isVirtual() {
			return base.isVirtual();
		}

		@Override
		public boolean isInheritedConstructorsSource() {
			return base.isInheritedConstructorsSource();
		}

		@Override
		public void setBaseClass(IBinding binding) {
			if (binding instanceof IType)
				baseClass = (IType) binding;
		}

		@Override
		public void setBaseClass(IType binding) {
			baseClass = binding;
		}

		@Override
		public ICPPBase clone() {
			return new PDOMCPPBaseClone(this);
		}
	}
}

Back to the top