1
/*******************************************************************************
2
* Copyright (c) 2005, 2010 QNX Software Systems and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Doug Schaefer (QNX) - Initial API and implementation
10
* Markus Schorn (Wind River Systems)
12
*******************************************************************************/
13
package org.eclipse.cdt.internal.core.pdom.dom.cpp;
15
import org.eclipse.cdt.core.CCorePlugin;
16
import org.eclipse.cdt.core.dom.ast.IASTName;
17
import org.eclipse.cdt.core.dom.ast.IBinding;
18
import org.eclipse.cdt.core.dom.ast.IType;
19
import org.eclipse.cdt.core.dom.ast.IValue;
20
import org.eclipse.cdt.core.dom.ast.IVariable;
21
import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable;
22
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVariableReadWriteFlags;
23
import org.eclipse.cdt.internal.core.index.IIndexCPPBindingConstants;
24
import org.eclipse.cdt.internal.core.pdom.db.Database;
25
import org.eclipse.cdt.internal.core.pdom.dom.PDOMLinkage;
26
import org.eclipse.cdt.internal.core.pdom.dom.PDOMName;
27
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNode;
28
import org.eclipse.cdt.internal.core.pdom.dom.c.PDOMCAnnotation;
29
import org.eclipse.core.runtime.CoreException;
32
* Binding for a c++ variable in the index, serves as a base class for fields.
34
class PDOMCPPVariable extends PDOMCPPBinding implements ICPPVariable {
36
private static final int TYPE_OFFSET = PDOMCPPBinding.RECORD_SIZE;
37
private static final int VALUE_OFFSET = TYPE_OFFSET + Database.TYPE_SIZE;
38
protected static final int ANNOTATIONS = VALUE_OFFSET + Database.VALUE_SIZE; // byte
39
@SuppressWarnings("hiding")
40
protected static final int RECORD_SIZE = ANNOTATIONS + 1;
42
public PDOMCPPVariable(PDOMLinkage linkage, PDOMNode parent, IVariable variable) throws CoreException {
43
super(linkage, parent, variable.getNameCharArray());
45
// Find the type record
46
Database db = getDB();
47
setType(parent.getLinkage(), variable.getType());
48
db.putByte(record + ANNOTATIONS, encodeFlags(variable));
49
setValue(db, variable);
52
private void setValue(Database db, IVariable variable) throws CoreException {
53
IValue val= variable.getInitialValue();
54
getLinkage().storeValue(record + VALUE_OFFSET, val);
58
public void update(final PDOMLinkage linkage, IBinding newBinding) throws CoreException {
59
if (newBinding instanceof IVariable) {
60
final Database db = getDB();
61
IVariable var= (IVariable) newBinding;
62
IType newType= var.getType();
63
setType(linkage, newType);
65
db.putByte(record + ANNOTATIONS, encodeFlags(var));
70
private void setType(final PDOMLinkage linkage, IType newType) throws CoreException {
71
linkage.storeType(record+TYPE_OFFSET, newType);
74
protected byte encodeFlags(IVariable variable) {
75
return PDOMCPPAnnotation.encodeAnnotation(variable);
78
public PDOMCPPVariable(PDOMLinkage linkage, long record) {
79
super(linkage, record);
83
protected int getRecordSize() {
88
public int getNodeType() {
89
return IIndexCPPBindingConstants.CPPVARIABLE;
92
public boolean isMutable() {
93
// ISO/IEC 14882:2003 7.1.1.8
97
public IType getType() {
99
return getLinkage().loadType(record + TYPE_OFFSET);
100
} catch (CoreException e) {
106
public IValue getInitialValue() {
108
return getLinkage().loadValue(record + VALUE_OFFSET);
109
} catch (CoreException e) {
115
public boolean isAuto() {
116
return getBit(getByte(record + ANNOTATIONS), PDOMCAnnotation.AUTO_OFFSET);
119
public boolean isExtern() {
120
return getBit(getByte(record + ANNOTATIONS), PDOMCAnnotation.EXTERN_OFFSET);
123
public boolean isExternC() {
124
return getBit(getByte(record + ANNOTATIONS), PDOMCPPAnnotation.EXTERN_C_OFFSET);
127
public boolean isRegister() {
128
return getBit(getByte(record + ANNOTATIONS), PDOMCAnnotation.REGISTER_OFFSET);
131
public boolean isStatic() {
132
return getBit(getByte(record + ANNOTATIONS), PDOMCAnnotation.STATIC_OFFSET);
136
public int getAdditionalNameFlags(int standardFlags, IASTName name) {
137
if ((standardFlags & PDOMName.IS_REFERENCE) == PDOMName.IS_REFERENCE) {
138
return CPPVariableReadWriteFlags.getReadWriteFlags(name);