compiler/ast/checked_ast

  Source   Edit

The CheckedAst type is implemented here, along with its accessor routines and routines for creating and manipulating CheckedAst instances.

CheckedAst is a wrapper around PNode, and is intended to make propagation less manual, by keeping track of whether there is an error somewhere in the managed AST.

The idea is to use CheckedAst as the return type of all semantic analysis procedures, eventually removing the need for wrapper errors (adWrappedError).

A second experiment, ElaborateAst, is also implemented here. Rather than being a wrapper around PNode, it acts as sort of "builder" during piece-by-piece production of an AST node.

Types

CheckedAst = object
  n: PNode                   ## stores the AST. Never nil nor an error
  hasError: bool             ## tracks whether `n` contains an ``nkError`` node
                             ## somewhere
  
Represents either in-progress or fully semanticized AST. It is meant as a wrapper for semantically checked AST, hence the name.   Source   Edit
ElaborateAst = object
  orig: PNode                ## the source node
  diag*: PAstDiag            ## the error diagnostic, or nil. If the production
                             ## finishes and the diagnostic is non-nil, an error
                             ## is produced instead
  hasError: bool             ## whether one of the produced children contains an
                             ## error somewhere
  children: seq[PNode]       ## the produced child nodes; has the number of
                             ## elements as `n` has children
  typ*: PType                ## the type of the produced expression
  
Note: the name is not final and going to change. Represents an in-progress or finished production of either an AST of the same kind and number of children as the source, or an error.   Source   Edit

Procs

func `[]=`(x: var CheckedAst; i: int | BackwardsIndex; n: sink CheckedAst)
  Source   Edit
func `[]=`(x: var CheckedAst; i: int | BackwardsIndex; n: sink PNode)
  Source   Edit
func `[]=`(x: var ElaborateAst; i: int | BackwardsIndex; n: PNode) {.inline.}
Sets the child node at i   Source   Edit
func `[]=`(x: var ElaborateAst; i: int | BackwardsIndex; n: sink CheckedAst) {.
    inline.}
Sets the child node at i   Source   Edit
func `[]`(x: CheckedAst; i: int | BackwardsIndex): PNode
  Source   Edit
proc add(x: var CheckedAst; n: sink CheckedAst) {.inline, ...raises: [], tags: [].}
  Source   Edit
proc add(x: var CheckedAst; n: sink PNode) {.inline, ...raises: [], tags: [].}
  Source   Edit
func assign(x: var CheckedAst; n: sink PNode) {.inline, ...raises: [], tags: [].}
Assigns the tree n to x. The behaviour is undefined if n is either an error error or contains one.   Source   Edit
proc extract(c: ConfigRef; n: sink CheckedAst): PNode {....raises: [], tags: [].}

Converts n to a PNode. If the AST contains an error somewhere, the resulting node is always an nkError node.

This procedure is only meant to be used at the edge to CheckedAst- unaware code.

  Source   Edit
proc extract(c: ConfigRef; n: sink ElaborateAst): PNode {....raises: [], tags: [].}
Finishes the production and returns the result as a PNode.   Source   Edit
func flattenIf(n: var CheckedAst; kind: TNodeKind) {....raises: [], tags: [].}
Replace n with its first child node, but only if n has a single child node and is of kind kind.   Source   Edit
func info(x: CheckedAst): TLineInfo {.inline, ...raises: [], tags: [].}
  Source   Edit
proc initForMappingChildren(x: var CheckedAst; n: PNode) {....raises: [KeyError],
    tags: [].}
Initialize x as a node of kind n.kind. Space for all children of n is allocated, but the child nodes are not copied. For the flags, only the persistent ones are copied.   Source   Edit
func initWith(x: var CheckedAst; n: sink PNode) {....raises: [], tags: [].}
Initializes x, using n as the tree. This is only meant to be used with the direct result of a newNodeX or newTreeX call.   Source   Edit
proc initWith(x: var ElaborateAst; n: PNode) {....raises: [], tags: [].}
Setup the production of a new AST of the kind and with the number of children that n has.   Source   Edit
func kind(x: CheckedAst): TNodeKind {.inline, ...raises: [], tags: [].}
  Source   Edit
func len(x: CheckedAst): int {.inline, ...raises: [], tags: [].}
  Source   Edit
func typ(x: CheckedAst): PType {.inline, ...raises: [], tags: [].}
  Source   Edit
func typ=(x: var CheckedAst; typ: PType) {.inline, ...raises: [], tags: [].}
  Source   Edit

Templates

template `[]`(x: var ElaborateAst; i: int | BackwardsIndex): PNode
  Source   Edit
template get(n: CheckedAst): PNode
Direct access to the internal node. Should only be used by code meant for debugging.   Source   Edit