Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4cc7128905596f1bb87edba86671b7d8420256d8 (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
/*******************************************************************************
 * Copyright (c) 2004, 2013 IBM Corporation 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:
 *     Andrew Niefer (IBM Corporation) - initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp;

import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
import org.eclipse.cdt.core.dom.ast.IASTPointer;
import org.eclipse.cdt.core.dom.ast.IPointerType;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPPointerToMemberType;
import org.eclipse.cdt.internal.core.dom.parser.ISerializableType;
import org.eclipse.cdt.internal.core.dom.parser.ITypeContainer;
import org.eclipse.cdt.internal.core.dom.parser.ITypeMarshalBuffer;
import org.eclipse.core.runtime.CoreException;

/**
 * Pointers in c++
 */
public class CPPPointerType implements IPointerType, ITypeContainer, ISerializableType {
	protected IType type;
	private boolean isConst;
	private boolean isVolatile;
	private boolean isRestrict;
	
	public CPPPointerType(IType type, boolean isConst, boolean isVolatile, boolean isRestrict) {
		this.isConst = isConst;
		this.isVolatile = isVolatile;
		this.isRestrict = isRestrict;
		setType(type);
	}

	public CPPPointerType(IType type, IASTPointer operator) {
		this(type, operator.isConst(), operator.isVolatile(), operator.isRestrict());
	}
	
	public CPPPointerType(IType type) {
	    this(type, false, false, false);
	}

	@Override
	public boolean isSameType(IType o) {
	    if (o == this)
            return true;
        if (o instanceof ITypedef)
            return o.isSameType(this);
        
        if (!(o instanceof IPointerType))
        	return false;
        
	    if (this instanceof ICPPPointerToMemberType != o instanceof ICPPPointerToMemberType) 
	        return false;
	    
	    if (type == null)
	        return false;
	    
	    IPointerType pt = (IPointerType) o;
	    if (isConst == pt.isConst() && isVolatile == pt.isVolatile() && isRestrict == pt.isRestrict()) {
			return type.isSameType(pt.getType());
	    }
	    return false;
	}

	@Override
	public IType getType() {
		return type;
	}
	
	@Override
	public void setType(IType t) {
		assert t != null;
	    type = t;
	}

	@Override
	public boolean isConst() {
		return isConst;
	}

	@Override
	public boolean isVolatile() {
		return isVolatile;
	}

	@Override
	public boolean isRestrict() {
		return isRestrict;
	}

    @Override
	public Object clone() {
        IType t = null;
   		try {
            t = (IType) super.clone();
        } catch (CloneNotSupportedException e) {
            //not going to happen
        }
        return t;
    }

	@Override
	public void marshal(ITypeMarshalBuffer buffer) throws CoreException {
		short firstBytes= ITypeMarshalBuffer.POINTER_TYPE;
		if (isConst()) firstBytes |= ITypeMarshalBuffer.FLAG1;
		if (isVolatile()) firstBytes |= ITypeMarshalBuffer.FLAG2;
		if (isRestrict()) firstBytes |= ITypeMarshalBuffer.FLAG3;
		buffer.putShort(firstBytes);
		final IType nestedType = getType();
		buffer.marshalType(nestedType);
	}
	
	public static IType unmarshal(short firstBytes, ITypeMarshalBuffer buffer) throws CoreException {
		IType nested= buffer.unmarshalType();
		return new CPPPointerType(nested, (firstBytes & ITypeMarshalBuffer.FLAG1) != 0,
				(firstBytes & ITypeMarshalBuffer.FLAG2) != 0,
				(firstBytes & ITypeMarshalBuffer.FLAG3) != 0);
	}

	@Override
	public String toString() {
		return ASTTypeUtil.getType(this);
	}
}

Back to the top