OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [go/] [doc/] [exports.go] - Blame information for rev 775

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2011 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
// This file implements export filtering of an AST.
6
 
7
package doc
8
 
9
import "go/ast"
10
 
11
// filterIdentList removes unexported names from list in place
12
// and returns the resulting list.
13
//
14
func filterIdentList(list []*ast.Ident) []*ast.Ident {
15
        j := 0
16
        for _, x := range list {
17
                if ast.IsExported(x.Name) {
18
                        list[j] = x
19
                        j++
20
                }
21
        }
22
        return list[0:j]
23
}
24
 
25
// filterFieldList removes unexported fields (field names) from the field list
26
// in place and returns true if fields were removed. Anonymous fields are
27
// recorded with the parent type. filterType is called with the types of
28
// all remaining fields.
29
//
30
func (r *reader) filterFieldList(parent *namedType, fields *ast.FieldList) (removedFields bool) {
31
        if fields == nil {
32
                return
33
        }
34
        list := fields.List
35
        j := 0
36
        for _, field := range list {
37
                keepField := false
38
                if n := len(field.Names); n == 0 {
39
                        // anonymous field
40
                        name := r.recordAnonymousField(parent, field.Type)
41
                        if ast.IsExported(name) {
42
                                keepField = true
43
                        }
44
                } else {
45
                        field.Names = filterIdentList(field.Names)
46
                        if len(field.Names) < n {
47
                                removedFields = true
48
                        }
49
                        if len(field.Names) > 0 {
50
                                keepField = true
51
                        }
52
                }
53
                if keepField {
54
                        r.filterType(nil, field.Type)
55
                        list[j] = field
56
                        j++
57
                }
58
        }
59
        if j < len(list) {
60
                removedFields = true
61
        }
62
        fields.List = list[0:j]
63
        return
64
}
65
 
66
// filterParamList applies filterType to each parameter type in fields.
67
//
68
func (r *reader) filterParamList(fields *ast.FieldList) {
69
        if fields != nil {
70
                for _, f := range fields.List {
71
                        r.filterType(nil, f.Type)
72
                }
73
        }
74
}
75
 
76
// filterType strips any unexported struct fields or method types from typ
77
// in place. If fields (or methods) have been removed, the corresponding
78
// struct or interface type has the Incomplete field set to true.
79
//
80
func (r *reader) filterType(parent *namedType, typ ast.Expr) {
81
        switch t := typ.(type) {
82
        case *ast.Ident:
83
                // nothing to do
84
        case *ast.ParenExpr:
85
                r.filterType(nil, t.X)
86
        case *ast.ArrayType:
87
                r.filterType(nil, t.Elt)
88
        case *ast.StructType:
89
                if r.filterFieldList(parent, t.Fields) {
90
                        t.Incomplete = true
91
                }
92
        case *ast.FuncType:
93
                r.filterParamList(t.Params)
94
                r.filterParamList(t.Results)
95
        case *ast.InterfaceType:
96
                if r.filterFieldList(parent, t.Methods) {
97
                        t.Incomplete = true
98
                }
99
        case *ast.MapType:
100
                r.filterType(nil, t.Key)
101
                r.filterType(nil, t.Value)
102
        case *ast.ChanType:
103
                r.filterType(nil, t.Value)
104
        }
105
}
106
 
107
func (r *reader) filterSpec(spec ast.Spec) bool {
108
        switch s := spec.(type) {
109
        case *ast.ImportSpec:
110
                // always keep imports so we can collect them
111
                return true
112
        case *ast.ValueSpec:
113
                s.Names = filterIdentList(s.Names)
114
                if len(s.Names) > 0 {
115
                        r.filterType(nil, s.Type)
116
                        return true
117
                }
118
        case *ast.TypeSpec:
119
                if ast.IsExported(s.Name.Name) {
120
                        r.filterType(r.lookupType(s.Name.Name), s.Type)
121
                        return true
122
                }
123
        }
124
        return false
125
}
126
 
127
func (r *reader) filterSpecList(list []ast.Spec) []ast.Spec {
128
        j := 0
129
        for _, s := range list {
130
                if r.filterSpec(s) {
131
                        list[j] = s
132
                        j++
133
                }
134
        }
135
        return list[0:j]
136
}
137
 
138
func (r *reader) filterDecl(decl ast.Decl) bool {
139
        switch d := decl.(type) {
140
        case *ast.GenDecl:
141
                d.Specs = r.filterSpecList(d.Specs)
142
                return len(d.Specs) > 0
143
        case *ast.FuncDecl:
144
                // ok to filter these methods early because any
145
                // conflicting method will be filtered here, too -
146
                // thus, removing these methods early will not lead
147
                // to the false removal of possible conflicts
148
                return ast.IsExported(d.Name.Name)
149
        }
150
        return false
151
}
152
 
153
// fileExports removes unexported declarations from src in place.
154
//
155
func (r *reader) fileExports(src *ast.File) {
156
        j := 0
157
        for _, d := range src.Decls {
158
                if r.filterDecl(d) {
159
                        src.Decls[j] = d
160
                        j++
161
                }
162
        }
163
        src.Decls = src.Decls[0:j]
164
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.