You can use `getTypeInst` combined with `getImpl` to get the type's AST 
including pragmas on its fields, that's how `hasCustomPragma` does it (but as a 
macro it can't be used from inside a macro AFAIK), see `customPragmaNode` in 
<https://github.com/nim-lang/Nim/blob/version-2-0/lib/core/macros.nim#L1571>

Example:
    
    
    import std/macros
    
    template opt1() {.pragma.}
    template opt2() {.pragma.}
    
    type
      A = object
        foo {.opt1, opt2.}, bar: string
        baz: int
    
    var a = A(foo: "foo", bar: "bar", baz: 1)
    
    macro parse(a: typed): untyped =
      let
        typ = a.getTypeInst
        typImpl = typ.getImpl
      for idef in typImpl[2][2]:
        for field in idef[0..^3]:
          if field.kind == nnkPragmaExpr:
            for pragma in field[1]:
              if pragma.eqident"opt1":
                echo field.treeRepr
    
    parse a
    
    
    Run

Note that in case the type is a generic or variant or some other non-trivial 
object, you might need to tweak the code, either to find the type symbol before 
using `getImpl`, or to get the complete list of fields, since the typed AST can 
vary in those cases.

Reply via email to