Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go: Rename "named type" to "defined type" #18489

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions go/extractor/dbscheme/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,13 +694,13 @@ var BuiltinObjectType = NewUnionType("@builtinobject")
// PkgObjectType is the type of imported packages
var PkgObjectType = ObjectKind.NewBranch("@pkgobject")

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

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

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

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

// NamedType is the type of named types
var NamedType = TypeKind.NewBranch("@namedtype", CompositeType)
// DefinedType is the type of defined types
var DefinedType = TypeKind.NewBranch("@namedtype", CompositeType)

// TypeSetLiteral is the type of type set literals
var TypeSetLiteral = TypeKind.NewBranch("@typesetliteraltype", CompositeType)
Expand Down Expand Up @@ -1080,10 +1080,10 @@ var FieldStructsTable = NewTable("fieldstructs",
EntityColumn(StructType, "struct"),
)

// MethodHostsTable maps interface methods to the named type they belong to
// MethodHostsTable maps interface methods to the defined type they belong to
var MethodHostsTable = NewTable("methodhosts",
EntityColumn(ObjectType, "method"),
EntityColumn(NamedType, "host"),
EntityColumn(DefinedType, "host"),
)

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

// TypeNameTable is the table associating named types with their names
// TypeNameTable is the table associating defined types with their names
var TypeNameTable = NewTable("typename",
EntityColumn(TypeType, "tp").Unique(),
StringColumn("name"),
Expand All @@ -1135,10 +1135,10 @@ var BaseTypeTable = NewTable("base_type",
EntityColumn(TypeType, "tp"),
)

// UnderlyingTypeTable is the table associating named types with their
// UnderlyingTypeTable is the table associating defined types with their
// underlying type
var UnderlyingTypeTable = NewTable("underlying_type",
EntityColumn(NamedType, "named").Unique(),
EntityColumn(DefinedType, "named").Unique(),
EntityColumn(TypeType, "tp"),
)

Expand Down
24 changes: 12 additions & 12 deletions go/extractor/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ func extractObjects(tw *trap.Writer, scope *types.Scope, scopeLabel trap.Label)
populateTypeParamParents(funcObj.Type().(*types.Signature).TypeParams(), obj)
populateTypeParamParents(funcObj.Type().(*types.Signature).RecvTypeParams(), obj)
}
// Populate type parameter parents for named types. Note that we
// Populate type parameter parents for defined types. Note that we
// skip type aliases as the original type should be the parent
// of any type parameters.
if typeNameObj, ok := obj.(*types.TypeName); ok && !typeNameObj.IsAlias() {
Expand Down Expand Up @@ -568,7 +568,7 @@ func extractObject(tw *trap.Writer, obj types.Object, lbl trap.Label) {
// For more information on objects, see:
// https://github.com/golang/example/blob/master/gotypes/README.md#objects
func extractObjectTypes(tw *trap.Writer) {
// calling `extractType` on a named type will extract all methods defined
// calling `extractType` on a defined type will extract all methods defined
// on it, which will add new objects. Therefore we need to do this first
// before we loop over all objects and emit them.
changed := true
Expand Down Expand Up @@ -1689,7 +1689,7 @@ func extractType(tw *trap.Writer, tp types.Type) trap.Label {
extractElementType(tw, lbl, tp.Elem())
case *types.Named:
origintp := tp.Origin()
kind = dbscheme.NamedType.Index()
kind = dbscheme.DefinedType.Index()
dbscheme.TypeNameTable.Emit(tw, lbl, origintp.Obj().Name())
underlying := origintp.Underlying()
extractUnderlyingType(tw, lbl, underlying)
Expand Down Expand Up @@ -1761,9 +1761,9 @@ func extractType(tw *trap.Writer, tp types.Type) trap.Label {
// Type labels refer to global keys to ensure that if the same type is
// encountered during the extraction of different files it is still ultimately
// mapped to the same entity. In particular, this means that keys for compound
// types refer to the labels of their component types. For named types, the key
// types refer to the labels of their component types. For defined types, the key
// is constructed from their globally unique ID. This prevents cyclic type keys
// since type recursion in Go always goes through named types.
// since type recursion in Go always goes through defined types.
func getTypeLabel(tw *trap.Writer, tp types.Type) (trap.Label, bool) {
tp = resolveTypeAlias(tp)
lbl, exists := tw.Labeler.TypeLabels[tp]
Expand Down Expand Up @@ -1868,12 +1868,12 @@ func getTypeLabel(tw *trap.Writer, tp types.Type) (trap.Label, bool) {
origintp := tp.Origin()
entitylbl, exists := tw.Labeler.LookupObjectID(origintp.Obj(), lbl)
if entitylbl == trap.InvalidLabel {
panic(fmt.Sprintf("Cannot construct label for named type %v (underlying object is %v).\n", origintp, origintp.Obj()))
panic(fmt.Sprintf("Cannot construct label for defined type %v (underlying object is %v).\n", origintp, origintp.Obj()))
}
if !exists {
extractObject(tw, origintp.Obj(), entitylbl)
}
lbl = tw.Labeler.GlobalID(fmt.Sprintf("{%s};namedtype", entitylbl))
lbl = tw.Labeler.GlobalID(fmt.Sprintf("{%s};definedtype", entitylbl))
case *types.TypeParam:
parentlbl := getTypeParamParentLabel(tw, tp)
idx := tp.Index()
Expand Down Expand Up @@ -1915,9 +1915,9 @@ func extractBaseType(tw *trap.Writer, ptr trap.Label, base types.Type) {
}

// extractUnderlyingType extracts `underlying` as the underlying type of the
// named type `named`
func extractUnderlyingType(tw *trap.Writer, named trap.Label, underlying types.Type) {
dbscheme.UnderlyingTypeTable.Emit(tw, named, extractType(tw, underlying))
// defined type `defined`
func extractUnderlyingType(tw *trap.Writer, defined trap.Label, underlying types.Type) {
dbscheme.UnderlyingTypeTable.Emit(tw, defined, extractType(tw, underlying))
}

// extractComponentType extracts `component` as the `idx`th component type of `parent` with name `name`
Expand Down Expand Up @@ -2167,8 +2167,8 @@ func checkObjectNotSpecialized(obj types.Object) {
log.Fatalf("Encountered unexpected specialization %s of generic variable object %s", varObj.String(), varObj.Origin().String())
}
if typeNameObj, ok := obj.(*types.TypeName); ok {
if namedType, ok := typeNameObj.Type().(*types.Named); ok && namedType != namedType.Origin() {
log.Fatalf("Encountered type object for specialization %s of named type %s", namedType.String(), namedType.Origin().String())
if definedType, ok := typeNameObj.Type().(*types.Named); ok && definedType != definedType.Origin() {
log.Fatalf("Encountered type object for specialization %s of defined type %s", definedType.String(), definedType.Origin().String())
}
}
}
6 changes: 3 additions & 3 deletions go/extractor/trap/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ func findMethodWithGivenReceiver(object types.Object) *types.Func {

// findMethodWithGivenReceiver finds a method on type `tp` with `object` as its receiver, if one exists
func findMethodOnTypeWithGivenReceiver(tp types.Type, object types.Object) *types.Func {
if namedType, ok := tp.(*types.Named); ok {
for i := 0; i < namedType.NumMethods(); i++ {
meth := namedType.Method(i)
if definedType, ok := tp.(*types.Named); ok {
for i := 0; i < definedType.NumMethods(); i++ {
meth := definedType.Method(i)
if object == meth.Type().(*types.Signature).Recv() {
return meth
}
Expand Down
2 changes: 1 addition & 1 deletion go/ql/examples/snippets/incompleteswitchoverenum.ql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import go

from ExpressionSwitchStmt ss, DeclaredConstant c, NamedType t
from ExpressionSwitchStmt ss, DeclaredConstant c, DefinedType t
where
t.getUnderlyingType() instanceof IntegerType and
t = ss.getExpr().getType() and
Expand Down
2 changes: 1 addition & 1 deletion go/ql/lib/semmle/go/Decls.qll
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class MethodDecl extends FuncDecl {
*
* is `Rectangle`.
*/
NamedType getReceiverBaseType() { result = lookThroughPointerType(this.getReceiverType()) }
DefinedType getReceiverBaseType() { result = lookThroughPointerType(this.getReceiverType()) }

/**
* Gets the receiver variable of this method.
Expand Down
15 changes: 9 additions & 6 deletions go/ql/lib/semmle/go/Scopes.qll
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,22 @@ class BuiltinEntity extends Entity, @builtinobject { }
/** An imported package. */
class PackageEntity extends Entity, @pkgobject { }

/** A built-in or declared named type. */
/**
* A named type (predeclared types, defined types, type parameters and aliases
* which refer to those things).
*/
class TypeEntity extends Entity, @typeobject { }

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

/** A declared named type. */
/** A named type which has a declaration. */
class DeclaredType extends TypeEntity, DeclaredEntity, TypeParamParentEntity, @decltypeobject {
/** Gets the declaration specifier declaring this type. */
TypeSpec getSpec() { result.getNameExpr() = this.getDeclaration() }
}

/** A built-in named type. */
/** A built-in type. */
class BuiltinType extends TypeEntity, BuiltinEntity, @builtintypeobject { }

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

/** Holds if this method has name `m` and belongs to the method set of type `tp` or `*tp`. */
private predicate isIn(NamedType tp, string m) {
private predicate isIn(DefinedType tp, string m) {
this = tp.getMethod(m) or
this = tp.getPointerType().getMethod(m)
}
Expand All @@ -536,7 +539,7 @@ class Method extends Function {
* distinguishes between the method sets of `T` and `*T`, while the former does not.
*/
override predicate hasQualifiedName(string tp, string m) {
exists(NamedType t |
exists(DefinedType t |
this.isIn(t, m) and
tp = t.getQualifiedName()
)
Expand All @@ -552,7 +555,7 @@ class Method extends Function {
*/
pragma[nomagic]
predicate hasQualifiedName(string pkg, string tp, string m) {
exists(NamedType t |
exists(DefinedType t |
this.isIn(t, m) and
t.hasQualifiedName(pkg, tp)
)
Expand Down
21 changes: 12 additions & 9 deletions go/ql/lib/semmle/go/Types.qll
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ class Type extends @type {
/**
* Gets the qualified name of this type, if any.
*
* Only (defined) named types like `io.Writer` have a qualified name. Basic types like `int`,
* Only defined types like `io.Writer` have a qualified name. Basic types like `int`,
* pointer types like `*io.Writer`, and other composite types do not have a qualified name.
*/
string getQualifiedName() { result = this.getEntity().getQualifiedName() }

/**
* Holds if this type is declared in a package with path `pkg` and has name `name`.
*
* Only (defined) named types like `io.Writer` have a qualified name. Basic types like `int`,
* Only defined types like `io.Writer` have a qualified name. Basic types like `int`,
* pointer types like `*io.Writer`, and other composite types do not have a qualified name.
*/
predicate hasQualifiedName(string pkg, string name) {
Expand All @@ -50,7 +50,7 @@ class Type extends @type {
* Gets the method `m` belonging to the method set of this type, if any.
*
* Note that this predicate never has a result for struct types. Methods are associated
* with the corresponding named type instead.
* with the corresponding defined type instead.
*/
Method getMethod(string m) {
result.getReceiverType() = this and
Expand Down Expand Up @@ -446,7 +446,7 @@ class StructType extends @structtype, CompositeType {
if n = ""
then (
isEmbedded = true and
name = lookThroughPointerType(tp).(NamedType).getName()
name = lookThroughPointerType(tp).(DefinedType).getName()
) else (
isEmbedded = false and
name = n
Expand Down Expand Up @@ -497,7 +497,7 @@ class StructType extends @structtype, CompositeType {
// embeddedParent is a field of 'this' at depth 'depth - 1'
this.hasFieldCand(_, embeddedParent, depth - 1, true) and
// embeddedParent's type has the result field. Note that it is invalid Go
// to have an embedded field with a named type whose underlying type is a
// to have an embedded field with a defined type whose underlying type is a
// pointer, so we don't have to have
// `lookThroughPointerType(embeddedParent.getType().getUnderlyingType())`.
result =
Expand Down Expand Up @@ -613,7 +613,7 @@ class PointerType extends @pointertype, CompositeType {
or
// promoted methods from embedded types
exists(StructType s, Type embedded |
s = this.getBaseType().(NamedType).getUnderlyingType() and
s = this.getBaseType().(DefinedType).getUnderlyingType() and
s.hasOwnField(_, _, embedded, true) and
// ensure that `m` can be promoted
not s.hasOwnField(_, m, _, _) and
Expand Down Expand Up @@ -918,7 +918,7 @@ class EmptyInterfaceType extends BasicInterfaceType {
/**
* The predeclared `comparable` type.
*/
class ComparableType extends NamedType {
class ComparableType extends DefinedType {
ComparableType() { this.getName() = "comparable" }
}

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

/** A named type. */
class NamedType extends @namedtype, CompositeType {
/** DEPRECATED: Use `DefinedType` instead. */
deprecated class NamedType = DefinedType;

/** A defined type. */
class DefinedType extends @namedtype, CompositeType {
/** Gets the type which this type is defined to be. */
Type getBaseType() { underlying_type(this, result) }

Expand Down
21 changes: 12 additions & 9 deletions go/ql/lib/semmle/go/frameworks/GoMicro.qll
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,31 @@ module GoMicro {
* A Server Interface type.
*/
class ServiceInterfaceType extends InterfaceType {
NamedType namedType;
DefinedType definedType;

ServiceInterfaceType() {
this = namedType.getUnderlyingType() and
namedType.hasLocationInfo(any(ProtocGeneratedFile f).getAbsolutePath(), _, _, _, _)
this = definedType.getUnderlyingType() and
definedType.hasLocationInfo(any(ProtocGeneratedFile f).getAbsolutePath(), _, _, _, _)
}

/**
* Gets the name of the interface.
*/
override string getName() { result = namedType.getName() }
override string getName() { result = definedType.getName() }

/** DEPRECATED: Use `getDefinedType` instead. */
deprecated DefinedType getNamedType() { result = definedType }

/**
* Gets the named type on top of this interface type.
* Gets the defined type on top of this interface type.
*/
NamedType getNamedType() { result = namedType }
DefinedType getDefinedType() { result = definedType }
}

/**
* A Service server handler type.
*/
class ServiceServerType extends NamedType {
class ServiceServerType extends DefinedType {
ServiceServerType() {
this.implements(any(ServiceInterfaceType i)) and
this.getName().regexpMatch("(?i).*Handler") and
Expand All @@ -79,7 +82,7 @@ module GoMicro {
/**
* A Client server handler type.
*/
class ClientServiceType extends NamedType {
class ClientServiceType extends DefinedType {
ClientServiceType() {
this.implements(any(ServiceInterfaceType i)) and
this.getName().regexpMatch("(?i).*Service") and
Expand All @@ -101,7 +104,7 @@ module GoMicro {
bindingset[m]
pragma[inline_late]
private predicate implementsServiceType(Method m) {
m.implements(any(ServiceInterfaceType i).getNamedType().getMethod(_))
m.implements(any(ServiceInterfaceType i).getDefinedType().getMethod(_))
}

/**
Expand Down
Loading