Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Validation for entry points (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
bettinaheim committed Oct 19, 2019
1 parent 865336f commit 5d8490c
Show file tree
Hide file tree
Showing 45 changed files with 832 additions and 159 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,4 @@ ASALocalRun/
# MFractors (Xamarin productivity tool) working folder
.mfractor/
/src/QsCompiler/TestTargets/Simulation/Example/generated
/src/ProjectTemplates/.vscode
2 changes: 1 addition & 1 deletion src/QsCompiler/CommandLineTool/CommandLineTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.2.1" />
<PackageReference Include="CommandLineParser" Version="2.6.0" />
</ItemGroup>

<ItemGroup>
Expand Down
10 changes: 7 additions & 3 deletions src/QsCompiler/CompilationManager/CompilationUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private static IEnumerable<TypeDeclarationHeader> TypeHeaders(IEnumerable<(strin
/// Throws an ArgumentNullException if the given dictionary of references is null.
/// Throws an ArgumentException if the given set shares references with the current one.
/// </summary>
internal References CombineWith(References other, Action<ErrorCode, string[]> onError)
internal References CombineWith(References other, Action<ErrorCode, string[]> onError = null)
{
if (other == null) throw new ArgumentNullException(nameof(other));
if (this.Declarations.Keys.Intersect(other.Declarations.Keys).Any()) throw new ArgumentException("common references exist");
Expand All @@ -106,7 +106,7 @@ internal References CombineWith(References other, Action<ErrorCode, string[]> on
/// i.e. if two or more references contain a declaration with the same fully qualified name.
/// Throws an ArgumentNullException if the given diagnostics are null.
/// </summary>
internal References Remove(NonNullable<string> source, Action<ErrorCode, string[]> onError) =>
internal References Remove(NonNullable<string> source, Action<ErrorCode, string[]> onError = null) =>
new References(this.Declarations.Remove(source), onError);

/// <summary>
Expand All @@ -116,7 +116,7 @@ internal References Remove(NonNullable<string> source, Action<ErrorCode, string[
/// i.e. if two or more references contain a declaration with the same fully qualified name.
/// Throws an ArgumentNullException if the given dictionary of references is null.
/// </summary>
public References(ImmutableDictionary<NonNullable<string>, Headers> refs, Action<ErrorCode, string[]> onError)
public References(ImmutableDictionary<NonNullable<string>, Headers> refs, Action<ErrorCode, string[]> onError = null)
{
this.Declarations = refs ?? throw new ArgumentNullException(nameof(refs));
if (onError == null) return;
Expand Down Expand Up @@ -199,6 +199,7 @@ internal void UpdateReferences(References externals)
/// </summary>
internal void RegisterDependentLock(ReaderWriterLockSlim depLock)
{
#if DEBUG
if (depLock == null) throw new ArgumentNullException(nameof(depLock));
this.SyncRoot.EnterWriteLock();
try
Expand All @@ -207,6 +208,7 @@ internal void RegisterDependentLock(ReaderWriterLockSlim depLock)
{ this.DependentLocks.Add(depLock); }
}
finally { this.SyncRoot.ExitWriteLock(); }
#endif
}

/// <summary>
Expand All @@ -216,6 +218,7 @@ internal void RegisterDependentLock(ReaderWriterLockSlim depLock)
/// </summary>
internal void UnregisterDependentLock(ReaderWriterLockSlim depLock)
{
#if DEBUG
if (depLock == null) throw new ArgumentNullException(nameof(depLock));
this.SyncRoot.EnterWriteLock();
try
Expand All @@ -224,6 +227,7 @@ internal void UnregisterDependentLock(ReaderWriterLockSlim depLock)
{ this.DependentLocks.Remove(depLock); }
}
finally { this.SyncRoot.ExitWriteLock(); }
#endif
}


Expand Down
11 changes: 6 additions & 5 deletions src/QsCompiler/CompilationManager/CompilationUnitManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public T FlushAndExecute<T>(Func<T> execute = null) where T : class
file.Flush();
this.PublishDiagnostics(file.Diagnostics());
}
var task = this.EnableVerification ? this.SpawnGlobalTypeCheckingAsync(runSynchronously: true) : Task.CompletedTask;
var task = this.EnableVerification ? this.SpawnGlobalTypeCheckingAsync(runSynchronously: true) : Task.CompletedTask;
QsCompilerError.Verify(task.IsCompleted, "global type checking hasn't completed");
return execute?.Invoke();
}
Expand Down Expand Up @@ -223,17 +223,18 @@ public static ImmutableHashSet<FileContentManager> InitializeFileManagers(IDicti
/// <summary>
/// Adds the given source file to this compilation unit, adapting the diagnostics for all remaining files as needed.
/// If a file with that Uri is already listed as source file,
/// replaces the current FileContentManager for that file with a new one and initialized its content to the given one.
/// replaces the current FileContentManager for that file with the given one.
/// If the content to update is specified and not null, replaces the tracked content in the file manager with the given one.
/// Throws an ArgumentNullException if any of the compulsory arguments is null or the set uri is.
/// Throws an ArgumentException if the uri of the given text document identifier is null or not an absolute file uri.
/// </summary>
public Task AddOrUpdateSourceFileAsync(FileContentManager file, string updatedContent = null)
{
if (file == null) throw new ArgumentNullException(nameof(file));
this.CompilationUnit.RegisterDependentLock(file.SyncRoot);
this.SubscribeToFileManagerEvents(file);
return this.Processing.QueueForExecutionAsync(() =>
{
this.CompilationUnit.RegisterDependentLock(file.SyncRoot);
this.SubscribeToFileManagerEvents(file);
this.FileContentManagers.AddOrUpdate(file.FileName, file, (k, v) => file);
if (updatedContent != null) file.ReplaceFileContent(updatedContent);
this.ChangedFiles.Add(file.FileName);
Expand Down Expand Up @@ -434,7 +435,7 @@ private Task SpawnGlobalTypeCheckingAsync(bool runSynchronously = false)
var cancellationToken = runSynchronously ? new CancellationToken() : this.WaitForTypeCheck.Token;

// work with a separate compilation unit instance such that processing of all further edits can go on in parallel
var sourceFiles = this.FileContentManagers.Values;
var sourceFiles = this.FileContentManagers.Values.OrderBy(m => m.FileName);
this.ChangedFiles.RemoveAll(f => sourceFiles.Any(m => m.FileName.Value == f.Value));
var compilation = new CompilationUnit(this.CompilationUnit.Externals, sourceFiles.Select(file => file.SyncRoot));
var content = compilation.UpdateGlobalSymbolsFor(sourceFiles);
Expand Down
2 changes: 1 addition & 1 deletion src/QsCompiler/Core/Core.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<Compile Include="ExpressionTransformation.fs" />
<Compile Include="StatementTransformation.fs" />
<Compile Include="TreeTransformation.fs" />
<Compile Include="CoreDependencies.fs" />
<Compile Include="Dependencies.fs" />
<Compile Include="SyntaxGenerator.fs" />
<Compile Include="DeclarationHeaders.fs" />
<Compile Include="SymbolResolution.fs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Microsoft.Quantum.QsCompiler

open System.Collections.Immutable
open Microsoft.Quantum.QsCompiler.DataTypes
open Microsoft.Quantum.QsCompiler.SyntaxTree


type BuiltIn = {
Expand All @@ -20,19 +21,27 @@ type BuiltIn = {
static member IntrinsicNamespace = NonNullable<string>.New "Microsoft.Quantum.Intrinsic"
static member StandardArrayNamespace = NonNullable<string>.New "Microsoft.Quantum.Arrays"

/// returns the set of namespaces that is automatically opened for each compilation
static member NamespacesToAutoOpen = ImmutableHashSet.Create (BuiltIn.CoreNamespace)

/// returns true if the given attributes is an entry point attributes
static member internal IsEntryPointAttribute (att : QsDeclarationAttribute) = att.TypeId |> function
| Value tId -> tId.Namespace.Value = BuiltIn.EntryPoint.Namespace.Value && tId.Name.Value = BuiltIn.EntryPoint.Name.Value
| Null -> false


// hard dependencies in Microsoft.Quantum.Core

static member Length = {
Name = "Length" |> NonNullable<string>.New
Namespace = BuiltIn.CoreNamespace
TypeParameters = ImmutableArray.Create("T" |> NonNullable<string>.New)
}

static member IndexRange = {
Name = "IndexRange" |> NonNullable<string>.New
Namespace = BuiltIn.StandardArrayNamespace
TypeParameters = ImmutableArray.Empty
static member RangeReverse = {
Name = "RangeReverse" |> NonNullable<string>.New
Namespace = BuiltIn.CoreNamespace
TypeParameters = ImmutableArray.Empty
}

static member Attribute = {
Expand All @@ -41,3 +50,17 @@ type BuiltIn = {
TypeParameters = ImmutableArray.Empty
}

static member EntryPoint = {
Name = "EntryPoint" |> NonNullable<string>.New
Namespace = BuiltIn.CoreNamespace
TypeParameters = ImmutableArray.Empty
}


// "weak dependencies" in other namespaces (e.g. things used for code actions)

static member IndexRange = {
Name = "IndexRange" |> NonNullable<string>.New
Namespace = BuiltIn.StandardArrayNamespace
TypeParameters = ImmutableArray.Empty
}
Loading

0 comments on commit 5d8490c

Please sign in to comment.