|
| 1 | +using Nitra; |
| 2 | +using Nitra.BackEnd.Cci; |
| 3 | +using Nitra.Declarations; |
| 4 | +using Nitra.Internal; |
| 5 | +using Nitra.ProjectSystem; |
| 6 | + |
| 7 | +using DotNet; |
| 8 | + |
| 9 | +using Nemerle; |
| 10 | +using Nemerle.Collections; |
| 11 | +using Nemerle.Imperative; |
| 12 | +using Nemerle.Text; |
| 13 | +using Nemerle.Utility; |
| 14 | + |
| 15 | +using System; |
| 16 | +using System.Collections.Generic; |
| 17 | +using System.Collections.Immutable; |
| 18 | +using System.Linq; |
| 19 | + |
| 20 | +using System.Threading; |
| 21 | +using ND = Nitra.Declarations; |
| 22 | + |
| 23 | +[assembly: ProjectSupport("TdlLang", typeof(Tdl.ProjectSupport))] |
| 24 | + |
| 25 | +namespace Tdl |
| 26 | +{ |
| 27 | + public partial class ProjectSupport : IProjectSupport |
| 28 | + { |
| 29 | + static NoLocation : Location = Location(SourceSnapshot.Default.File, NSpan(0)); |
| 30 | + static NoFile : ProjectSystem.File = SourceSnapshot.Default.File; |
| 31 | + static NoSpan : NSpan = NSpan(0); |
| 32 | + |
| 33 | + public RefreshReferences(cancellationToken : CancellationToken, project : Project) : object |
| 34 | + { |
| 35 | + TypeUnifier.InitInstance(); |
| 36 | + |
| 37 | + def bindNs(scope : Scope, name : string) : Ref[NamespaceSymbol] |
| 38 | + { |
| 39 | + //assert2(false); |
| 40 | + def res = Reference(Location.Default, name).Bind.[NamespaceSymbol](scope); |
| 41 | + assert2(!res.IsUnresolved); |
| 42 | + assert2(!res.IsAmbiguous); |
| 43 | + assert2(res.Symbols.Length == 1); |
| 44 | + res |
| 45 | + } |
| 46 | + def bindType(scope : Scope, name : string) : Ref[TopGenericTypeSymbol] |
| 47 | + { |
| 48 | + //assert2(false); |
| 49 | + def res = Reference(Location.Default, name).Bind.[TopGenericTypeSymbol](scope); |
| 50 | + assert2(!res.IsUnresolved); |
| 51 | + assert2(!res.IsAmbiguous); |
| 52 | + assert2(res.Symbols.Length == 1); |
| 53 | + res |
| 54 | + } |
| 55 | + def defineAlias(context : DependentPropertyEvalContext, systemNs : NamespaceSymbol, declaredIn : NamespaceSymbol, name : string, aliasName : string) : void |
| 56 | + { |
| 57 | + def symbolRef = bindType(systemNs.Scope, name); |
| 58 | + unless (symbolRef.IsSymbolEvaluated) |
| 59 | + return; |
| 60 | + |
| 61 | + def symbol = symbolRef.Symbol; |
| 62 | + def decl = symbol.FirstDeclarationOrDefault :> IExternalTopTypeDeclaration; |
| 63 | + def aliasDecl = ExternalTopTypeDeclaration.[TypeAliasSymbol](ND.Name(Location.Default, aliasName), decl.Type); |
| 64 | + def alias = aliasDecl.DefineSymbol(declaredIn.MemberTable); |
| 65 | + alias.Replacement = symbolRef; |
| 66 | + alias.TypeParametersCount = 0; |
| 67 | + alias.TypeParameters = ImmutableArray.Empty; |
| 68 | + alias.DeclaredIn = declaredIn; |
| 69 | + alias.EvalProperties(context); |
| 70 | + } |
| 71 | + |
| 72 | + def backEnd = CciBackEnd(); |
| 73 | + backEnd.CancellationToken = cancellationToken; |
| 74 | + def context = DependentPropertyEvalContext(project); |
| 75 | + def rootNamespace = backEnd.LoadExternalSymbols(project.Libs, project.ProjectDir, project.CompilerMessages, context); |
| 76 | + def systemNsRef = bindNs(rootNamespace.Scope, "System"); |
| 77 | + when (systemNsRef.IsSymbolEvaluated) |
| 78 | + { |
| 79 | + def systemNs = systemNsRef.Symbol; |
| 80 | + |
| 81 | + defineAlias(context, systemNs, rootNamespace, "Object", "object"); |
| 82 | + defineAlias(context, systemNs, rootNamespace, "Void", "void"); |
| 83 | + defineAlias(context, systemNs, rootNamespace, "String", "string"); |
| 84 | + defineAlias(context, systemNs, rootNamespace, "Boolean", "bool"); |
| 85 | + defineAlias(context, systemNs, rootNamespace, "Byte", "byte"); |
| 86 | + defineAlias(context, systemNs, rootNamespace, "SByte", "sbyte"); |
| 87 | + defineAlias(context, systemNs, rootNamespace, "Int16", "short"); |
| 88 | + defineAlias(context, systemNs, rootNamespace, "UInt16", "ushort"); |
| 89 | + defineAlias(context, systemNs, rootNamespace, "Int32", "int"); |
| 90 | + defineAlias(context, systemNs, rootNamespace, "UInt32", "uint"); |
| 91 | + defineAlias(context, systemNs, rootNamespace, "Int64", "long"); |
| 92 | + defineAlias(context, systemNs, rootNamespace, "UInt64", "ulong"); |
| 93 | + defineAlias(context, systemNs, rootNamespace, "Single", "float"); |
| 94 | + defineAlias(context, systemNs, rootNamespace, "Double", "double"); |
| 95 | + defineAlias(context, systemNs, rootNamespace, "Decimal", "decimal"); |
| 96 | + defineAlias(context, systemNs, rootNamespace, "Char", "char"); |
| 97 | + } |
| 98 | + |
| 99 | + (backEnd : IDotNetBackEnd, context, rootNamespace) } |
| 100 | + |
| 101 | + public RefreshProject(cancellationToken : CancellationToken, files : ImmutableArray[FileEvalPropertiesData], data : object) : void |
| 102 | + { |
| 103 | + def (backEnd, context, rootNamespace) = (data :> IDotNetBackEnd * DependentPropertyEvalContext * NamespaceSymbol); |
| 104 | + context.CancellationToken = cancellationToken; |
| 105 | + backEnd.CancellationToken = cancellationToken; |
| 106 | + |
| 107 | + def removeParsedSymbols(tableScope : TableScope) |
| 108 | + { |
| 109 | + tableScope.Undefine(_.IsParsed); |
| 110 | + foreach (symbols in tableScope.Symbols) |
| 111 | + foreach (symbol is NamespaceSymbol in symbols) |
| 112 | + removeParsedSymbols(symbol.MemberTable); |
| 113 | + } |
| 114 | + removeParsedSymbols(rootNamespace.MemberTable); |
| 115 | + |
| 116 | + when (cancellationToken.IsCancellationRequested) |
| 117 | + return; |
| 118 | + |
| 119 | + def evalHost = CSharp.CSharpProjectEvalPropertiesHost(files, rootNamespace); |
| 120 | + evalHost.EvalProperties(context, "SymbolHierarchy", 0); |
| 121 | + evalHost.EvalProperties(context, "Scopes", 1); |
| 122 | + evalHost.EvalProperties(context, "Type bodies binding", 2); |
| 123 | + } |
| 124 | + |
| 125 | + public GetSymbolById(data : object, symbolId : int) : ValueOption[DeclarationSymbol] |
| 126 | + { |
| 127 | + def (_, _, rootNamespace) = DecomposeData(data); |
| 128 | + // TODO: cache symbols an use it cache to find symbol by id |
| 129 | + def findSymbol(symbols : Seq[DeclarationSymbol]) : ValueOption[DeclarationSymbol] |
| 130 | + { |
| 131 | + foreach (symbol in symbols) |
| 132 | + { |
| 133 | + when (symbol.Id == symbolId) |
| 134 | + return ValueOption.Some(symbol); |
| 135 | + |
| 136 | + match (symbol) |
| 137 | + { |
| 138 | + | t is GenericContainerTypeSymbol => |
| 139 | + def result = findSymbol(t.NestedTypes); |
| 140 | + when (result.IsSome) |
| 141 | + return result; |
| 142 | + |
| 143 | + | NamespaceSymbol as ns => |
| 144 | + foreach (symbols in ns.MemberTable.Symbols) |
| 145 | + { |
| 146 | + def result = findSymbol(symbols); |
| 147 | + when (result.IsSome) |
| 148 | + return result; |
| 149 | + } |
| 150 | + |
| 151 | + | _ => () |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + ValueOption.None() |
| 156 | + } |
| 157 | + |
| 158 | + foreach (symbols in rootNamespace.MemberTable.Symbols) |
| 159 | + { |
| 160 | + def result = findSymbol(symbols); |
| 161 | + when (result.IsSome) |
| 162 | + return result; |
| 163 | + } |
| 164 | + |
| 165 | + ValueOption.None() |
| 166 | + } |
| 167 | + |
| 168 | + public DeconstructType(symbol : DeclarationSymbol, type : out TypeSymbol, typeArgs : out ImmutableArray[TypeSymbol]) : bool |
| 169 | + { |
| 170 | + match (symbol) |
| 171 | + { |
| 172 | + | s is TopConstructedTypeSymbol => type = s.TypeInfo; typeArgs = s.Args; true |
| 173 | + | s is NestedConstructedTypeSymbol => type = s.TypeInfo; typeArgs = s.Args; true |
| 174 | + | _ => type = null; typeArgs = ImmutableArray.Empty; false |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + public VisitGlobalSymbols(data : object, callback : Predicate[DeclarationSymbol]) : void |
| 179 | + { |
| 180 | + def (_, _, rootNamespace) = DecomposeData(data); |
| 181 | + _ = rootNamespace.VisitChildrenAndSelf(SymbolUtils.GetNestedSymbol, callback); |
| 182 | + } |
| 183 | + |
| 184 | + public Postprocessing(cancellationToken : System.Threading.CancellationToken, project : Nitra.ProjectSystem.Project, asts : System.Collections.Immutable.ImmutableArray[(Nitra.Declarations.IAst * bool)], data : object) : void |
| 185 | + { |
| 186 | + IgnoreParams(); |
| 187 | + } |
| 188 | + |
| 189 | + public DecomposeData(data : object) : IDotNetBackEnd * DependentPropertyEvalContext * NamespaceSymbol |
| 190 | + { |
| 191 | + data :> (IDotNetBackEnd * DependentPropertyEvalContext * NamespaceSymbol) |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments