/plugins/PHPParser/tags/PHPParser-1.3.1/src/net/sourceforge/phpdt/internal/compiler/ast/VariableDeclaration.java

# · Java · 282 lines · 186 code · 39 blank · 57 comment · 17 complexity · 43f48645a138ba2ce639ac0d219f4d5a MD5 · raw file

  1. /*
  2. * :tabSize=8:indentSize=8:noTabs=false:
  3. * :folding=explicit:collapseFolds=1:
  4. *
  5. * Copyright (C) 2003, 2010 Matthieu Casanova
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or any later version.
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. package net.sourceforge.phpdt.internal.compiler.ast;
  21. import net.sourceforge.phpdt.internal.compiler.ast.declarations.VariableUsage;
  22. import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
  23. import java.util.List;
  24. import gatchan.phpparser.project.itemfinder.PHPItem;
  25. import gatchan.phpparser.parser.PHPParser;
  26. import sidekick.IAsset;
  27. import javax.swing.text.Position;
  28. import javax.swing.*;
  29. import org.gjt.sp.jedit.GUIUtilities;
  30. /**
  31. * A variable declaration.
  32. *
  33. * @author Matthieu Casanova
  34. */
  35. public class VariableDeclaration extends Expression implements Outlineable, IAsset
  36. {
  37. private final AbstractVariable variable;
  38. /**
  39. * The value for variable initialization.
  40. */
  41. private Expression initialization;
  42. private final transient Outlineable parent;
  43. private boolean reference;
  44. private String operator;
  45. private transient Position start;
  46. private transient Position end;
  47. private Icon icon;
  48. private String cachedToString;
  49. private static final long serialVersionUID = 4707939646109273633L;
  50. /**
  51. * Create a variable.
  52. *
  53. * @param variable the name of the variable
  54. * @param initialization the initialization (it could be null when you have a parse error)
  55. * @param operator the assign operator
  56. * @param sourceStart the start point
  57. * @param sourceEnd the end point
  58. */
  59. public VariableDeclaration(Outlineable parent,
  60. AbstractVariable variable,
  61. Expression initialization,
  62. String operator,
  63. int sourceStart,
  64. int sourceEnd,
  65. int beginLine,
  66. int endLine,
  67. int beginColumn,
  68. int endColumn)
  69. {
  70. super(initialization == null ? Type.UNKNOWN : initialization.getType(),
  71. sourceStart,
  72. sourceEnd,
  73. beginLine,
  74. endLine,
  75. beginColumn,
  76. endColumn);
  77. this.initialization = initialization;
  78. this.variable = variable;
  79. variable.setType(type);
  80. this.operator = operator;
  81. this.parent = parent;
  82. }
  83. /**
  84. * Create a variable.
  85. *
  86. * @param parent the outlineable parent
  87. * @param variable a variable (in case of $$variablename)
  88. */
  89. public VariableDeclaration(Outlineable parent,
  90. AbstractVariable variable)
  91. {
  92. super(Type.NULL, variable);
  93. this.variable = variable;
  94. this.parent = parent;
  95. }
  96. public void setReference(boolean reference, int sourceStart, int beginLine, int beginColumn)
  97. {
  98. this.reference = reference;
  99. this.sourceStart = sourceStart;
  100. this.beginLine = beginLine;
  101. this.beginColumn = beginColumn;
  102. }
  103. /**
  104. * Return the variable into String.
  105. *
  106. * @return a String
  107. */
  108. @Override
  109. public String toStringExpression()
  110. {
  111. if (cachedToString == null)
  112. {
  113. String variableString = variable.toStringExpression();
  114. if (initialization == null)
  115. {
  116. if (reference) return '&' + variableString;
  117. else return variableString;
  118. }
  119. else
  120. {
  121. // final String operatorString = operatorToString();
  122. String initString = initialization.toStringExpression();
  123. StringBuilder buff = new StringBuilder(variableString.length() +
  124. operator.length() +
  125. initString.length() +
  126. 1);
  127. buff.append(variableString);
  128. buff.append(operator);
  129. buff.append(initString);
  130. cachedToString = buff.toString();
  131. }
  132. }
  133. return cachedToString;
  134. }
  135. public Outlineable getParent()
  136. {
  137. return parent;
  138. }
  139. public String toString()
  140. {
  141. return toStringExpression();
  142. }
  143. /**
  144. * Get the variables from outside (parameters, globals ...)
  145. */
  146. @Override
  147. public void getOutsideVariable(List<VariableUsage> list)
  148. {
  149. }
  150. /**
  151. * get the modified variables.
  152. */
  153. @Override
  154. public void getModifiedVariable(List<VariableUsage> list)
  155. {
  156. variable.getUsedVariable(list);
  157. if (initialization != null)
  158. {
  159. initialization.getModifiedVariable(list);
  160. }
  161. }
  162. /**
  163. * Get the variables used.
  164. */
  165. @Override
  166. public void getUsedVariable(List<VariableUsage> list)
  167. {
  168. if (initialization != null)
  169. {
  170. initialization.getUsedVariable(list);
  171. }
  172. }
  173. public String getName()
  174. {
  175. return variable.getName();
  176. }
  177. public Expression getInitialization()
  178. {
  179. return initialization;
  180. }
  181. public int getItemType()
  182. {
  183. return PHPItem.VARIABLE;
  184. }
  185. public Position getStart()
  186. {
  187. return start;
  188. }
  189. public void setStart(Position start)
  190. {
  191. this.start = start;
  192. }
  193. public Position getEnd()
  194. {
  195. return end;
  196. }
  197. public void setEnd(Position end)
  198. {
  199. this.end = end;
  200. }
  201. public Icon getIcon()
  202. {
  203. if (icon == null)
  204. {
  205. icon = GUIUtilities.loadIcon(ClassHeader.class.getResource("/gatchan/phpparser/icons/field.png").toString());
  206. }
  207. return icon;
  208. }
  209. public String getShortString()
  210. {
  211. return toString();
  212. }
  213. public String getLongString()
  214. {
  215. return toString();
  216. }
  217. public void setName(String name)
  218. {
  219. }
  220. @Override
  221. public Expression expressionAt(int line, int column)
  222. {
  223. if (variable.isAt(line, column)) return variable;
  224. if (initialization != null && initialization.isAt(line, column)) return initialization;
  225. return null;
  226. }
  227. @Override
  228. public void analyzeCode(PHPParser parser)
  229. {
  230. }
  231. public boolean add(Outlineable o)
  232. {
  233. return false;
  234. }
  235. public Outlineable get(int index)
  236. {
  237. return null;
  238. }
  239. public int size()
  240. {
  241. return 0;
  242. }
  243. }