Esempio n. 1
0
func (itc *irgTypeConstructor) DefineDependencies(annotator typegraph.Annotator, graph *typegraph.TypeGraph) {
	for _, module := range itc.irg.GetModules() {
		for _, declaration := range module.Declarations() {
			if declaration.HasOneAnnotation(GLOBAL_CONTEXT_ANNOTATIONS...) {
				continue
			}

			// Ensure that we don't have duplicate types across modules. Intra-module is handled by the type
			// graph.
			existingModule, found := itc.typesEncountered.Get(declaration.Name())
			if found && existingModule != module {
				annotator.ReportError(declaration.GraphNode, "Redeclaration of WebIDL interface %v is not supported", declaration.Name())
			}

			itc.typesEncountered.Set(declaration.Name(), module)

			// Determine whether we have a parent type for inheritance.
			parentTypeString, hasParentType := declaration.ParentType()
			if !hasParentType {
				continue
			}

			parentType, err := itc.ResolveType(parentTypeString, graph)
			if err != nil {
				annotator.ReportError(declaration.GraphNode, "%v", err)
				continue
			}

			annotator.DefineParentType(declaration.GraphNode, parentType)
		}
	}
}
Esempio n. 2
0
func (stc *srgTypeConstructor) DefineDependencies(annotator typegraph.Annotator, graph *typegraph.TypeGraph) {
	for _, srgType := range stc.srg.GetTypes() {
		// Decorate all types with their inheritance.
		if srgType.TypeKind() != srg.InterfaceType {
			for _, inheritsRef := range srgType.Inheritance() {
				// Resolve the type to which the inherits points.
				resolvedType, err := stc.BuildTypeRef(inheritsRef, graph)
				if err != nil {
					annotator.ReportError(srgType.Node(), "%s", err.Error())
					continue
				}

				if srgType.TypeKind() == srg.ClassType {
					if resolvedType.ReferredType().TypeKind() != typegraph.ClassType {
						switch resolvedType.ReferredType().TypeKind() {
						case typegraph.GenericType:
							annotator.ReportError(srgType.Node(), "Type '%s' cannot derive from a generic ('%s')", srgType.Name(), resolvedType.ReferredType().Name())

						case typegraph.ImplicitInterfaceType:
							annotator.ReportError(srgType.Node(), "Type '%s' cannot derive from an interface ('%s')", srgType.Name(), resolvedType.ReferredType().Name())
						}

						continue
					}

					annotator.DefineParentType(srgType.Node(), resolvedType)
				} else if srgType.TypeKind() == srg.NominalType {
					annotator.DefineParentType(srgType.Node(), resolvedType)
				}
			}
		}

		// Decorate all type generics with their constraints.
		for _, srgGeneric := range srgType.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(srgGeneric.Node(), srgGeneric.GetConstraint, graph, annotator)

			// If the constraint type is `any` and this is a generic on a struct, then
			// change it to a structural any reference.
			if srgType.TypeKind() == srg.StructType && constraintType.IsAny() {
				constraintType = graph.StructTypeReference()
			}

			annotator.DefineGenericConstraint(srgGeneric.Node(), constraintType)
		}
	}
}