本文整理汇总了Golang中github.com/serulian/compiler/graphs/typegraph.TypeGraph.AnyTypeReference方法的典型用法代码示例。如果您正苦于以下问题:Golang TypeGraph.AnyTypeReference方法的具体用法?Golang TypeGraph.AnyTypeReference怎么用?Golang TypeGraph.AnyTypeReference使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/serulian/compiler/graphs/typegraph.TypeGraph
的用法示例。
在下文中一共展示了TypeGraph.AnyTypeReference方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: resolvePossibleType
// resolvePossibleType calls the specified type getter function and, if found, attempts to resolve it.
// Returns a reference to the resolved type or Any if the getter returns false.
func (stc *srgTypeConstructor) resolvePossibleType(sourceNode compilergraph.GraphNode, getter typeGetter, tdg *typegraph.TypeGraph, reporter typegraph.IssueReporter) (typegraph.TypeReference, bool) {
srgTypeRef, found := getter()
if !found {
return tdg.AnyTypeReference(), true
}
resolvedTypeRef, err := stc.BuildTypeRef(srgTypeRef, tdg)
if err != nil {
reporter.ReportError(sourceNode, "%s", err.Error())
return tdg.AnyTypeReference(), false
}
return resolvedTypeRef, true
}
示例2: ResolveType
// ResolveType attempts to resolve the given type string.
func (itc *irgTypeConstructor) ResolveType(typeString string, graph *typegraph.TypeGraph) (typegraph.TypeReference, error) {
if typeString == "any" {
return graph.AnyTypeReference(), nil
}
if typeString == "void" {
return graph.VoidTypeReference(), nil
}
var nullable = false
if strings.HasSuffix(typeString, "?") {
nullable = true
typeString = typeString[0 : len(typeString)-1]
}
// Perform native type mapping.
if found, ok := NATIVE_TYPES[typeString]; ok {
typeString = found
}
declaration, hasDeclaration := itc.irg.FindDeclaration(typeString)
if !hasDeclaration {
return graph.AnyTypeReference(), fmt.Errorf("Could not find WebIDL type %v", typeString)
}
typeDecl, hasType := graph.GetTypeForSourceNode(declaration.GraphNode)
if !hasType {
panic("Type not found for WebIDL type declaration")
}
typeRef := typeDecl.GetTypeReference()
if nullable {
return typeRef.AsNullable(), nil
}
return typeRef, nil
}
示例3: decorateMember
// decorateMember decorates a single type member.
func (stc *srgTypeConstructor) decorateMember(member srg.SRGMember, parent typegraph.TGTypeOrModule, decorator *typegraph.MemberDecorator, reporter typegraph.IssueReporter, graph *typegraph.TypeGraph) {
// Add the generic's constraints.
for _, generic := range member.Generics() {
// Note: If the constraint is not valid, the resolve method will report the error and return Any, which is the correct type.
constraintType, _ := stc.resolvePossibleType(generic.Node(), generic.GetConstraint, graph, reporter)
decorator.DefineGenericConstraint(generic.Node(), constraintType)
}
// Build all member-specific information.
var memberType typegraph.TypeReference = graph.AnyTypeReference()
var memberKind typegraph.MemberSignatureKind = typegraph.CustomMemberSignature
var isReadOnly bool = true
var isStatic bool = false
var isPromising bool = true
var isImplicitlyCalled bool = false
var hasDefaultValue bool = false
var isField = false
switch member.MemberKind() {
case srg.VarMember:
// Variables have their declared type.
memberType, _ = stc.resolvePossibleType(member.Node(), member.DeclaredType, graph, reporter)
memberKind = typegraph.FieldMemberSignature
isReadOnly = false
isPromising = false
isField = true
_, hasDefaultValue = member.Node().TryGetNode(parser.NodePredicateTypeFieldDefaultValue)
case srg.PropertyMember:
// Properties have their declared type.
memberType, _ = stc.resolvePossibleType(member.Node(), member.DeclaredType, graph, reporter)
memberKind = typegraph.PropertyMemberSignature
isReadOnly = member.IsReadOnly()
isImplicitlyCalled = true
// Decorate the property *getter* with its return type.
getter, found := member.Getter()
if found {
decorator.CreateReturnable(getter.GraphNode, memberType)
}
case srg.ConstructorMember:
memberKind = typegraph.ConstructorMemberSignature
// Constructors are static.
isStatic = true
// Constructors have a type of a function that returns an instance of the parent type.
returnType := graph.NewInstanceTypeReference(parent.(typegraph.TGTypeDecl))
functionType := graph.NewTypeReference(graph.FunctionType(), returnType)
memberType, _ = stc.addSRGParameterTypes(member, functionType, graph, reporter)
// Decorate the constructor with its return type.
decorator.CreateReturnable(member.Node(), returnType)
// Constructors have custom signature types that return 'any' to allow them to match
// interfaces.
var signatureType = graph.FunctionTypeReference(graph.AnyTypeReference())
signatureType, _ = stc.addSRGParameterTypes(member, signatureType, graph, reporter)
decorator.SignatureType(signatureType)
case srg.OperatorMember:
memberKind = typegraph.OperatorMemberSignature
// Operators are read-only.
isReadOnly = true
// Operators have type function<DeclaredType>(parameters).
returnType, _ := stc.resolvePossibleType(member.Node(), member.DeclaredType, graph, reporter)
functionType := graph.NewTypeReference(graph.FunctionType(), returnType)
memberType, _ = stc.addSRGParameterTypes(member, functionType, graph, reporter)
// Make sure instance members under interfaces do not have bodies (and static members do).
if parent.IsType() {
parentType := parent.AsType()
if parentType.TypeKind() == typegraph.ImplicitInterfaceType {
opDef, found := graph.GetOperatorDefinition(member.Name())
// Note: If not found, the type graph will emit an error.
if found {
if member.HasImplementation() != opDef.IsStatic {
if opDef.IsStatic {
reporter.ReportError(member.GraphNode, "Static operator %v under %v %v must have an implementation", member.Name(), parentType.Title(), parentType.Name())
} else {
reporter.ReportError(member.GraphNode, "Instance operator %v under %v %v cannot have an implementation", member.Name(), parentType.Title(), parentType.Name())
}
}
}
}
}
// Note: Operators get decorated with a returnable by the construction system automatically.
case srg.FunctionMember:
memberKind = typegraph.FunctionMemberSignature
//.........这里部分代码省略.........
示例4: resolveTypeRef
func (trr *TypeReferenceResolver) resolveTypeRef(typeref srg.SRGTypeRef, tdg *typegraph.TypeGraph) (typegraph.TypeReference, error) {
switch typeref.RefKind() {
case srg.TypeRefVoid:
return tdg.VoidTypeReference(), nil
case srg.TypeRefAny:
return tdg.AnyTypeReference(), nil
case srg.TypeRefStruct:
return tdg.StructTypeReference(), nil
case srg.TypeRefMapping:
innerType, err := trr.ResolveTypeRef(typeref.InnerReference(), tdg)
if err != nil {
return tdg.AnyTypeReference(), err
}
return tdg.NewTypeReference(tdg.MappingType(), innerType), nil
case srg.TypeRefSlice:
innerType, err := trr.ResolveTypeRef(typeref.InnerReference(), tdg)
if err != nil {
return tdg.AnyTypeReference(), err
}
return tdg.NewTypeReference(tdg.SliceType(), innerType), nil
case srg.TypeRefStream:
innerType, err := trr.ResolveTypeRef(typeref.InnerReference(), tdg)
if err != nil {
return tdg.AnyTypeReference(), err
}
return tdg.NewTypeReference(tdg.StreamType(), innerType), nil
case srg.TypeRefNullable:
innerType, err := trr.ResolveTypeRef(typeref.InnerReference(), tdg)
if err != nil {
return tdg.AnyTypeReference(), err
}
return innerType.AsNullable(), nil
case srg.TypeRefPath:
// Resolve the package type for the type ref.
resolvedTypeInfo, found := typeref.ResolveType()
if !found {
sourceError := compilercommon.SourceErrorf(typeref.Location(),
"Type '%s' could not be found",
typeref.ResolutionPath())
return tdg.AnyTypeReference(), sourceError
}
// If the type information refers to an SRG type or generic, find the node directly
// in the type graph.
var constructedRef = tdg.AnyTypeReference()
if !resolvedTypeInfo.IsExternalPackage {
// Get the type in the type graph.
resolvedType, hasResolvedType := tdg.GetTypeForSourceNode(resolvedTypeInfo.ResolvedType.Node())
if !hasResolvedType {
panic(fmt.Sprintf("Could not find typegraph type for SRG type %v", resolvedTypeInfo.ResolvedType.Name()))
}
constructedRef = tdg.NewTypeReference(resolvedType)
} else {
// Otherwise, we search for the type in the type graph based on the package from which it
// was imported.
resolvedType, hasResolvedType := tdg.ResolveTypeUnderPackage(resolvedTypeInfo.ExternalPackageTypePath, resolvedTypeInfo.ExternalPackage)
if !hasResolvedType {
sourceError := compilercommon.SourceErrorf(typeref.Location(),
"Type '%s' could not be found",
typeref.ResolutionPath())
return tdg.AnyTypeReference(), sourceError
}
constructedRef = tdg.NewTypeReference(resolvedType)
}
// Add the generics.
if typeref.HasGenerics() {
for _, srgGeneric := range typeref.Generics() {
genericTypeRef, err := trr.ResolveTypeRef(srgGeneric, tdg)
if err != nil {
return tdg.AnyTypeReference(), err
}
constructedRef = constructedRef.WithGeneric(genericTypeRef)
}
}
// Add the parameters.
if typeref.HasParameters() {
for _, srgParameter := range typeref.Parameters() {
parameterTypeRef, err := trr.ResolveTypeRef(srgParameter, tdg)
if err != nil {
return tdg.AnyTypeReference(), err
}
constructedRef = constructedRef.WithParameter(parameterTypeRef)
//.........这里部分代码省略.........