// MergePackageFiles creates a file AST by merging the ASTs of the
// files belonging to a package. The mode flags control merging behavior.
//

MergePackageFiles creates a file AST by merging the ASTs of the
files belonging to a package. The mode flags control merging behavior.
// MergePackageFiles creates a file AST by merging the ASTs of the // files belonging to a package . The mode flags control merging behavior . //
func MergePackageFiles(pkg *Package, mode MergeMode) *File {
	ndocs := 0
	ncomments := 0
	ndecls := 0
	for _, f := range pkg.Files {
		if f.Doc != nil {
			ndocs += len(f.Doc.List) + 1 
		}
		ncomments += len(f.Comments)
		ndecls += len(f.Decls)
	}
	var doc *CommentGroup
	var pos token.Pos
	if ndocs > 0 {
		list := make([]*Comment, ndocs-1) 
		i := 0
		for _, f := range pkg.Files {
			if f.Doc != nil {
				if i > 0 {
					list[i] = separator
					i++
				}
				for _, c := range f.Doc.List {
					list[i] = c
					i++
				}
				if f.Package > pos {
					pos = f.Package
				}
			}
		}
		doc = &CommentGroup{list}
	}
	var decls []Decl
	if ndecls > 0 {
		decls = make([]Decl, ndecls)
		funcs := make(map[string]int) 
		i := 0                        
		n := 0                        
		for _, f := range pkg.Files {
			for _, d := range f.Decls {
				if mode&FilterFuncDuplicates != 0 {
					if f, isFun := d.(*FuncDecl); isFun {
						name := f.Name.Name
						if j, exists := funcs[name]; exists {
							if decls[j] != nil && decls[j].(*FuncDecl).Doc == nil {
								decls[j] = nil
							} else {
								d = nil
							}
							n++ 
						} else {
							funcs[name] = i
						}
					}
				}
				decls[i] = d
				i++
			}
		}
		if n > 0 {
			i = 0
			for _, d := range decls {
				if d != nil {
					decls[i] = d
					i++
				}
			}
			decls = decls[0:i]
		}
	}
	var comments []*CommentGroup
	if mode&FilterUnassociatedComments == 0 {
		comments = make([]*CommentGroup, ncomments)
		i := 0
		for _, f := range pkg.Files {
			i += copy(comments[i:], f.Comments)
		}
	}
	return &File{doc, pos, NewIdent(pkg.Name), decls, nil, nil, nil, comments}
}
func MergePackageFiles ( pkg * Package , mode MergeMode ) * File { ndocs := 0 ncomments := 0 ndecls := 0 for _ , f := range pkg . Files { if f . Doc != nil { ndocs += len ( f . Doc . List ) + 1 } ncomments += len ( f . Comments ) ndecls += len ( f . Decls ) } var doc * CommentGroup var pos token . Pos if ndocs > 0 { list := make ( [ ] * Comment , ndocs - 1 ) i := 0 for _ , f := range pkg . Files { if f . Doc != nil { if i > 0 { list [ i ] = separator i ++ } for _ , c := range f . Doc . List { list [ i ] = c i ++ } if f . Package > pos { pos = f . Package } } } doc = & CommentGroup { list } } var decls [ ] Decl if ndecls > 0 { decls = make ( [ ] Decl , ndecls ) funcs := make ( map [ string ] int ) i := 0 n := 0 for _ , f := range pkg . Files { for _ , d := range f . Decls { if mode & FilterFuncDuplicates != 0 { if f , isFun := d . ( * FuncDecl ) ; isFun { name := f . Name . Name if j , exists := funcs [ name ] ; exists { if decls [ j ] != nil && decls [ j ] . ( * FuncDecl ) . Doc == nil { decls [ j ] = nil } else { d = nil } n ++ } else { funcs [ name ] = i } } } decls [ i ] = d i ++ } } if n > 0 { i = 0 for _ , d := range decls { if d != nil { decls [ i ] = d i ++ } } decls = decls [ 0 : i ] } } var comments [ ] * CommentGroup if mode & FilterUnassociatedComments == 0 { comments = make ( [ ] * CommentGroup , ncomments ) i := 0 for _ , f := range pkg . Files { i += copy ( comments [ i : ] , f . Comments ) } } return & File { doc , pos , NewIdent ( pkg . Name ) , decls , nil , nil , nil , comments } }
MergePackageFiles creates a file AST by merging the ASTs of the
files belonging to a package.
MergePackageFiles creates a file AST by merging the ASTs of the files belonging to a package .
// Count the number of package docs, comments and declarations across
 // all package files.
 // +1 for separator
 // Collect package comments from all package files into a single
 // CommentGroup - the collected package documentation. The order
 // is unspecified. In general there should be only one file with
 // a package comment; but it's better to collect extra comments
 // than drop them on the floor.
 // -1: no separator before first group
 // not the first group - add separator
 // Keep the maximum package clause position as
 // position for the package clause of the merged
 // files.
 // Collect declarations from all package files.
 // map of global function name -> decls index
 // current index
 // number of filtered entries
 // A language entity may be declared multiple
 // times in different package files; only at
 // build time declarations must be unique.
 // For now, exclude multiple declarations of
 // functions - keep the one with documentation.
 //
 // TODO(gri): Expand this filtering to other
 //            entities (const, type, vars) if
 //            multiple declarations are common.
 // function declared already
 // existing declaration has no documentation;
 // ignore the existing declaration
 // ignore the new declaration
 // filtered an entry
 // Eliminate nil entries from the decls list if entries were
 // filtered. We do this using a 2nd pass in order to not disturb
 // the original declaration order in the source (otherwise, this
 // would also invalidate the monotonically increasing position
 // info within a single file).
 // Collect comments from all package files.
 // TODO(gri) need to compute pkgScope and unresolved identifiers!
 // TODO(gri) need to compute imports!
