Skip to content

Commit b5b8857

Browse files
committed
Rename "named type" to "defined type"
1 parent 8833019 commit b5b8857

25 files changed

+96
-84
lines changed

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("@namedtype", 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, "named").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. Note that we
478+
// Populate type parameter parents for defined types. Note that we
479479
// skip type aliases as the original type should be the parent
480480
// of any type parameters.
481481
if typeNameObj, ok := obj.(*types.TypeName); ok && !typeNameObj.IsAlias() {
@@ -568,7 +568,7 @@ func extractObject(tw *trap.Writer, obj types.Object, lbl trap.Label) {
568568
// For more information on objects, see:
569569
// https://github.com/golang/example/blob/master/gotypes/README.md#objects
570570
func extractObjectTypes(tw *trap.Writer) {
571-
// calling `extractType` on a named type will extract all methods defined
571+
// calling `extractType` on a defined type will extract all methods defined
572572
// on it, which will add new objects. Therefore we need to do this first
573573
// before we loop over all objects and emit them.
574574
changed := true
@@ -1689,7 +1689,7 @@ func extractType(tw *trap.Writer, tp types.Type) trap.Label {
16891689
extractElementType(tw, lbl, tp.Elem())
16901690
case *types.Named:
16911691
origintp := tp.Origin()
1692-
kind = dbscheme.NamedType.Index()
1692+
kind = dbscheme.DefinedType.Index()
16931693
dbscheme.TypeNameTable.Emit(tw, lbl, origintp.Obj().Name())
16941694
underlying := origintp.Underlying()
16951695
extractUnderlyingType(tw, lbl, underlying)
@@ -1761,9 +1761,9 @@ func extractType(tw *trap.Writer, tp types.Type) trap.Label {
17611761
// Type labels refer to global keys to ensure that if the same type is
17621762
// encountered during the extraction of different files it is still ultimately
17631763
// mapped to the same entity. In particular, this means that keys for compound
1764-
// types refer to the labels of their component types. For named types, the key
1764+
// types refer to the labels of their component types. For defined types, the key
17651765
// is constructed from their globally unique ID. This prevents cyclic type keys
1766-
// since type recursion in Go always goes through named types.
1766+
// since type recursion in Go always goes through defined types.
17671767
func getTypeLabel(tw *trap.Writer, tp types.Type) (trap.Label, bool) {
17681768
tp = resolveTypeAlias(tp)
17691769
lbl, exists := tw.Labeler.TypeLabels[tp]
@@ -1868,12 +1868,12 @@ func getTypeLabel(tw *trap.Writer, tp types.Type) (trap.Label, bool) {
18681868
origintp := tp.Origin()
18691869
entitylbl, exists := tw.Labeler.LookupObjectID(origintp.Obj(), lbl)
18701870
if entitylbl == trap.InvalidLabel {
1871-
panic(fmt.Sprintf("Cannot construct label for named type %v (underlying object is %v).\n", origintp, origintp.Obj()))
1871+
panic(fmt.Sprintf("Cannot construct label for defined type %v (underlying object is %v).\n", origintp, origintp.Obj()))
18721872
}
18731873
if !exists {
18741874
extractObject(tw, origintp.Obj(), entitylbl)
18751875
}
1876-
lbl = tw.Labeler.GlobalID(fmt.Sprintf("{%s};namedtype", entitylbl))
1876+
lbl = tw.Labeler.GlobalID(fmt.Sprintf("{%s};definedtype", entitylbl))
18771877
case *types.TypeParam:
18781878
parentlbl := getTypeParamParentLabel(tw, tp)
18791879
idx := tp.Index()
@@ -1915,9 +1915,9 @@ func extractBaseType(tw *trap.Writer, ptr trap.Label, base types.Type) {
19151915
}
19161916

19171917
// extractUnderlyingType extracts `underlying` as the underlying type of the
1918-
// named type `named`
1919-
func extractUnderlyingType(tw *trap.Writer, named trap.Label, underlying types.Type) {
1920-
dbscheme.UnderlyingTypeTable.Emit(tw, named, extractType(tw, underlying))
1918+
// defined type `defined`
1919+
func extractUnderlyingType(tw *trap.Writer, defined trap.Label, underlying types.Type) {
1920+
dbscheme.UnderlyingTypeTable.Emit(tw, defined, extractType(tw, underlying))
19211921
}
19221922

19231923
// extractComponentType extracts `component` as the `idx`th component type of `parent` with name `name`
@@ -2167,8 +2167,8 @@ func checkObjectNotSpecialized(obj types.Object) {
21672167
log.Fatalf("Encountered unexpected specialization %s of generic variable object %s", varObj.String(), varObj.Origin().String())
21682168
}
21692169
if typeNameObj, ok := obj.(*types.TypeName); ok {
2170-
if namedType, ok := typeNameObj.Type().(*types.Named); ok && namedType != namedType.Origin() {
2171-
log.Fatalf("Encountered type object for specialization %s of named type %s", namedType.String(), namedType.Origin().String())
2170+
if definedType, ok := typeNameObj.Type().(*types.Named); ok && definedType != definedType.Origin() {
2171+
log.Fatalf("Encountered type object for specialization %s of defined type %s", definedType.String(), definedType.Origin().String())
21722172
}
21732173
}
21742174
}

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

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 aliases
199+
* which refer to those things).
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 @namedtype, CompositeType {
10331036
/** Gets the type which this type is defined to be. */
10341037
Type getBaseType() { underlying_type(this, result) }
10351038

go/ql/lib/semmle/go/frameworks/GoMicro.qll

+12-9
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,31 @@ module GoMicro {
4747
* A Server Interface type.
4848
*/
4949
class ServiceInterfaceType extends InterfaceType {
50-
NamedType namedType;
50+
DefinedType definedType;
5151

5252
ServiceInterfaceType() {
53-
this = namedType.getUnderlyingType() and
54-
namedType.hasLocationInfo(any(ProtocGeneratedFile f).getAbsolutePath(), _, _, _, _)
53+
this = definedType.getUnderlyingType() and
54+
definedType.hasLocationInfo(any(ProtocGeneratedFile f).getAbsolutePath(), _, _, _, _)
5555
}
5656

5757
/**
5858
* Gets the name of the interface.
5959
*/
60-
override string getName() { result = namedType.getName() }
60+
override string getName() { result = definedType.getName() }
61+
62+
/** DEPRECATED: Use `getDefinedType` instead. */
63+
deprecated DefinedType getNamedType() { result = definedType }
6164

6265
/**
63-
* Gets the named type on top of this interface type.
66+
* Gets the defined type on top of this interface type.
6467
*/
65-
NamedType getNamedType() { result = namedType }
68+
DefinedType getDefinedType() { result = definedType }
6669
}
6770

6871
/**
6972
* A Service server handler type.
7073
*/
71-
class ServiceServerType extends NamedType {
74+
class ServiceServerType extends DefinedType {
7275
ServiceServerType() {
7376
this.implements(any(ServiceInterfaceType i)) and
7477
this.getName().regexpMatch("(?i).*Handler") and
@@ -79,7 +82,7 @@ module GoMicro {
7982
/**
8083
* A Client server handler type.
8184
*/
82-
class ClientServiceType extends NamedType {
85+
class ClientServiceType extends DefinedType {
8386
ClientServiceType() {
8487
this.implements(any(ServiceInterfaceType i)) and
8588
this.getName().regexpMatch("(?i).*Service") and
@@ -101,7 +104,7 @@ module GoMicro {
101104
bindingset[m]
102105
pragma[inline_late]
103106
private predicate implementsServiceType(Method m) {
104-
m.implements(any(ServiceInterfaceType i).getNamedType().getMethod(_))
107+
m.implements(any(ServiceInterfaceType i).getDefinedType().getMethod(_))
105108
}
106109

107110
/**

0 commit comments

Comments
 (0)