compiler/sem/semtypinst

  Source   Edit

This module does the instantiation of generic types.

Types

LayeredIdTable {.acyclic.} = ref object
  topLayer*: TIdTable
  nextLayer*: LayeredIdTable
  Source   Edit
TReplTypeVars = object
  c*: PContext
  typeMap*: LayeredIdTable
  symMap*: TIdTable
  info*: TLineInfo
  skipTypedesc*: bool
  isReturnType*: bool
  owner*: PSym
  recursionLimit: int
  Source   Edit

Procs

func addInheritedFields(check: var IntSet; obj: PType): int {....raises: [],
    tags: [].}
Counts the number of fields in obj (and its base types) and returns it. In addition, adds the IDs of all field names in obj and its base types to check.   Source   Edit
proc checkConstructedType(conf: ConfigRef; info: TLineInfo; typ: PType) {.
    ...raises: [Exception, ERecoverableError], tags: [RootEffect].}
  Source   Edit
proc eraseVoidParams(t: PType) {....raises: [], tags: [].}
  Source   Edit
proc generateTypeInstance(p: PContext; pt: TIdTable; info: TLineInfo; t: PType): PType {.
    ...raises: [Exception], tags: [RootEffect].}
Produces the instantiated type for the generic type t, using the bindings provided by pt. All type variables used by t must have concrete types bound to them -- both meta types and missing bindings are disallowed and will result in an instantiation failure.   Source   Edit
proc initLayeredTypeMap(pt: TIdTable): LayeredIdTable {....raises: [], tags: [].}
  Source   Edit
proc initTypeVars(p: PContext; typeMap: LayeredIdTable; info: TLineInfo;
                  owner: PSym): TReplTypeVars {....raises: [], tags: [].}
  Source   Edit
proc instantiateTypesInBody(p: PContext; pt: TIdTable; n: PNode; owner: PSym): PNode {.
    ...raises: [KeyError, Exception], tags: [RootEffect].}
Instantiates all types referenced in the generic AST n. pt provides the bindings for all used type variables (i.e., generic parameters and type-classes) -- missing bindings result in an error.   Source   Edit
proc instCopyType(cl: var TReplTypeVars; t: PType): PType {.
    ...raises: [KeyError, Exception], tags: [ReadDirEffect, RootEffect].}
  Source   Edit
proc newTypeMapLayer(cl: var TReplTypeVars): LayeredIdTable {....raises: [],
    tags: [].}
  Source   Edit
proc prepareNode(cl: var TReplTypeVars; n: PNode): PNode {.
    ...raises: [Exception, KeyError], tags: [RootEffect].}

Replaces unresolved types referenced by the nodes of expression/statement n with the corresponding bound ones.

Because of how late-bound static parameters are currently implemented, prepareNode is also responsible for replacing sub-expression of type tyStatic with the computed value.

  Source   Edit
proc replaceTypeParamsInType(c: PContext; pt: TIdTable; t: PType): PType {.
    ...raises: [Exception], tags: [RootEffect].}

Replaces with their bound type, if a binding exists, type parameters in the type t. Only type parameters inside the types and attached AST of tyRanges and tyTypeFrom are replaced -- the AST of, for example, named tuple types or object types is not adjusted.

t is not modified directly. Instead, an exact replica (which includes the type ID) is first created prior to something being replaced. If nothing is replaced, the returned type is exactly t.

If something was replaced, the returned type is not a proper instantiated one and thus must not be used in contexts where a proper type is expected.

  Source   Edit
proc replaceTypeVarsInBody(c: PContext; pt: TIdTable; n: PNode): PNode {.
    ...raises: [KeyError], tags: [].}

Replaces with their bound types (provided by pt) all type variables in the generic AST n, with unbound type variables being ignored. Generic types are not instantiated and static expression not evaluated.

Use this procedure instead of instantiateTypesInBody when it's not guaranteed whether all type variables have been resolved.

  Source   Edit
proc replaceTypeVarsT(cl: var TReplTypeVars; t: PType): PType {.
    ...raises: [Exception], tags: [RootEffect].}
  Source   Edit
proc skipIntLiteralParams(t: PType; idgen: IdGenerator) {....raises: [], tags: [].}
  Source   Edit
proc tryGenerateInstance(c: PContext; pt: TIdTable; info: TLineInfo; t: PType): PType {.
    ...raises: [Exception], tags: [RootEffect].}
Tries to resolve the generic type t to a non-generic one, using the type bindings provided by pt. Returns nil on failure, and the resolved type on success. Do note that the returned type can still be a meta type.
XXX: this procedure is only a workaround. replaceTypeVarsT should
properly support the case where it's not certain whether all referenced type parameters are resolved already
  Source   Edit

Templates

template generateTypeInstance(p: PContext; pt: TIdTable; arg: PNode; t: PType): untyped
  Source   Edit