1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: UnaryOpExpr.java,v 1.8 2004/02/16 22:25:10 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.compiler;
  20. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  21. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  22. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  23. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodType;
  24. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  25. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  26. /**
  27. * @author Jacek Ambroziak
  28. * @author Santiago Pericas-Geertsen
  29. */
  30. final class UnaryOpExpr extends Expression {
  31. private Expression _left;
  32. public UnaryOpExpr(Expression left) {
  33. (_left = left).setParent(this);
  34. }
  35. /**
  36. * Returns true if this expressions contains a call to position(). This is
  37. * needed for context changes in node steps containing multiple predicates.
  38. */
  39. public boolean hasPositionCall() {
  40. return(_left.hasPositionCall());
  41. }
  42. /**
  43. * Returns true if this expressions contains a call to last()
  44. */
  45. public boolean hasLastCall() {
  46. return(_left.hasLastCall());
  47. }
  48. public void setParser(Parser parser) {
  49. super.setParser(parser);
  50. _left.setParser(parser);
  51. }
  52. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  53. final Type tleft = _left.typeCheck(stable);
  54. final MethodType ptype = lookupPrimop(stable, "u-",
  55. new MethodType(Type.Void,
  56. tleft));
  57. if (ptype != null) {
  58. final Type arg1 = (Type) ptype.argsType().elementAt(0);
  59. if (!arg1.identicalTo(tleft)) {
  60. _left = new CastExpr(_left, arg1);
  61. }
  62. return _type = ptype.resultType();
  63. }
  64. throw new TypeCheckError(this);
  65. }
  66. public String toString() {
  67. return "u-" + '(' + _left + ')';
  68. }
  69. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  70. InstructionList il = methodGen.getInstructionList();
  71. _left.translate(classGen, methodGen);
  72. il.append(_type.NEG());
  73. }
  74. }