MCPcopy Create free account
hub / github.com/despiteallobjections/amigo / objDecl

Method objDecl

types/decl.go:57–206  ·  view source on GitHub ↗

objDecl type-checks the declaration of obj in its respective (file) environment. For the meaning of def, see Checker.definedType, in typexpr.go.

(obj Object, def *Named)

Source from the content-addressed store, hash-verified

55// objDecl type-checks the declaration of obj in its respective (file) environment.
56// For the meaning of def, see Checker.definedType, in typexpr.go.
57func (check *Checker) objDecl(obj Object, def *Named) {
58 if check.conf.Trace && obj.Type() == nil {
59 if check.indent == 0 {
60 fmt.Println() // empty line between top-level objects for readability
61 }
62 check.trace(obj.Pos(), "-- checking %s (%s, objPath = %s)", obj, obj.color(), pathString(check.objPath))
63 check.indent++
64 defer func() {
65 check.indent--
66 check.trace(obj.Pos(), "=> %s (%s)", obj, obj.color())
67 }()
68 }
69
70 // Checking the declaration of obj means inferring its type
71 // (and possibly its value, for constants).
72 // An object's type (and thus the object) may be in one of
73 // three states which are expressed by colors:
74 //
75 // - an object whose type is not yet known is painted white (initial color)
76 // - an object whose type is in the process of being inferred is painted grey
77 // - an object whose type is fully inferred is painted black
78 //
79 // During type inference, an object's color changes from white to grey
80 // to black (pre-declared objects are painted black from the start).
81 // A black object (i.e., its type) can only depend on (refer to) other black
82 // ones. White and grey objects may depend on white and black objects.
83 // A dependency on a grey object indicates a cycle which may or may not be
84 // valid.
85 //
86 // When objects turn grey, they are pushed on the object path (a stack);
87 // they are popped again when they turn black. Thus, if a grey object (a
88 // cycle) is encountered, it is on the object path, and all the objects
89 // it depends on are the remaining objects on that path. Color encoding
90 // is such that the color value of a grey object indicates the index of
91 // that object in the object path.
92
93 // During type-checking, white objects may be assigned a type without
94 // traversing through objDecl; e.g., when initializing constants and
95 // variables. Update the colors of those objects here (rather than
96 // everywhere where we set the type) to satisfy the color invariants.
97 if obj.color() == white && obj.Type() != nil {
98 obj.setColor(black)
99 return
100 }
101
102 switch obj.color() {
103 case white:
104 assert(obj.Type() == nil)
105 // All color values other than white and black are considered grey.
106 // Because black and white are < grey, all values >= grey are grey.
107 // Use those values to encode the object's index into the object path.
108 obj.setColor(grey + color(check.push(obj)))
109 defer func() {
110 check.pop().setColor(black)
111 }()
112
113 case black:
114 assert(obj.Type() != nil)

Callers 5

selectorMethod · 0.95
missingMethodMethod · 0.95
packageObjectsMethod · 0.95
identMethod · 0.95
instantiateMethodMethod · 0.80

Calls 15

traceMethod · 0.95
pushMethod · 0.95
popMethod · 0.95
validCycleMethod · 0.95
dumpMethod · 0.95
constDeclMethod · 0.95
varDeclMethod · 0.95
typeDeclMethod · 0.95
collectMethodsMethod · 0.95
funcDeclMethod · 0.95
pathStringFunction · 0.85
colorTypeAlias · 0.85

Tested by

no test coverage detected