protected override void VisitModuleSyntax(ModuleSyntax pNode) { //Global scope _locals.AddScope(); foreach (var f in pNode.Fields) { Visit(f); } //Infer methods foreach (var m in pNode.Methods) { Visit(m); } foreach (var s in pNode.Structs) { Visit(s.DeclaredType); Visit(s.AppliesTo); _unit.FromString(s.GetApplicableType(), out SmallType type); using (var st = Store.AddValue("__Struct", type)) { foreach (var m in s.Methods) { Visit(m); } } } _locals.RemoveScope(); }
private bool IsConstant(string pNamespace, SyntaxNode pNode) { if (pNode.SyntaxType == SyntaxType.BooleanLiteral || pNode.SyntaxType == SyntaxType.NumericLiteral || pNode.SyntaxType == SyntaxType.StringLiteral) { return(true); } else if (pNode.SyntaxType == SyntaxType.MemberAccess) { var ma = (MemberAccessSyntax)pNode; if (ma.Value.SyntaxType == SyntaxType.MemberAccess) { return(IsConstant(ma.Identifier.Value, ma.Value)); } if (_cache.IsTypeDefined(pNamespace, ma.Identifier.Value) && _cache.FromString(pNamespace, ma.Identifier.Value, out SmallType t) == Compiler.FindResult.Found) { return(t.IsEnum); } } return(false); }
protected override void VisitModuleSyntax(ModuleSyntax pNode) { ////// ////// Discover enums ////// Add enums first since they can't reference anything, but things can reference enums ////// foreach (var e in pNode.Enums) { _unit.AddType(e); } //Build our list for discovering types for (int i = 0; i < pNode.Structs.Count; i++) { var s = pNode.Structs[i]; if (s.DefinitionType == DefinitionTypes.Implement) { var applies = SyntaxHelper.GetFullTypeName(s.AppliesTo); if (!_implements.ContainsKey(applies)) { _implements.Add(applies, new List <TypeDefinitionSyntax>()); } _implements[applies].Add(s); } else { _discoveryGraph.AddNode(s); } } ///// ///// Discover all other types ///// var nodes = _discoveryGraph.GetNodes(); for (int i = 0; i < nodes.Count; i++) { var t = SyntaxHelper.GetFullTypeName(nodes[i].Node.DeclaredType); if (!nodes[i].Permanent && !nodes[i].Temporary && DiscoverTypes(t, nodes[i].Node.Span)) { //If we discover a type go back to the beginning to see if any that were dependent //on this can now be typed i = -1; } } foreach (var i in _implements) { foreach (var s in i.Value) { //Validate that the namespace and type exist var applyName = SyntaxHelper.GetFullTypeName(s.AppliesTo); var name = SyntaxHelper.GetFullTypeName(s.DeclaredType); //Mark any traits for types if (ValidateType(s.AppliesTo.Namespace, applyName, s) && ValidateType(s.DeclaredType.Namespace, name, s)) { var result = _unit.FromString(s.DeclaredType, out SmallType traitType); System.Diagnostics.Debug.Assert(result == Compiler.FindResult.Found); result = _unit.FromString(s.AppliesTo, out SmallType applyType); System.Diagnostics.Debug.Assert(result == Compiler.FindResult.Found); applyType.AddTrait(traitType); TypeDefinitionSyntax trait; if (s.DeclaredType.Namespace == null) { trait = _discoveryGraph.GetNode(name).Node; } else { //TODO clean this up trait = _unit.GetReference(s.DeclaredType.Namespace.Value).Module.Structs.SingleOrDefault((pt) => pt.DeclaredType.Value == s.DeclaredType.Value); } ValidateImplementation(trait, s); } } } // // Add all methods to the MethodCache // for (int j = 0; j < pNode.Methods.Count; j++) { AddMethodToCache(null, pNode.Methods[j], out MethodDefinition m); } // // Add struct methods to MethodCache // foreach (var s in pNode.Structs) { var result = _unit.FromString(s.GetApplicableType(), out SmallType type); //This can occur if we are specifying an undeclared type //or a type imported from another module // - The namespace might not be specified // - The type might not be exported switch (result) { case Compiler.FindResult.IncorrectScope: CompilerErrors.TypeNotInScope(s.GetApplicableType().ToString(), s.Span); break; case Compiler.FindResult.NotFound: CompilerErrors.UndeclaredType(s.GetApplicableType().ToString(), s.Span); break; } //Add each method to the cache and set type constructors if necessary for (int j = 0; j < s.Methods.Count; j++) { if (AddMethodToCache(type, s.Methods[j], out MethodDefinition m) && s.Methods[j].Annotation.Value == KeyAnnotations.Constructor) { type.SetConstructor(m); } } if (!type.HasDefinedConstructor()) { type.SetDefaultConstructor(); } } }