chapel-lang / chapel

a Productive Parallel Programming Language
https://chapel-lang.org
Other
1.8k stars 422 forks source link

Add library routine for getting a class's dynamic type as a string #16916

Open bradcray opened 3 years ago

bradcray commented 3 years ago

Within the code base, we have the ability to get the dynamic type of a class as a string, and this can be nice from a debugging / I/O perspective, however, it is not currently a user-facing feature. Doing so seems like a nice utility, where the hard questions are:

For example, a very trivial implementation might look like:

proc dynamicTypeAsString(expr) {
    return __primitive("class name by id",
                       __primitive("getcid", expr.borrow())
                      ):string;
}

and a more complex version might look like:

proc dynamicTypeAsString(expr, printClassMgmt = true) {
  var ret: string;

  if (isClass(expr)) {
    if (printClassMgmt) {
      if (isOwnedClass(expr)) {
        ret = "owned ";
      } else if (isBorrowedClass(expr)) {
        ret = "borrowed ";
      } else if (isSharedClass(expr)) {
        ret = "shared ";
      } else if (isUnmanagedClass(expr)) {
        ret = "unmanaged ";
      } else {
        halt("Unanticipated class management type in dynamicTypeAsString()");
      }
    }
    ret += __primitive("class name by id",
                       __primitive("getcid", expr.borrow())
                      ):string;
    return ret;
  } else {
    compilerError("dynamicTypeAsString() isn't currently implemented for non-classes");
  }
}

(where ultimately we might extend this to support non-class types as well? E.g., by printing out the static type for simple value types, information about arrays for arrays?)

12mohaned commented 3 years ago

I believe Type module is a good fit compared to to other modules for this problem, if it is going to support in the future other types like arrays, tuples,etc.. I believe it should have a dedicated module for it, or they could be added in a related way to each other.

For example, we can bundle all functions together.

proc getDynamicClass(expr) { return __primitive("class name by id", __primitive("getcid", expr.borrow()) ):string; }

proc getDynamicArray(expr) { }

bradcray commented 3 years ago

Note that where my OP above has sketched out what this capability might look like for classes, @12mohaned has developed a branch that does similarly for arrays in #17591.

bradcray commented 3 weeks ago

I'm finding that running the workaround in the compiler today, we're getting a warning about use of c_string, which is deprecated, which I think suggests that the primitive itself is using c_string rather than c_ptr(c_char)). I'm wondering whether this would be a simple fix in Dyno.