Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
w.bool(false)
w.expr(expr)
}
}
func (w *writer) declStmt(decl syntax.Decl) {
switch decl := decl.(type) {
default:
w.p.unexpected("declaration", decl)
case *syntax.ConstDecl:
case *syntax.TypeDecl:
// Quirk: The legacy inliner doesn't support inlining functions
// with type declarations. Unified IR doesn't have any need to
// write out type declarations explicitly (they're always looked
// up via global index tables instead), so we just write out a
// marker so the reader knows to synthesize a fake declaration to
// prevent inlining.
if quirksMode() {
w.code(stmtTypeDeclHack)
}
case *syntax.VarDecl:
values := unpackListExpr(decl.Values)
// Quirk: When N variables are declared with N initialization
// values, we need to decompose that into N interleaved
// declarations+initializations, because it leads to different
// (albeit semantically equivalent) code generation.
if quirksMode() && len(decl.NameList) == len(values) {
for i, name := range decl.NameList {
w.code(stmtAssign)
w.pos(decl)
w.exprList(values[i])
w.assignList(name)
}
break
}
w.code(stmtAssign)
w.pos(decl)
w.exprList(decl.Values)
w.assignList(namesAsExpr(decl.NameList))
}
}
func (w *writer) blockStmt(stmt *syntax.BlockStmt) {
w.sync(syncBlockStmt)
w.openScope(stmt.Pos())
w.stmts(stmt.List)
w.closeScope(stmt.Rbrace)
}
func (w *writer) forStmt(stmt *syntax.ForStmt) {
w.sync(syncForStmt)
w.openScope(stmt.Pos())
if rang, ok := stmt.Init.(*syntax.RangeClause); w.bool(ok) {
w.pos(rang)
w.expr(rang.X)
w.assignList(rang.Lhs)
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
} else {
w.pos(stmt)
w.stmt(stmt.Init)
w.expr(stmt.Cond)
w.stmt(stmt.Post)
}
w.blockStmt(stmt.Body)
w.closeAnotherScope()
}
func (w *writer) ifStmt(stmt *syntax.IfStmt) {
w.sync(syncIfStmt)
w.openScope(stmt.Pos())
w.pos(stmt)
w.stmt(stmt.Init)
w.expr(stmt.Cond)
w.blockStmt(stmt.Then)
w.stmt(stmt.Else)
w.closeAnotherScope()
}
func (w *writer) selectStmt(stmt *syntax.SelectStmt) {
w.sync(syncSelectStmt)
w.pos(stmt)
w.len(len(stmt.Body))
for i, clause := range stmt.Body {
if i > 0 {
w.closeScope(clause.Pos())
}
w.openScope(clause.Pos())
w.pos(clause)
w.stmt(clause.Comm)
w.stmts(clause.Body)
}
if len(stmt.Body) > 0 {
w.closeScope(stmt.Rbrace)
}
}
func (w *writer) switchStmt(stmt *syntax.SwitchStmt) {
w.sync(syncSwitchStmt)
w.openScope(stmt.Pos())
w.pos(stmt)
w.stmt(stmt.Init)
if guard, ok := stmt.Tag.(*syntax.TypeSwitchGuard); w.bool(ok) {
w.pos(guard)
if tag := guard.Lhs; w.bool(tag != nil) {
w.pos(tag)
w.string(tag.Value)
}
w.expr(guard.X)
} else {
w.expr(stmt.Tag)
}
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
w.len(len(stmt.Body))
for i, clause := range stmt.Body {
if i > 0 {
w.closeScope(clause.Pos())
}
w.openScope(clause.Pos())
w.pos(clause)
w.exprList(clause.Cases)
if obj, ok := w.p.info.Implicits[clause]; ok {
// TODO(mdempsky): These pos details are quirkish, but also
// necessary so the variable's position is correct for DWARF
// scope assignment later. It would probably be better for us to
// instead just set the variable's DWARF scoping info earlier so
// we can give it the correct position information.
pos := clause.Pos()
if typs := unpackListExpr(clause.Cases); len(typs) != 0 {
pos = typeExprEndPos(typs[len(typs)-1])
}
w.pos(pos)
obj := obj.(*types2.Var)
w.typ(obj.Type())
w.addLocal(obj)
}
w.stmts(clause.Body)
}
if len(stmt.Body) > 0 {
w.closeScope(stmt.Rbrace)
}
w.closeScope(stmt.Rbrace)
}
func (w *writer) label(label *syntax.Name) {
w.sync(syncLabel)
// TODO(mdempsky): Replace label strings with dense indices.
w.string(label.Value)
}
func (w *writer) optLabel(label *syntax.Name) {
w.sync(syncOptLabel)
if w.bool(label != nil) {
w.label(label)
}
}
// @@@ Expressions
func (w *writer) expr(expr syntax.Expr) {
expr = unparen(expr) // skip parens; unneeded after typecheck
obj, targs := lookupObj(w.p.info, expr)
if tv, ok := w.p.info.Types[expr]; ok {
Matthew Dempsky
committed
// TODO(mdempsky): Be more judicious about which types are marked as "needed".
w.needType(tv.Type)
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
if tv.IsType() {
w.code(exprType)
w.typ(tv.Type)
return
}
if tv.Value != nil {
pos := expr.Pos()
if quirksMode() {
if obj != nil {
// Quirk: IR (and thus iexport) doesn't track position
// information for uses of declared objects.
pos = syntax.Pos{}
} else if tv.Value.Kind() == constant.String {
// Quirk: noder.sum picks a particular position for certain
// string concatenations.
pos = sumPos(expr)
}
}
w.code(exprConst)
w.pos(pos)
w.value(tv.Type, tv.Value)
// TODO(mdempsky): These details are only important for backend
// diagnostics. Explore writing them out separately.
w.op(constExprOp(expr))
w.string(syntax.String(expr))
return
}
}
if obj != nil {
if isGlobal(obj) {
w.code(exprName)
w.obj(obj, targs)
return
}
obj := obj.(*types2.Var)
assert(len(targs) == 0)
w.code(exprLocal)
w.useLocal(expr.Pos(), obj)
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
return
}
switch expr := expr.(type) {
default:
w.p.unexpected("expression", expr)
case nil: // absent slice index, for condition, or switch tag
w.code(exprNone)
case *syntax.Name:
assert(expr.Value == "_")
w.code(exprBlank)
case *syntax.CompositeLit:
w.code(exprCompLit)
w.compLit(expr)
case *syntax.FuncLit:
w.code(exprFuncLit)
w.funcLit(expr)
case *syntax.SelectorExpr:
sel, ok := w.p.info.Selections[expr]
assert(ok)
w.code(exprSelector)
w.expr(expr.X)
w.pos(expr)
w.selector(sel.Obj())
case *syntax.IndexExpr:
tv, ok := w.p.info.Types[expr.Index]
assert(ok && tv.IsValue())
w.code(exprIndex)
w.expr(expr.X)
w.pos(expr)
w.expr(expr.Index)
case *syntax.SliceExpr:
w.code(exprSlice)
w.expr(expr.X)
w.pos(expr)
for _, n := range &expr.Index {
w.expr(n)
}
case *syntax.AssertExpr:
w.code(exprAssert)
w.expr(expr.X)
w.pos(expr)
w.expr(expr.Type)
case *syntax.Operation:
if expr.Y == nil {
w.code(exprUnaryOp)
w.op(unOps[expr.Op])
w.pos(expr)
w.expr(expr.X)
break
}
w.code(exprBinaryOp)
w.op(binOps[expr.Op])
w.expr(expr.X)
w.pos(expr)
w.expr(expr.Y)
case *syntax.CallExpr:
tv, ok := w.p.info.Types[expr.Fun]
assert(ok)
if tv.IsType() {
assert(len(expr.ArgList) == 1)
assert(!expr.HasDots)
w.code(exprConvert)
w.typ(tv.Type)
w.pos(expr)
w.expr(expr.ArgList[0])
break
}
writeFunExpr := func() {
if selector, ok := unparen(expr.Fun).(*syntax.SelectorExpr); ok {
if sel, ok := w.p.info.Selections[selector]; ok && sel.Kind() == types2.MethodVal {
w.expr(selector.X)
w.bool(true) // method call
w.pos(selector)
w.selector(sel.Obj())
return
}
}
if inf, ok := w.p.info.Inferred[expr]; ok {
obj, _ := lookupObj(w.p.info, expr.Fun)
assert(obj != nil)
// As if w.expr(expr.Fun), but using inf.TArgs instead.
w.code(exprName)
w.obj(obj, inf.TArgs)
} else {
w.expr(expr.Fun)
}
w.bool(false) // not a method call (i.e., normal function call)
w.code(exprCall)
writeFunExpr()
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
w.pos(expr)
w.exprs(expr.ArgList)
w.bool(expr.HasDots)
}
}
func (w *writer) compLit(lit *syntax.CompositeLit) {
tv, ok := w.p.info.Types[lit]
assert(ok)
w.sync(syncCompLit)
w.pos(lit)
w.typ(tv.Type)
typ := tv.Type
if ptr, ok := typ.Underlying().(*types2.Pointer); ok {
typ = ptr.Elem()
}
str, isStruct := typ.Underlying().(*types2.Struct)
w.len(len(lit.ElemList))
for i, elem := range lit.ElemList {
if isStruct {
if kv, ok := elem.(*syntax.KeyValueExpr); ok {
// use position of expr.Key rather than of elem (which has position of ':')
w.pos(kv.Key)
w.len(fieldIndex(w.p.info, str, kv.Key.(*syntax.Name)))
elem = kv.Value
} else {
w.pos(elem)
w.len(i)
}
} else {
if kv, ok := elem.(*syntax.KeyValueExpr); w.bool(ok) {
// use position of expr.Key rather than of elem (which has position of ':')
w.pos(kv.Key)
w.expr(kv.Key)
elem = kv.Value
}
}
w.pos(elem)
w.expr(elem)
}
}
func (w *writer) funcLit(expr *syntax.FuncLit) {
tv, ok := w.p.info.Types[expr]
assert(ok)
sig := tv.Type.(*types2.Signature)
body, closureVars := w.p.bodyIdx(w.p.curpkg, sig, expr.Body, w.dict)
w.sync(syncFuncLit)
w.pos(expr)
w.pos(expr.Type) // for QuirksMode
w.signature(sig)
w.len(len(closureVars))
for _, cv := range closureVars {
w.pos(cv.pos)
if quirksMode() {
cv.pos = expr.Body.Rbrace
}
w.useLocal(cv.pos, cv.obj)
w.reloc(relocBody, body)
}
type posObj struct {
pos syntax.Pos
obj *types2.Var
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
}
func (w *writer) exprList(expr syntax.Expr) {
w.sync(syncExprList)
w.exprs(unpackListExpr(expr))
}
func (w *writer) exprs(exprs []syntax.Expr) {
if len(exprs) == 0 {
assert(exprs == nil)
}
w.sync(syncExprs)
w.len(len(exprs))
for _, expr := range exprs {
w.expr(expr)
}
}
func (w *writer) op(op ir.Op) {
// TODO(mdempsky): Remove in favor of explicit codes? Would make
// export data more stable against internal refactorings, but low
// priority at the moment.
assert(op != 0)
w.sync(syncOp)
w.len(int(op))
}
Matthew Dempsky
committed
func (w *writer) needType(typ types2.Type) {
// Decompose tuple into component element types.
if typ, ok := typ.(*types2.Tuple); ok {
for i := 0; i < typ.Len(); i++ {
w.needType(typ.At(i).Type())
}
return
}
if info := w.p.typIdx(typ, w.dict); info.derived {
w.dict.derived[info.idx].needed = true
}
}
// @@@ Package initialization
// Caution: This code is still clumsy, because toolstash -cmp is
// particularly sensitive to it.
type typeDeclGen struct {
*syntax.TypeDecl
gen int
// Implicit type parameters in scope at this type declaration.
implicits []*types2.TypeName
type fileImports struct {
importedEmbed, importedUnsafe bool
}
type declCollector struct {
pw *pkgWriter
typegen *int
file *fileImports
withinFunc bool
implicits []*types2.TypeName
}
func (c *declCollector) withTParams(obj types2.Object) *declCollector {
tparams := objTypeParams(obj)
if len(tparams) == 0 {
return c
}
copy := *c
copy.implicits = copy.implicits[:len(copy.implicits):len(copy.implicits)]
copy.implicits = append(copy.implicits, objTypeParams(obj)...)
return ©
func (c *declCollector) Visit(n syntax.Node) syntax.Visitor {
pw := c.pw
switch n := n.(type) {
case *syntax.File:
pw.checkPragmas(n.Pragma, ir.GoBuildPragma, false)
case *syntax.ImportDecl:
pw.checkPragmas(n.Pragma, 0, false)
switch pkgNameOf(pw.info, n).Imported().Path() {
case "embed":
c.file.importedEmbed = true
case "unsafe":
c.file.importedUnsafe = true
}
case *syntax.ConstDecl:
pw.checkPragmas(n.Pragma, 0, false)
case *syntax.FuncDecl:
pw.checkPragmas(n.Pragma, funcPragmas, false)
obj := pw.info.Defs[n.Name].(*types2.Func)
pw.funDecls[obj] = n
return c.withTParams(obj)
case *syntax.TypeDecl:
obj := pw.info.Defs[n.Name].(*types2.TypeName)
d := typeDeclGen{TypeDecl: n, implicits: c.implicits}
if n.Alias {
pw.checkPragmas(n.Pragma, 0, false)
} else {
pw.checkPragmas(n.Pragma, typePragmas, false)
// Assign a unique ID to function-scoped defined types.
if c.withinFunc {
*c.typegen++
d.gen = *c.typegen
}
}
pw.typDecls[obj] = d
// TODO(mdempsky): Omit? Not strictly necessary; only matters for
// type declarations within function literals within parameterized
// type declarations, but types2 the function literals will be
// constant folded away.
return c.withTParams(obj)
case *syntax.VarDecl:
pw.checkPragmas(n.Pragma, 0, true)
if p, ok := n.Pragma.(*pragmas); ok && len(p.Embeds) > 0 {
if err := checkEmbed(n, c.file.importedEmbed, c.withinFunc); err != nil {
pw.errorf(p.Embeds[0].Pos, "%s", err)
}
}
// Workaround for #46208. For variable declarations that
// declare multiple variables and have an explicit type
// expression, the type expression is evaluated multiple
// times. This affects toolstash -cmp, because iexport is
// sensitive to *types.Type pointer identity.
if quirksMode() && n.Type != nil {
tv, ok := pw.info.Types[n.Type]
assert(ok)
assert(tv.IsType())
for _, name := range n.NameList {
obj := pw.info.Defs[name].(*types2.Var)
pw.dups.add(obj.Type(), tv.Type)
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
}
case *syntax.BlockStmt:
if !c.withinFunc {
copy := *c
copy.withinFunc = true
return ©
}
}
return c
}
func (pw *pkgWriter) collectDecls(noders []*noder) {
var typegen int
for _, p := range noders {
var file fileImports
syntax.Walk(p.file, &declCollector{
pw: pw,
typegen: &typegen,
file: &file,
})
pw.cgoPragmas = append(pw.cgoPragmas, p.pragcgobuf...)
for _, l := range p.linknames {
if !file.importedUnsafe {
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
pw.errorf(l.pos, "//go:linkname only allowed in Go files that import \"unsafe\"")
continue
}
switch obj := pw.curpkg.Scope().Lookup(l.local).(type) {
case *types2.Func, *types2.Var:
if _, ok := pw.linknames[obj]; !ok {
pw.linknames[obj] = l.remote
} else {
pw.errorf(l.pos, "duplicate //go:linkname for %s", l.local)
}
default:
// TODO(mdempsky): Enable after #42938 is fixed.
if false {
pw.errorf(l.pos, "//go:linkname must refer to declared function or variable")
}
}
}
}
}
func (pw *pkgWriter) checkPragmas(p syntax.Pragma, allowed ir.PragmaFlag, embedOK bool) {
if p == nil {
return
}
pragma := p.(*pragmas)
for _, pos := range pragma.Pos {
if pos.Flag&^allowed != 0 {
pw.errorf(pos.Pos, "misplaced compiler directive")
}
}
if !embedOK {
for _, e := range pragma.Embeds {
pw.errorf(e.Pos, "misplaced go:embed directive")
}
}
}
func (w *writer) pkgInit(noders []*noder) {
if quirksMode() {
posBases := posBasesOf(noders)
w.len(len(posBases))
for _, posBase := range posBases {
w.posBase(posBase)
}
objs := importedObjsOf(w.p.curpkg, w.p.info, noders)
w.len(len(objs))
for _, obj := range objs {
w.qualifiedIdent(obj)
}
}
w.len(len(w.p.cgoPragmas))
for _, cgoPragma := range w.p.cgoPragmas {
w.strings(cgoPragma)
}
w.sync(syncDecls)
for _, p := range noders {
for _, decl := range p.file.DeclList {
w.pkgDecl(decl)
}
}
w.code(declEnd)
w.sync(syncEOF)
}
func (w *writer) pkgDecl(decl syntax.Decl) {
switch decl := decl.(type) {
default:
w.p.unexpected("declaration", decl)
case *syntax.ImportDecl:
case *syntax.ConstDecl:
w.code(declOther)
w.pkgObjs(decl.NameList...)
case *syntax.FuncDecl:
obj := w.p.info.Defs[decl.Name].(*types2.Func)
sig := obj.Type().(*types2.Signature)
if sig.RParams() != nil || sig.TParams() != nil {
break // skip generic functions
}
if recv := sig.Recv(); recv != nil && obj.Name() != "_" {
w.code(declMethod)
w.typ(recvBase(recv))
w.selector(obj)
break
}
w.code(declFunc)
w.pkgObjs(decl.Name)
case *syntax.TypeDecl:
if len(decl.TParamList) != 0 {
break // skip generic type decls
}
if decl.Name.Value == "_" {
break // skip blank type decls
}
name := w.p.info.Defs[decl.Name].(*types2.TypeName)
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
// Skip type declarations for interfaces that are only usable as
// type parameter bounds.
if iface, ok := name.Type().Underlying().(*types2.Interface); ok && iface.IsConstraint() {
break
}
// Skip aliases to uninstantiated generic types.
// TODO(mdempsky): Revisit after #46477 is resolved.
if name.IsAlias() {
named, ok := name.Type().(*types2.Named)
if ok && len(named.TParams()) != 0 && len(named.TArgs()) == 0 {
break
}
}
w.code(declOther)
w.pkgObjs(decl.Name)
case *syntax.VarDecl:
w.code(declVar)
w.pos(decl)
w.pkgObjs(decl.NameList...)
w.exprList(decl.Values)
var embeds []pragmaEmbed
if p, ok := decl.Pragma.(*pragmas); ok {
embeds = p.Embeds
}
w.len(len(embeds))
for _, embed := range embeds {
w.pos(embed.Pos)
w.strings(embed.Patterns)
}
}
}
func (w *writer) pkgObjs(names ...*syntax.Name) {
w.sync(syncDeclNames)
w.len(len(names))
for _, name := range names {
obj, ok := w.p.info.Defs[name]
assert(ok)
w.sync(syncDeclName)
w.obj(obj, nil)
}
}
// @@@ Helpers
// isDefinedType reports whether obj is a defined type.
func isDefinedType(obj types2.Object) bool {
if obj, ok := obj.(*types2.TypeName); ok {
return !obj.IsAlias()
}
return false
}
// isGlobal reports whether obj was declared at package scope.
//
// Caveat: blank objects are not declared.
func isGlobal(obj types2.Object) bool {
return obj.Parent() == obj.Pkg().Scope()
}
// lookupObj returns the object that expr refers to, if any. If expr
// is an explicit instantiation of a generic object, then the type
// arguments are returned as well.
func lookupObj(info *types2.Info, expr syntax.Expr) (obj types2.Object, targs []types2.Type) {
if index, ok := expr.(*syntax.IndexExpr); ok {
if inf, ok := info.Inferred[index]; ok {
targs = inf.TArgs
} else {
args := unpackListExpr(index.Index)
if len(args) == 1 {
tv, ok := info.Types[args[0]]
assert(ok)
if tv.IsValue() {
return // normal index expression
}
}
targs = make([]types2.Type, len(args))
for i, arg := range args {
tv, ok := info.Types[arg]
assert(ok)
assert(tv.IsType())
targs[i] = tv.Type
}
}
expr = index.X
}
// Strip package qualifier, if present.
if sel, ok := expr.(*syntax.SelectorExpr); ok {
if !isPkgQual(info, sel) {
return // normal selector expression
}
expr = sel.Sel
}
if name, ok := expr.(*syntax.Name); ok {
obj, _ = info.Uses[name]
}
return
}
// isPkgQual reports whether the given selector expression is a
// package-qualified identifier.
func isPkgQual(info *types2.Info, sel *syntax.SelectorExpr) bool {
if name, ok := sel.X.(*syntax.Name); ok {
_, isPkgName := info.Uses[name].(*types2.PkgName)
return isPkgName
}
return false
}
// recvBase returns the base type for the given receiver parameter.
func recvBase(recv *types2.Var) *types2.Named {
typ := recv.Type()
if ptr, ok := typ.(*types2.Pointer); ok {
typ = ptr.Elem()
}
return typ.(*types2.Named)
}
// namesAsExpr returns a list of names as a syntax.Expr.
func namesAsExpr(names []*syntax.Name) syntax.Expr {
if len(names) == 1 {
return names[0]
}
exprs := make([]syntax.Expr, len(names))
for i, name := range names {
exprs[i] = name
}
return &syntax.ListExpr{ElemList: exprs}
}
// fieldIndex returns the index of the struct field named by key.
func fieldIndex(info *types2.Info, str *types2.Struct, key *syntax.Name) int {
field := info.Uses[key].(*types2.Var)
for i := 0; i < str.NumFields(); i++ {
if str.Field(i) == field {
return i
}
}
panic(fmt.Sprintf("%s: %v is not a field of %v", key.Pos(), field, str))
}
// objTypeParams returns the type parameters on the given object.
func objTypeParams(obj types2.Object) []*types2.TypeName {
switch obj := obj.(type) {
case *types2.Func:
sig := obj.Type().(*types2.Signature)
if sig.Recv() != nil {
return sig.RParams()
}
return sig.TParams()
case *types2.TypeName:
if !obj.IsAlias() {
return obj.Type().(*types2.Named).TParams()
}
}
return nil
}
func asPragmaFlag(p syntax.Pragma) ir.PragmaFlag {
if p == nil {
return 0
}
return p.(*pragmas).Flag
}