| /******************************************************************************* |
| * Copyright (c) 2009, 2011 Nokia 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: |
| * Nokia - Initial API and implementation |
| * Broadcom - added hashCode() and equals() implementations |
| *******************************************************************************/ |
| package org.eclipse.cdt.debug.edc.internal.symbols; |
| |
| import org.eclipse.cdt.debug.edc.symbols.ILocationProvider; |
| import org.eclipse.cdt.debug.edc.symbols.IScope; |
| import org.eclipse.cdt.debug.edc.symbols.IType; |
| import org.eclipse.cdt.debug.edc.symbols.IVariable; |
| import org.eclipse.core.runtime.IPath; |
| |
| |
| public class Variable implements IVariable { |
| |
| protected String name; |
| protected IScope scope; |
| protected IType type; |
| protected ILocationProvider locationProvider; |
| protected long startScope; |
| protected boolean isDeclared; |
| protected IPath definingFile; |
| |
| |
| public Variable(String name, IScope scope, IType type, ILocationProvider locationProvider, boolean isDeclared, IPath definingFile) { |
| this.name = name; |
| this.scope = scope; |
| this.type = type; |
| this.locationProvider = locationProvider; |
| this.isDeclared = isDeclared; |
| this.definingFile = definingFile; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.cdt.debug.edc.internal.symbols.IVariable#dispose() |
| */ |
| public void dispose() { |
| type = null; |
| locationProvider = null; |
| scope = null; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.cdt.debug.edc.internal.symbols.IVariable#getName() |
| */ |
| public String getName() { |
| return name; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.cdt.debug.edc.internal.symbols.IVariable#getScope() |
| */ |
| public IScope getScope() { |
| return scope; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.cdt.debug.edc.internal.symbols.IVariable#getType() |
| */ |
| public IType getType() { |
| if (type instanceof IForwardTypeReference) |
| type = ((IForwardTypeReference) type).getReferencedType(); |
| |
| return type; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.cdt.debug.edc.internal.symbols.IVariable#getLocationProvider() |
| */ |
| public ILocationProvider getLocationProvider() { |
| return locationProvider; |
| } |
| |
| public void setStartScope(long start) { |
| this.startScope = start; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.cdt.debug.edc.internal.symbols.IVariable#getStartScope() |
| */ |
| public long getStartScope() { |
| return startScope; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.cdt.debug.edc.internal.symbols.IVariable#isDeclared() |
| */ |
| public boolean isDeclared() { |
| return isDeclared; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.cdt.debug.edc.symbols.IVariable#getDefiningFile() |
| */ |
| public IPath getDefiningFile() { |
| return definingFile; |
| } |
| |
| @Override |
| public String toString() { |
| StringBuilder builder = new StringBuilder(); |
| builder.append("Var ["); //$NON-NLS-1$ |
| if (name != null) { |
| builder.append(name); |
| } |
| if (scope != null) { |
| builder.append(", "); //$NON-NLS-1$ |
| builder.append("scope="); //$NON-NLS-1$ |
| String sname = scope.getName(); |
| if (sname.length() == 0) { |
| for (IScope parent = scope.getParent() |
| ; parent != null |
| ; parent = parent.getParent()) |
| if ((sname = parent.getName()).length() != 0) |
| break; |
| if (sname.length() != 0) { |
| sname += "()"; //$NON-NLS-1$ |
| if (scope instanceof LexicalBlockScope) |
| sname += "{.lexBlk.}"; //$NON-NLS-1$ |
| } else |
| sname = "{." + scope.getClass().getSimpleName() + ".}"; |
| } |
| builder.append(sname); |
| } |
| builder.append("]"); //$NON-NLS-1$ |
| return builder.toString(); |
| } |
| |
| @Override |
| public int hashCode() { |
| final int prime = 31; |
| int result = 1; |
| result = prime * result + ((definingFile == null) ? 0 : definingFile.hashCode()); |
| result = prime * result + (isDeclared ? 1231 : 1237); |
| result = prime * result + ((name == null) ? 0 : name.hashCode()); |
| result = prime * result + ((scope == null) ? 0 : scope.hashCode()); |
| result = prime * result + (int) (startScope ^ (startScope >>> 32)); |
| result = prime * result + ((type == null) ? 0 : type.hashCode()); |
| return result; |
| } |
| |
| @Override |
| public boolean equals(Object obj) { |
| if (this == obj) |
| return true; |
| if (obj == null) |
| return false; |
| if (getClass() != obj.getClass()) |
| return false; |
| Variable other = (Variable) obj; |
| if (definingFile == null) { |
| if (other.definingFile != null) |
| return false; |
| } else if (!definingFile.equals(other.definingFile)) |
| return false; |
| if (isDeclared != other.isDeclared) |
| return false; |
| if (name == null) { |
| if (other.name != null) |
| return false; |
| } else if (!name.equals(other.name)) |
| return false; |
| if (scope == null) { |
| if (other.scope != null) |
| return false; |
| } else if (!scope.equals(other.scope)) |
| return false; |
| if (startScope != other.startScope) |
| return false; |
| if (type == null) { |
| if (other.type != null) |
| return false; |
| } else if (!type.equals(other.type)) |
| return false; |
| return true; |
| } |
| } |