Beispiel #1
0
 public void setAttr(State s, @NotNull Type v) {
   Type targetType = transformExpr(target, s);
   if (targetType.isUnionType()) {
     Set<Type> types = targetType.asUnionType().types;
     for (Type tp : types) {
       setAttrType(tp, v);
     }
   } else {
     setAttrType(targetType, v);
   }
 }
Beispiel #2
0
 private void setAttrType(@NotNull Type targetType, @NotNull Type v) {
   if (targetType.isUnknownType()) {
     Analyzer.self.putProblem(this, "Can't set attribute for UnknownType");
     return;
   }
   // new attr, mark the type as "mutated"
   if (targetType.table.lookupAttr(attr.id) == null
       || !targetType.table.lookupAttrType(attr.id).equals(v)) {
     targetType.setMutated(true);
   }
   targetType.table.insert(attr.id, attr, v, ATTRIBUTE);
 }
Beispiel #3
0
  private void importStar(@NotNull Scope s, @Nullable ModuleType mt) {
    if (mt == null || mt.getFile() == null) {
      return;
    }

    Module mod = Indexer.idx.getAstForFile(mt.getFile());
    if (mod == null) {
      return;
    }

    List<String> names = new ArrayList<>();
    Type allType = mt.getTable().lookupType("__all__");

    if (allType != null && allType.isListType()) {
      ListType lt = allType.asListType();

      for (Object o : lt.values) {
        if (o instanceof String) {
          names.add((String) o);
        }
      }
    }

    if (!names.isEmpty()) {
      for (String name : names) {
        List<Binding> b = mt.getTable().lookupLocal(name);
        if (b != null) {
          s.update(name, b);
        } else {
          List<Name> m2 = new ArrayList<>(module);
          m2.add(new Name(name));
          ModuleType mod2 = Indexer.idx.loadModule(m2, s);
          if (mod2 != null) {
            s.insert(name, null, mod2, Binding.Kind.VARIABLE);
          }
        }
      }
    } else {
      // Fall back to importing all names not starting with "_".
      for (Entry<String, List<Binding>> e : mt.getTable().entrySet()) {
        if (!e.getKey().startsWith("_")) {
          s.update(e.getKey(), e.getValue());
        }
      }
    }
  }
Beispiel #4
0
  @NotNull
  @Override
  public Type transform(State s) {
    // the form of ::A in ruby
    if (target == null) {
      return transformExpr(attr, s);
    }

    Type targetType = transformExpr(target, s);
    if (targetType.isUnionType()) {
      Set<Type> types = targetType.asUnionType().types;
      Type retType = Type.UNKNOWN;
      for (Type tt : types) {
        retType = UnionType.union(retType, getAttrType(tt));
      }
      return retType;
    } else {
      return getAttrType(targetType);
    }
  }
Beispiel #5
0
  private Type getAttrType(@NotNull Type targetType) {
    List<Binding> bs = targetType.table.lookupAttr(attr.id);
    if (bs == null) {
      Analyzer.self.putProblem(attr, "attribute not found in type: " + targetType);
      Type t = Type.UNKNOWN;
      t.table.setPath(targetType.table.extendPath(attr.id));
      return t;
    } else {
      for (Binding b : bs) {
        Analyzer.self.putRef(attr, b);
        if (parent != null
            && parent.isCall()
            && b.type.isFuncType()
            && targetType.isInstanceType()) { // method call
          b.type.asFuncType().setSelfType(targetType);
        }
      }

      return State.makeUnion(bs);
    }
  }