Skip to content

Commit d8fe4d2

Browse files
authored
Merge pull request #18489 from owen-mc/go/rename-namedtype-definedtype
Go: Rename "named type" to "defined type"
2 parents 083c756 + 1a52398 commit d8fe4d2

34 files changed

+2319
-89
lines changed

go/downgrades/b3da71c3ac204b557c86e9d9c26012360bdbdccb/go.dbscheme

+552
Large diffs are not rendered by default.

go/downgrades/b3da71c3ac204b557c86e9d9c26012360bdbdccb/old.dbscheme

+552
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
description: Rename @definedtype to @namedtype
2+
compatibility: full

go/extractor/dbscheme/tables.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -694,13 +694,13 @@ var BuiltinObjectType = NewUnionType("@builtinobject")
694694
// PkgObjectType is the type of imported packages
695695
var PkgObjectType = ObjectKind.NewBranch("@pkgobject")
696696

697-
// TypeObjectType is the type of declared or built-in named types
697+
// TypeObjectType is the type of named types (predeclared types, defined types, type parameters and aliases which refer to those things)
698698
var TypeObjectType = NewUnionType("@typeobject")
699699

700-
// DeclTypeObjectType is the type of declared named types
700+
// DeclTypeObjectType is the type of defined types, type parameters and aliases which refer to named types
701701
var DeclTypeObjectType = ObjectKind.NewBranch("@decltypeobject", TypeObjectType, DeclObjectType, TypeParamParentObjectType)
702702

703-
// BuiltinTypeObjectType is the type of built-in named types
703+
// BuiltinTypeObjectType is the type of built-in types (predeclared types)
704704
var BuiltinTypeObjectType = ObjectKind.NewBranch("@builtintypeobject", TypeObjectType, BuiltinObjectType)
705705

706706
// ValueObjectType is the type of declared or built-in variables or constants
@@ -855,8 +855,8 @@ var ChanTypes = map[gotypes.ChanDir]*BranchType{
855855
gotypes.SendRecv: TypeKind.NewBranch("@sendrcvchantype", ChanType),
856856
}
857857

858-
// NamedType is the type of named types
859-
var NamedType = TypeKind.NewBranch("@namedtype", CompositeType)
858+
// DefinedType is the type of defined types
859+
var DefinedType = TypeKind.NewBranch("@definedtype", CompositeType)
860860

861861
// TypeSetLiteral is the type of type set literals
862862
var TypeSetLiteral = TypeKind.NewBranch("@typesetliteraltype", CompositeType)
@@ -1080,10 +1080,10 @@ var FieldStructsTable = NewTable("fieldstructs",
10801080
EntityColumn(StructType, "struct"),
10811081
)
10821082

1083-
// MethodHostsTable maps interface methods to the named type they belong to
1083+
// MethodHostsTable maps interface methods to the defined type they belong to
10841084
var MethodHostsTable = NewTable("methodhosts",
10851085
EntityColumn(ObjectType, "method"),
1086-
EntityColumn(NamedType, "host"),
1086+
EntityColumn(DefinedType, "host"),
10871087
)
10881088

10891089
// DefsTable maps identifiers to the objects they define
@@ -1110,7 +1110,7 @@ var TypeOfTable = NewTable("type_of",
11101110
EntityColumn(TypeType, "tp"),
11111111
)
11121112

1113-
// TypeNameTable is the table associating named types with their names
1113+
// TypeNameTable is the table associating defined types with their names
11141114
var TypeNameTable = NewTable("typename",
11151115
EntityColumn(TypeType, "tp").Unique(),
11161116
StringColumn("name"),
@@ -1135,10 +1135,10 @@ var BaseTypeTable = NewTable("base_type",
11351135
EntityColumn(TypeType, "tp"),
11361136
)
11371137

1138-
// UnderlyingTypeTable is the table associating named types with their
1138+
// UnderlyingTypeTable is the table associating defined types with their
11391139
// underlying type
11401140
var UnderlyingTypeTable = NewTable("underlying_type",
1141-
EntityColumn(NamedType, "named").Unique(),
1141+
EntityColumn(DefinedType, "defined").Unique(),
11421142
EntityColumn(TypeType, "tp"),
11431143
)
11441144

go/extractor/extractor.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ func extractObjects(tw *trap.Writer, scope *types.Scope, scopeLabel trap.Label)
475475
populateTypeParamParents(funcObj.Type().(*types.Signature).TypeParams(), obj)
476476
populateTypeParamParents(funcObj.Type().(*types.Signature).RecvTypeParams(), obj)
477477
}
478-
// Populate type parameter parents for named types.
478+
// Populate type parameter parents for defined types and alias types.
479479
if typeNameObj, ok := obj.(*types.TypeName); ok {
480480
// `types.TypeName` represents a type with a name: a defined
481481
// type, an alias type, a type parameter, or a predeclared
@@ -574,7 +574,7 @@ func extractObject(tw *trap.Writer, obj types.Object, lbl trap.Label) {
574574
// For more information on objects, see:
575575
// https://github.com/golang/example/blob/master/gotypes/README.md#objects
576576
func extractObjectTypes(tw *trap.Writer) {
577-
// calling `extractType` on a named type will extract all methods defined
577+
// calling `extractType` on a defined type will extract all methods defined
578578
// on it, which will add new objects. Therefore we need to do this first
579579
// before we loop over all objects and emit them.
580580
changed := true
@@ -1695,7 +1695,7 @@ func extractType(tw *trap.Writer, tp types.Type) trap.Label {
16951695
extractElementType(tw, lbl, tp.Elem())
16961696
case *types.Named:
16971697
origintp := tp.Origin()
1698-
kind = dbscheme.NamedType.Index()
1698+
kind = dbscheme.DefinedType.Index()
16991699
dbscheme.TypeNameTable.Emit(tw, lbl, origintp.Obj().Name())
17001700
underlying := origintp.Underlying()
17011701
extractUnderlyingType(tw, lbl, underlying)
@@ -1767,9 +1767,9 @@ func extractType(tw *trap.Writer, tp types.Type) trap.Label {
17671767
// Type labels refer to global keys to ensure that if the same type is
17681768
// encountered during the extraction of different files it is still ultimately
17691769
// mapped to the same entity. In particular, this means that keys for compound
1770-
// types refer to the labels of their component types. For named types, the key
1770+
// types refer to the labels of their component types. For defined types, the key
17711771
// is constructed from their globally unique ID. This prevents cyclic type keys
1772-
// since type recursion in Go always goes through named types.
1772+
// since type recursion in Go always goes through defined types.
17731773
func getTypeLabel(tw *trap.Writer, tp types.Type) (trap.Label, bool) {
17741774
tp = resolveTypeAlias(tp)
17751775
lbl, exists := tw.Labeler.TypeLabels[tp]
@@ -1874,12 +1874,12 @@ func getTypeLabel(tw *trap.Writer, tp types.Type) (trap.Label, bool) {
18741874
origintp := tp.Origin()
18751875
entitylbl, exists := tw.Labeler.LookupObjectID(origintp.Obj(), lbl)
18761876
if entitylbl == trap.InvalidLabel {
1877-
panic(fmt.Sprintf("Cannot construct label for named type %v (underlying object is %v).\n", origintp, origintp.Obj()))
1877+
panic(fmt.Sprintf("Cannot construct label for defined type %v (underlying object is %v).\n", origintp, origintp.Obj()))
18781878
}
18791879
if !exists {
18801880
extractObject(tw, origintp.Obj(), entitylbl)
18811881
}
1882-
lbl = tw.Labeler.GlobalID(fmt.Sprintf("{%s};namedtype", entitylbl))
1882+
lbl = tw.Labeler.GlobalID(fmt.Sprintf("{%s};definedtype", entitylbl))
18831883
case *types.TypeParam:
18841884
parentlbl := getTypeParamParentLabel(tw, tp)
18851885
idx := tp.Index()
@@ -1921,9 +1921,9 @@ func extractBaseType(tw *trap.Writer, ptr trap.Label, base types.Type) {
19211921
}
19221922

19231923
// extractUnderlyingType extracts `underlying` as the underlying type of the
1924-
// named type `named`
1925-
func extractUnderlyingType(tw *trap.Writer, named trap.Label, underlying types.Type) {
1926-
dbscheme.UnderlyingTypeTable.Emit(tw, named, extractType(tw, underlying))
1924+
// defined type `defined`
1925+
func extractUnderlyingType(tw *trap.Writer, defined trap.Label, underlying types.Type) {
1926+
dbscheme.UnderlyingTypeTable.Emit(tw, defined, extractType(tw, underlying))
19271927
}
19281928

19291929
// extractComponentType extracts `component` as the `idx`th component type of `parent` with name `name`
@@ -2173,8 +2173,8 @@ func checkObjectNotSpecialized(obj types.Object) {
21732173
log.Fatalf("Encountered unexpected specialization %s of generic variable object %s", varObj.String(), varObj.Origin().String())
21742174
}
21752175
if typeNameObj, ok := obj.(*types.TypeName); ok {
2176-
if namedType, ok := typeNameObj.Type().(*types.Named); ok && namedType != namedType.Origin() {
2177-
log.Fatalf("Encountered type object for specialization %s of named type %s", namedType.String(), namedType.Origin().String())
2176+
if definedType, ok := typeNameObj.Type().(*types.Named); ok && definedType != definedType.Origin() {
2177+
log.Fatalf("Encountered type object for specialization %s of defined type %s", definedType.String(), definedType.Origin().String())
21782178
}
21792179
}
21802180
}

go/extractor/trap/labels.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ func findMethodWithGivenReceiver(object types.Object) *types.Func {
181181

182182
// findMethodWithGivenReceiver finds a method on type `tp` with `object` as its receiver, if one exists
183183
func findMethodOnTypeWithGivenReceiver(tp types.Type, object types.Object) *types.Func {
184-
if namedType, ok := tp.(*types.Named); ok {
185-
for i := 0; i < namedType.NumMethods(); i++ {
186-
meth := namedType.Method(i)
184+
if definedType, ok := tp.(*types.Named); ok {
185+
for i := 0; i < definedType.NumMethods(); i++ {
186+
meth := definedType.Method(i)
187187
if object == meth.Type().(*types.Signature).Recv() {
188188
return meth
189189
}

go/ql/examples/snippets/incompleteswitchoverenum.ql

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import go
1111

12-
from ExpressionSwitchStmt ss, DeclaredConstant c, NamedType t
12+
from ExpressionSwitchStmt ss, DeclaredConstant c, DefinedType t
1313
where
1414
t.getUnderlyingType() instanceof IntegerType and
1515
t = ss.getExpr().getType() and
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
category: deprecated
3+
---
4+
* The class `NamedType` has been deprecated. Use the new class `DefinedType` instead. This better matches the terminology used in the Go language specification, which was changed in Go 1.9.
5+
* The member predicate `getNamedType` on `GoMicro::ServiceInterfaceType` has been deprecated. Use the new member predicate `getDefinedType` instead.
6+
* The member predicate `getNamedType` on `Twirp::ServiceInterfaceType` has been deprecated. Use the new member predicate `getDefinedType` instead.

go/ql/lib/go.dbscheme

+4-4
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ methodreceivers(unique int method: @object ref, int receiver: @object ref);
184184

185185
fieldstructs(unique int field: @object ref, int struct: @structtype ref);
186186

187-
methodhosts(int method: @object ref, int host: @namedtype ref);
187+
methodhosts(int method: @object ref, int host: @definedtype ref);
188188

189189
defs(int ident: @ident ref, int object: @object ref);
190190

@@ -202,7 +202,7 @@ element_type(unique int container: @containertype ref, int tp: @type ref);
202202

203203
base_type(unique int ptr: @pointertype ref, int tp: @type ref);
204204

205-
underlying_type(unique int named: @namedtype ref, int tp: @type ref);
205+
underlying_type(unique int defined: @definedtype ref, int tp: @type ref);
206206

207207
#keyset[parent, index]
208208
component_types(int parent: @compositetype ref, int index: int ref, string name: string ref, int tp: @type ref);
@@ -506,7 +506,7 @@ case @type.kind of
506506
| 35 = @sendchantype
507507
| 36 = @recvchantype
508508
| 37 = @sendrcvchantype
509-
| 38 = @namedtype
509+
| 38 = @definedtype
510510
| 39 = @typesetliteraltype;
511511

512512
@basictype = @booltype | @numerictype | @stringtype | @literaltype | @invalidtype | @unsafepointertype;
@@ -531,7 +531,7 @@ case @type.kind of
531531
| @stringliteraltype | @nilliteraltype;
532532

533533
@compositetype = @typeparamtype | @containertype | @structtype | @pointertype | @interfacetype | @tupletype
534-
| @signaturetype | @namedtype | @typesetliteraltype;
534+
| @signaturetype | @definedtype | @typesetliteraltype;
535535

536536
@containertype = @arraytype | @slicetype | @maptype | @chantype;
537537

go/ql/lib/go.dbscheme.stats

+1-1
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@
681681
<v>101202</v>
682682
</e>
683683
<e>
684-
<k>@namedtype</k>
684+
<k>@definedtype</k>
685685
<v>12852686</v>
686686
</e>
687687
<e>

go/ql/lib/semmle/go/Decls.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class MethodDecl extends FuncDecl {
212212
*
213213
* is `Rectangle`.
214214
*/
215-
NamedType getReceiverBaseType() { result = lookThroughPointerType(this.getReceiverType()) }
215+
DefinedType getReceiverBaseType() { result = lookThroughPointerType(this.getReceiverType()) }
216216

217217
/**
218218
* Gets the receiver variable of this method.

go/ql/lib/semmle/go/Scopes.qll

+9-6
Original file line numberDiff line numberDiff line change
@@ -194,19 +194,22 @@ class BuiltinEntity extends Entity, @builtinobject { }
194194
/** An imported package. */
195195
class PackageEntity extends Entity, @pkgobject { }
196196

197-
/** A built-in or declared named type. */
197+
/**
198+
* A named type: predeclared types, defined types, type parameters, and type
199+
* aliases.
200+
*/
198201
class TypeEntity extends Entity, @typeobject { }
199202

200203
/** The parent of a type parameter type, either a declared type or a declared function. */
201204
class TypeParamParentEntity extends Entity, @typeparamparentobject { }
202205

203-
/** A declared named type. */
206+
/** A named type which has a declaration. */
204207
class DeclaredType extends TypeEntity, DeclaredEntity, TypeParamParentEntity, @decltypeobject {
205208
/** Gets the declaration specifier declaring this type. */
206209
TypeSpec getSpec() { result.getNameExpr() = this.getDeclaration() }
207210
}
208211

209-
/** A built-in named type. */
212+
/** A built-in type. */
210213
class BuiltinType extends TypeEntity, BuiltinEntity, @builtintypeobject { }
211214

212215
/** A built-in or declared constant, variable, field, method or function. */
@@ -522,7 +525,7 @@ class Method extends Function {
522525
Type getReceiverBaseType() { result = lookThroughPointerType(this.getReceiverType()) }
523526

524527
/** Holds if this method has name `m` and belongs to the method set of type `tp` or `*tp`. */
525-
private predicate isIn(NamedType tp, string m) {
528+
private predicate isIn(DefinedType tp, string m) {
526529
this = tp.getMethod(m) or
527530
this = tp.getPointerType().getMethod(m)
528531
}
@@ -536,7 +539,7 @@ class Method extends Function {
536539
* distinguishes between the method sets of `T` and `*T`, while the former does not.
537540
*/
538541
override predicate hasQualifiedName(string tp, string m) {
539-
exists(NamedType t |
542+
exists(DefinedType t |
540543
this.isIn(t, m) and
541544
tp = t.getQualifiedName()
542545
)
@@ -552,7 +555,7 @@ class Method extends Function {
552555
*/
553556
pragma[nomagic]
554557
predicate hasQualifiedName(string pkg, string tp, string m) {
555-
exists(NamedType t |
558+
exists(DefinedType t |
556559
this.isIn(t, m) and
557560
t.hasQualifiedName(pkg, tp)
558561
)

go/ql/lib/semmle/go/Types.qll

+12-9
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ class Type extends @type {
2626
/**
2727
* Gets the qualified name of this type, if any.
2828
*
29-
* Only (defined) named types like `io.Writer` have a qualified name. Basic types like `int`,
29+
* Only defined types like `io.Writer` have a qualified name. Basic types like `int`,
3030
* pointer types like `*io.Writer`, and other composite types do not have a qualified name.
3131
*/
3232
string getQualifiedName() { result = this.getEntity().getQualifiedName() }
3333

3434
/**
3535
* Holds if this type is declared in a package with path `pkg` and has name `name`.
3636
*
37-
* Only (defined) named types like `io.Writer` have a qualified name. Basic types like `int`,
37+
* Only defined types like `io.Writer` have a qualified name. Basic types like `int`,
3838
* pointer types like `*io.Writer`, and other composite types do not have a qualified name.
3939
*/
4040
predicate hasQualifiedName(string pkg, string name) {
@@ -50,7 +50,7 @@ class Type extends @type {
5050
* Gets the method `m` belonging to the method set of this type, if any.
5151
*
5252
* Note that this predicate never has a result for struct types. Methods are associated
53-
* with the corresponding named type instead.
53+
* with the corresponding defined type instead.
5454
*/
5555
Method getMethod(string m) {
5656
result.getReceiverType() = this and
@@ -446,7 +446,7 @@ class StructType extends @structtype, CompositeType {
446446
if n = ""
447447
then (
448448
isEmbedded = true and
449-
name = lookThroughPointerType(tp).(NamedType).getName()
449+
name = lookThroughPointerType(tp).(DefinedType).getName()
450450
) else (
451451
isEmbedded = false and
452452
name = n
@@ -497,7 +497,7 @@ class StructType extends @structtype, CompositeType {
497497
// embeddedParent is a field of 'this' at depth 'depth - 1'
498498
this.hasFieldCand(_, embeddedParent, depth - 1, true) and
499499
// embeddedParent's type has the result field. Note that it is invalid Go
500-
// to have an embedded field with a named type whose underlying type is a
500+
// to have an embedded field with a defined type whose underlying type is a
501501
// pointer, so we don't have to have
502502
// `lookThroughPointerType(embeddedParent.getType().getUnderlyingType())`.
503503
result =
@@ -613,7 +613,7 @@ class PointerType extends @pointertype, CompositeType {
613613
or
614614
// promoted methods from embedded types
615615
exists(StructType s, Type embedded |
616-
s = this.getBaseType().(NamedType).getUnderlyingType() and
616+
s = this.getBaseType().(DefinedType).getUnderlyingType() and
617617
s.hasOwnField(_, _, embedded, true) and
618618
// ensure that `m` can be promoted
619619
not s.hasOwnField(_, m, _, _) and
@@ -918,7 +918,7 @@ class EmptyInterfaceType extends BasicInterfaceType {
918918
/**
919919
* The predeclared `comparable` type.
920920
*/
921-
class ComparableType extends NamedType {
921+
class ComparableType extends DefinedType {
922922
ComparableType() { this.getName() = "comparable" }
923923
}
924924

@@ -1028,8 +1028,11 @@ class SendRecvChanType extends @sendrcvchantype, ChanType {
10281028
override string toString() { result = "send-receive-channel type" }
10291029
}
10301030

1031-
/** A named type. */
1032-
class NamedType extends @namedtype, CompositeType {
1031+
/** DEPRECATED: Use `DefinedType` instead. */
1032+
deprecated class NamedType = DefinedType;
1033+
1034+
/** A defined type. */
1035+
class DefinedType extends @definedtype, CompositeType {
10331036
/** Gets the type which this type is defined to be. */
10341037
Type getBaseType() { underlying_type(this, result) }
10351038

0 commit comments

Comments
 (0)