MCPcopy
hub / github.com/google/mangle / Decls

Method Decls

packages/packages.go:81–135  ·  view source on GitHub ↗

Decls returns Decls of the package with rewritten identifiers.

()

Source from the content-addressed store, hash-verified

79
80// Decls returns Decls of the package with rewritten identifiers.
81func (p *Package) Decls() ([]ast.Decl, error) {
82 usedPackages, definedIdentifier, err := p.declarationMappings()
83 if err != nil {
84 return nil, err
85 }
86
87 decls := []ast.Decl{}
88 for _, u := range p.units {
89 for _, decl := range u.Decls {
90 // Skip Package and Import Decls.
91 if decl.DeclaredAtom.Predicate == symbols.Package {
92 continue
93 }
94 if decl.DeclaredAtom.Predicate == symbols.Use {
95 continue
96 }
97
98 if p.Name != "" {
99 decl.DeclaredAtom.Predicate.Symbol = fmt.Sprintf("%s.%s", p.Name, decl.DeclaredAtom.Predicate.Symbol)
100 }
101 for i, bd := range decl.Bounds {
102 for j, b := range bd.Bounds {
103 if err := symbols.WellformedBound(b); err == nil { // if no error
104 continue
105 }
106
107 if c, ok := b.(ast.Constant); ok {
108 if c.Type != ast.StringType {
109 return nil, fmt.Errorf("cannot handle bound %v part of %v for decl %v", c, bd, decl.DeclaredAtom.Predicate.Symbol)
110 }
111 sv, err := c.StringValue()
112 if err != nil {
113 return nil, err
114 }
115 if _, ok := definedIdentifier[ast.PredicateSym{Symbol: sv, Arity: 1}]; ok {
116 if p.Name != "" {
117 decl.Bounds[i].Bounds[j] = ast.String(fmt.Sprintf("%s.%s", p.Name, sv))
118 }
119 continue
120 }
121 u := strings.LastIndex(sv, ".")
122 if u == -1 {
123 continue
124 }
125 if !usedPackages.Contains(sv[:u]) {
126 return nil, fmt.Errorf("in package %q, 'Use' declaration for %v not found", p.Name, sv)
127 }
128 }
129 }
130 }
131 decls = append(decls, decl)
132 }
133 }
134 return decls, nil
135}
136
137func (p *Package) updatedAtom(a ast.Atom, definedIdentifier map[ast.PredicateSym]bool, usedPackages stringset.Set) (ast.Atom, error) {
138 if _, ok := definedIdentifier[a.Predicate]; ok {

Callers 3

TestDeclsFunction · 0.80
TestDeclsErrorsFunction · 0.80
AnalyzeAndCheckBoundsFunction · 0.80

Calls 5

declarationMappingsMethod · 0.95
WellformedBoundFunction · 0.92
StringFunction · 0.92
StringValueMethod · 0.80
ContainsMethod · 0.65

Tested by 2

TestDeclsFunction · 0.64
TestDeclsErrorsFunction · 0.64