Example #1
0
  public void atVariable(Variable v) throws CompileError {
    Declarator d = v.getDeclarator();
    exprType = d.getType();
    arrayDim = d.getArrayDim();
    className = d.getClassName();
    int var = getLocalVar(d);

    if (arrayDim > 0) bytecode.addAload(var);
    else
      switch (exprType) {
        case CLASS:
          bytecode.addAload(var);
          break;
        case LONG:
          bytecode.addLload(var);
          break;
        case FLOAT:
          bytecode.addFload(var);
          break;
        case DOUBLE:
          bytecode.addDload(var);
          break;
        default: // BOOLEAN, BYTE, CHAR, SHORT, INT
          bytecode.addIload(var);
          break;
      }
  }
Example #2
0
  /* op is either =, %=, &=, *=, /=, +=, -=, ^=, |=, <<=, >>=, or >>>=.
   *
   * expr and var can be null.
   */
  private void atVariableAssign(
      Expr expr, int op, Variable var, Declarator d, ASTree right, boolean doDup)
      throws CompileError {
    int varType = d.getType();
    int varArray = d.getArrayDim();
    String varClass = d.getClassName();
    int varNo = getLocalVar(d);

    if (op != '=') atVariable(var);

    // expr is null if the caller is atDeclarator().
    if (expr == null && right instanceof ArrayInit)
      atArrayVariableAssign((ArrayInit) right, varType, varArray, varClass);
    else atAssignCore(expr, op, right, varType, varArray, varClass);

    if (doDup)
      if (is2word(varType, varArray)) bytecode.addOpcode(DUP2);
      else bytecode.addOpcode(DUP);

    if (varArray > 0) bytecode.addAstore(varNo);
    else if (varType == DOUBLE) bytecode.addDstore(varNo);
    else if (varType == FLOAT) bytecode.addFstore(varNo);
    else if (varType == LONG) bytecode.addLstore(varNo);
    else if (isRefType(varType)) bytecode.addAstore(varNo);
    else bytecode.addIstore(varNo);

    exprType = varType;
    arrayDim = varArray;
    className = varClass;
  }
 public void setPackage(String s) {
   s = parser.pack_replace(s);
   type_spec.setPackage(s);
   for (Enumeration e = declarators.v.elements(); e.hasMoreElements(); ) {
     Declarator d = (Declarator) e.nextElement();
     d.setPackage(s);
   }
 }
Example #4
0
  protected int getLocalVar(Declarator d) {
    int v = d.getLocalVar();
    if (v < 0) {
      v = getMaxLocals(); // delayed variable allocation.
      d.setLocalVar(v);
      incMaxLocals(1);
    }

    return v;
  }
Example #5
0
  public void member_print(PrintWriter ps, String prefix) {
    /* only print members that are not interfaces */

    if (type_spec.typeSpec() instanceof ConstrTypeSpec
            && !(((ConstrTypeSpec) type_spec.typeSpec()).c_type_spec.declaration()
                instanceof Interface)
        || type_spec.typeSpec() instanceof SequenceType
        || type_spec.typeSpec() instanceof ArrayTypeSpec) {
      type_spec.print(ps);
    }

    if (type_spec.typeSpec() instanceof StringType)
      ps.print(prefix + type_spec.toString() + " " + declarator.toString() + " = \"\";");
    else ps.print(prefix + type_spec.toString() + " " + declarator.toString() + ";");
  }
Example #6
0
  @Override
  public void getCode(CodeGenerator codeGenerator) {
    IType type = getVariableType().getType();

    CompilationUnit compilationUnit = codeGenerator.getCompilationUnit();
    compilationUnit.importType(type);

    String typeName = type.getJavaName();
    String declaration = typeName + " " + declarator.getName();

    if (type.isPrimary()) {
      String throwableName = compilationUnit.createUniqueName();
      codeGenerator.append("catch(java.lang.Throwable " + throwableName + ") {");
      codeGenerator.breakLine().incrementIndent().indent();

      codeGenerator.append(declaration + " = new " + typeName + "(" + throwableName + ");");
      codeGenerator.breakLine().indent();

      statement.getCode(codeGenerator);

      codeGenerator.decrementIndent().indent().append('}').breakLine();
    } else {
      codeGenerator.append("catch(" + declaration + ")").breakLine().indent();
      statement.getCode(codeGenerator);
    }
  }
Example #7
0
  @Override
  public boolean resolveTypes(CompilationUnit compilationUnit, IType declaringType) {
    if (!super.resolveTypes(compilationUnit, declaringType)) return false;

    declarator.resolveTypes(compilationUnit, declaringType);
    statement.resolveTypes(compilationUnit, declaringType);
    return true;
  }
Example #8
0
  @Override
  public boolean checkSemantics(
      CompilationUnit compilationUnit,
      IType declaringType,
      IMethod declaringMethod,
      IVariable leftHandValue,
      IVariableType context) {
    if (!super.checkSemantics(compilationUnit, declaringType, declaringMethod, null, null))
      return false;

    declaringMethod.openLocalScope();

    if (declarator != null)
      declarator.checkSemantics(compilationUnit, declaringType, declaringMethod, null, null);

    IType exceptionType = Primary.resolveType(compilationUnit, Primary.Exception);

    if (exceptionType != null) {
      IVariableType variableType = declarator.getVariableType();

      ITypeCast typeCast = variableType.getCastTo(exceptionType);

      if (typeCast == null || typeCast.getOperator() != null)
        setError(
            declarator.getPosition(),
            "Type mismatch: cannot convert from "
                + variableType.getSignature()
                + " to "
                + exceptionType.getUserName());
    }

    if (statement != null)
      statement.checkSemantics(compilationUnit, declaringType, declaringMethod, null, null);

    declaringMethod.closeLocalScope();

    return true;
  }
  public void open() {
    try {
      connection = createConnection();
      channel = connection.createChannel();
      if (prefetchCount > 0) {
        logger.info("setting basic.qos / prefetch count to " + prefetchCount + " for " + queueName);
        channel.basicQos(prefetchCount);
      }
      // run any declaration prior to queue consumption
      declarator.execute(channel);

      consumer = new QueueingConsumer(channel);
      consumerTag = channel.basicConsume(queueName, isAutoAcking(), consumer);
    } catch (Exception e) {
      reset();
      logger.error("could not open listener on queue " + queueName);
      reporter.reportError(e);
    }
  }
Example #10
0
  public void atDeclarator(Declarator d) throws CompileError {
    d.setLocalVar(getMaxLocals());
    d.setClassName(resolveClassName(d.getClassName()));

    int size;
    if (is2word(d.getType(), d.getArrayDim())) size = 2;
    else size = 1;

    incMaxLocals(size);

    /*  NOTE: Array initializers has not been supported.
     */
    ASTree init = d.getInitializer();
    if (init != null) {
      doTypeCheck(init);
      atVariableAssign(null, '=', null, d, init, false);
    }
  }
  public void visitDeclarator(Declarator declarator) {
    visitModifiableElement(declarator);

    visit(declarator.getDefaultValue());
  }
Example #12
0
 public CtClass lookupClass(Declarator decl) throws CompileError {
   return lookupClass(decl.getType(), decl.getArrayDim(), decl.getClassName());
 }
Example #13
0
 public CtClass lookupClassByName(ASTList name) throws CompileError {
   return lookupClass(Declarator.astToClassName(name, '.'), false);
 }
Example #14
0
  /** Parsing members means creating new members for definitions with more than one declarator. */
  public void parse() {
    boolean clone_and_parse = true;

    if (extendVector == null) throw new RuntimeException("Compiler Error: extendVector not set!");

    if (type_spec.typeSpec() instanceof ScopedName) {
      token = type_spec.typeSpec().get_token();
      type_spec = ((ScopedName) type_spec.typeSpec()).resolvedTypeSpec();
      clone_and_parse = false;
      if (type_spec instanceof ConstrTypeSpec) {
        if (((ConstrTypeSpec) type_spec.typeSpec()).c_type_spec instanceof StructType) {
          //	System.out.println("Struct " + containing_struct.typeName() + " contains struct " +
          // ((ConstrTypeSpec)type_spec.typeSpec()).typeName());
          if (((ConstrTypeSpec) type_spec.typeSpec())
              .c_type_spec
              .typeName()
              .equals(containing_struct.typeName())) {
            parser.fatal_error(
                "Illegal recursion in struct (use sequence<"
                    + containing_struct.typeName()
                    + "> instead)",
                token);
          }
        }
      }

    } else if (type_spec.typeSpec() instanceof SequenceType) {
      TypeSpec ts = ((SequenceType) type_spec.typeSpec()).elementTypeSpec().typeSpec();
      SequenceType seqTs = (SequenceType) type_spec.typeSpec();
      while (ts instanceof SequenceType) {
        seqTs = (SequenceType) ts;
        ts = ((SequenceType) ts.typeSpec()).elementTypeSpec().typeSpec();
      }

      //           if( ts.typeName().equals( containing_struct.typeName()) ||
      if (ScopedName.isRecursionScope(ts.typeName())) {
        seqTs.setRecursive();
      }
    } else if (type_spec instanceof ConstrTypeSpec) {
      type_spec.parse();
    }

    for (Enumeration e = declarators.v.elements(); e.hasMoreElements(); ) {
      Declarator d = (Declarator) e.nextElement();

      // we don't parse the declarator itself
      // as that would result in its name getting defined
      // we define the declarator's name as a type name indirectly
      // through the cloned type specs.

      Member m = new Member(new_num());
      m.declarator = d;

      TypeSpec ts = type_spec.typeSpec();

      /* create a separate type spec copy for every declarator
         if the type spec is a new type definition, i.e. a
         struct, enum, union, sequence or the declarator is
         an array declarator
      */

      //			if( clone_and_parse && !(ts instanceof BaseType) )
      if (clone_and_parse || d.d instanceof ArrayDeclarator) {
        /* arrays need special treatment */

        if (d.d instanceof ArrayDeclarator) {
          ts = new ArrayTypeSpec(new_num(), ts, (ArrayDeclarator) d.d, pack_name);
          ts.parse();
        } else if (!(ts instanceof BaseType)) {
          ts = (TypeSpec) ts.clone();
          if (!(ts instanceof ConstrTypeSpec)) ts.set_name(d.name());

          /* important: only parse type specs once (we do it for the last
          declarator only) */
          if (!e.hasMoreElements()) ts.parse();
        }
      }

      //            else
      if (!(d.d instanceof ArrayDeclarator)) {
        try {
          NameTable.define(containing_struct + "." + d.name(), "declarator");
        } catch (NameAlreadyDefined nad) {
          parser.error("Declarator " + d.name() + " already defined in scope.", token);
        }
      }

      /* if the type spec is a scoped name, it is already parsed and
       * the type name is defined
       */
      //			if( clone_and_parse )
      //				ts.parse();

      m.type_spec = ts;
      m.pack_name = this.pack_name;
      m.name = this.name;
      extendVector.addElement(m);
    }
    declarators = null;
  }
Example #15
0
 public boolean catchesAll() {
   return declarator.getVariableType().getType().isPrimary();
 }
Example #16
0
  private void atPlusPlus(int token, ASTree oprand, Expr expr, boolean doDup) throws CompileError {
    boolean isPost = oprand == null; // ++i or i++?
    if (isPost) oprand = expr.oprand2();

    if (oprand instanceof Variable) {
      Declarator d = ((Variable) oprand).getDeclarator();
      int t = exprType = d.getType();
      arrayDim = d.getArrayDim();
      int var = getLocalVar(d);
      if (arrayDim > 0) badType(expr);

      if (t == DOUBLE) {
        bytecode.addDload(var);
        if (doDup && isPost) bytecode.addOpcode(DUP2);

        bytecode.addDconst(1.0);
        bytecode.addOpcode(token == PLUSPLUS ? DADD : DSUB);
        if (doDup && !isPost) bytecode.addOpcode(DUP2);

        bytecode.addDstore(var);
      } else if (t == LONG) {
        bytecode.addLload(var);
        if (doDup && isPost) bytecode.addOpcode(DUP2);

        bytecode.addLconst((long) 1);
        bytecode.addOpcode(token == PLUSPLUS ? LADD : LSUB);
        if (doDup && !isPost) bytecode.addOpcode(DUP2);

        bytecode.addLstore(var);
      } else if (t == FLOAT) {
        bytecode.addFload(var);
        if (doDup && isPost) bytecode.addOpcode(DUP);

        bytecode.addFconst(1.0f);
        bytecode.addOpcode(token == PLUSPLUS ? FADD : FSUB);
        if (doDup && !isPost) bytecode.addOpcode(DUP);

        bytecode.addFstore(var);
      } else if (t == BYTE || t == CHAR || t == SHORT || t == INT) {
        if (doDup && isPost) bytecode.addIload(var);

        int delta = token == PLUSPLUS ? 1 : -1;
        if (var > 0xff) {
          bytecode.addOpcode(WIDE);
          bytecode.addOpcode(IINC);
          bytecode.addIndex(var);
          bytecode.addIndex(delta);
        } else {
          bytecode.addOpcode(IINC);
          bytecode.add(var);
          bytecode.add(delta);
        }

        if (doDup && !isPost) bytecode.addIload(var);
      } else badType(expr);
    } else {
      if (oprand instanceof Expr) {
        Expr e = (Expr) oprand;
        if (e.getOperator() == ARRAY) {
          atArrayPlusPlus(token, isPost, e, doDup);
          return;
        }
      }

      atFieldPlusPlus(token, isPost, oprand, expr, doDup);
    }
  }
Example #17
0
 @Override
 public IVariableType getVariableType() {
   return declarator.getVariableType();
 }