Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/QsCompiler/CompilationManager/AssemblyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.Quantum.QsCompiler.ReservedKeywords;
using Microsoft.Quantum.QsCompiler.Serialization;
using Microsoft.Quantum.QsCompiler.SyntaxTree;
using Microsoft.Quantum.QsCompiler.Transformations;
using Newtonsoft.Json.Bson;

namespace Microsoft.Quantum.QsCompiler
Expand Down
2 changes: 1 addition & 1 deletion src/QsCompiler/CompilationManager/CompilationUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ public QsCompilation Build()
/// imported within a certain source file for the given namespace.
/// Throws an ArgumentException if no namespace with the given name exists.
/// </summary>
public ILookup<NonNullable<string>, (NonNullable<string>, string)> GetOpenDirectives(NonNullable<string> nsName)
public ILookup<NonNullable<string>, (NonNullable<string>, string?)> GetOpenDirectives(NonNullable<string> nsName)
{
this.syncRoot.EnterReadLock();
try
Expand Down
12 changes: 6 additions & 6 deletions src/QsCompiler/CompilationManager/CompilationUnitManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.Quantum.QsCompiler.SyntaxProcessing;
using Microsoft.Quantum.QsCompiler.SyntaxTokens;
using Microsoft.Quantum.QsCompiler.SyntaxTree;
using Microsoft.Quantum.QsCompiler.Transformations;
using Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.Quantum.QsCompiler.CompilationBuilder
Expand Down Expand Up @@ -930,7 +931,7 @@ public class Compilation
/// Contains a dictionary that maps the name of each namespace defined in the compilation to a look-up
/// containing the names and corresponding short form (if any) of all opened namespaces for that (part of the) namespace in a particular source file.
/// </summary>
private readonly ImmutableDictionary<NonNullable<string>, ILookup<NonNullable<string>, (NonNullable<string>, string)>> openDirectivesForEachFile;
private readonly ImmutableDictionary<NonNullable<string>, ILookup<NonNullable<string>, (NonNullable<string>, string?)>> openDirectivesForEachFile;

/// <summary>
/// Contains a dictionary that given the ID of a file included in the compilation
Expand Down Expand Up @@ -1022,11 +1023,10 @@ public QsComments NamespaceComments(NonNullable<string> sourceFile, NonNullable<
/// returns the names and corresponding short form (if any) of all opened namespaces for the (part of the) namespace in that file.
/// Returns an empy sequence if no source file with the given ID and/or namespace with the given name exists in the compilation.
/// </summary>
public IEnumerable<(NonNullable<string>, string)> OpenDirectives(NonNullable<string> sourceFile, NonNullable<string> nsName) =>
this.openDirectivesForEachFile
.TryGetValue(nsName, out ILookup<NonNullable<string>, (NonNullable<string>, string)> lookUp)
? lookUp[sourceFile]
: Enumerable.Empty<(NonNullable<string>, string)>();
public IEnumerable<(NonNullable<string>, string?)> OpenDirectives(NonNullable<string> sourceFile, NonNullable<string> nsName) =>
this.openDirectivesForEachFile.TryGetValue(nsName, out var lookUp)
? lookUp[sourceFile]
: Enumerable.Empty<(NonNullable<string>, string?)>();

/// <summary>
/// Returns all the names of all callable and types defined in the namespace with the given name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.Quantum.QsCompiler.SyntaxProcessing;
using Microsoft.Quantum.QsCompiler.SyntaxTokens;
using Microsoft.Quantum.QsCompiler.TextProcessing;
using Microsoft.Quantum.QsCompiler.Transformations;
using Microsoft.Quantum.QsCompiler.Transformations.QsCodeOutput;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Lsp = Microsoft.VisualStudio.LanguageServer.Protocol;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.Quantum.QsCompiler.SyntaxTree;
using Microsoft.Quantum.QsCompiler.TextProcessing;
using Microsoft.Quantum.QsCompiler.TextProcessing.CodeCompletion;
using Microsoft.Quantum.QsCompiler.Transformations;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using static Microsoft.Quantum.QsCompiler.SyntaxGenerator;
using static Microsoft.Quantum.QsCompiler.TextProcessing.CodeCompletion.FragmentParsing;
Expand Down Expand Up @@ -538,14 +539,15 @@ private static IEnumerable<CompletionItem> GetNamespaceAliasCompletions(
{
prefix += ".";
}
var @namespace = file.TryGetNamespaceAt(position);
if (@namespace == null || !compilation.GlobalSymbols.NamespaceExists(NonNullable<string>.New(@namespace)))
var ns = file.TryGetNamespaceAt(position);
if (ns == null || !compilation.GlobalSymbols.NamespaceExists(NonNullable<string>.New(ns)))
{
return Array.Empty<CompletionItem>();
}
return compilation
.GetOpenDirectives(NonNullable<string>.New(@namespace))[file.FileName]
.Where(open => open.Item2 != null && open.Item2.StartsWith(prefix))
.GetOpenDirectives(NonNullable<string>.New(ns))[file.FileName]
.SelectNotNull(open => open.Item2?.Apply(alias => (open.Item1, alias)))
.Where(open => open.Item2.StartsWith(prefix))
.GroupBy(open => NextNamespacePart(open.Item2, prefix.Length))
.Select(open => new CompletionItem()
{
Expand Down
1 change: 1 addition & 0 deletions src/QsCompiler/CompilationManager/ProjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.Quantum.QsCompiler.DataTypes;
using Microsoft.Quantum.QsCompiler.Diagnostics;
using Microsoft.Quantum.QsCompiler.ReservedKeywords;
using Microsoft.Quantum.QsCompiler.Transformations;
using Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.Quantum.QsCompiler.CompilationBuilder
Expand Down
2 changes: 1 addition & 1 deletion src/QsCompiler/CompilationManager/TypeChecking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
using Microsoft.Quantum.QsCompiler.SyntaxTokens;
using Microsoft.Quantum.QsCompiler.SyntaxTree;
using Microsoft.Quantum.QsCompiler.TextProcessing;
using Microsoft.Quantum.QsCompiler.Transformations;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Lsp = Microsoft.VisualStudio.LanguageServer.Protocol;
using Position = Microsoft.Quantum.QsCompiler.DataTypes.Position;
using Range = Microsoft.Quantum.QsCompiler.DataTypes.Range;

Expand Down
32 changes: 0 additions & 32 deletions src/QsCompiler/CompilationManager/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,6 @@ internal static string GetTextChangedLines(FileContentManager file, TextDocument

// general purpose type extensions

/// <summary>
/// Applies the action <paramref name="f"/> to the value <paramref name="x"/>.
/// </summary>
internal static void Apply<T>(this T x, Action<T> f) => f(x);

/// <summary>
/// Applies the function <paramref name="f"/> to the value <paramref name="x"/> and returns the result.
/// </summary>
internal static TOut Apply<TIn, TOut>(this TIn x, Func<TIn, TOut> f) => f(x);

/// <summary>
/// Partitions the given IEnumerable into the elements for which predicate returns true and those for which it returns false.
/// Throws an ArgumentNullException if the given IEnumerable or predicate is null.
Expand All @@ -179,28 +169,6 @@ public static (List<T>, List<T>) Partition<T>(this IEnumerable<T> collection, Fu
return (collection.Where(predicate).ToList(), collection.Where(x => !predicate(x)).ToList());
}

/// <summary>
/// Projects each element of a sequence into a new form and discards null values.
/// </summary>
/// <remarks>Overload for reference types.</remarks>
internal static IEnumerable<TResult> SelectNotNull<TSource, TResult>(
this IEnumerable<TSource> source, Func<TSource, TResult?> selector)
where TResult : class =>
source.SelectMany(item =>
selector(item)?.Apply(result => new[] { result })
?? Enumerable.Empty<TResult>());

/// <summary>
/// Projects each element of a sequence into a new form and discards null values.
/// </summary>
/// <remarks>Overload for value types.</remarks>
internal static IEnumerable<TResult> SelectNotNull<TSource, TResult>(
this IEnumerable<TSource> source, Func<TSource, TResult?> selector)
where TResult : struct =>
source.SelectMany(item =>
selector(item)?.Apply(result => new[] { result })
?? Enumerable.Empty<TResult>());

/// <summary>
/// Returns true if the given lock is either ReadLockHeld, or is UpgradeableReadLockHeld, or isWriteLockHeld.
/// Throws an ArgumentNullException if the given lock is null.
Expand Down
2 changes: 1 addition & 1 deletion src/QsCompiler/DocumentationParser/DocCallable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class DocCallable : DocItem
{
private readonly string syntax;
private readonly string inputContent;
private readonly string outputType;
private readonly string? outputType;
private readonly List<string> functors = new List<string>();
private readonly QsCallable callable;

Expand Down
8 changes: 4 additions & 4 deletions src/QsCompiler/DocumentationParser/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ internal static class Utils
internal static readonly string YamlExtension = ".yml";
internal static readonly string LogExtension = ".log";

internal static YamlNode BuildStringNode(string item)
internal static YamlNode BuildStringNode(string? item)
{
var node = new YamlScalarNode(item);
// Set the style to literal (YAML |-) if the string is multi-line
if (item.IndexOfAny(new char[] { '\n', '\r' }) >= 0)
if (item?.IndexOfAny(new char[] { '\n', '\r' }) >= 0)
{
node.Style = YamlDotNet.Core.ScalarStyle.Literal;
}
Expand All @@ -69,7 +69,7 @@ internal static YamlNode BuildStringNode(string item)
internal static void AddString(this YamlSequenceNode root, string item) => root.Add(BuildStringNode(item));

// We don't need to set the key to literal because all of our keys are simple one-line strings.
internal static void AddStringMapping(this YamlMappingNode root, string key, string item)
internal static void AddStringMapping(this YamlMappingNode root, string key, string? item)
{
root.Children[new YamlScalarNode(key)] = BuildStringNode(item);
}
Expand Down Expand Up @@ -129,7 +129,7 @@ internal static YamlNode BuildSequenceMappingNode(Dictionary<string, string> pai
/// </summary>
/// <param name="t">The resolved type</param>
/// <returns>A string containing the source representation of the type</returns>
internal static string ResolvedTypeToString(ResolvedType t) =>
internal static string? ResolvedTypeToString(ResolvedType t) =>
SyntaxTreeToQsharp.Default.ToCode(t);

/// <summary>
Expand Down
18 changes: 9 additions & 9 deletions src/QsCompiler/Transformations/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,46 +53,46 @@ public static TypedExpression StringArguments(params string[] items) =>
/// - if the predicate is specified and not null.
/// Throws an ArgumentNullException if the given attribute or compilation is null.
/// </summary>
public static QsCompilation AddToCallables(QsCompilation compilation, QsDeclarationAttribute attribute, CallablePredicate predicate = null) =>
public static QsCompilation AddToCallables(QsCompilation compilation, QsDeclarationAttribute attribute, CallablePredicate? predicate = null) =>
new AddAttributes(new[] { (attribute, predicate) }).OnCompilation(compilation);

/// <summary>
/// Adds the given attribute(s) to all callables in the given compilation that satisfy the given predicate
/// - if the predicate is specified and not null.
/// Throws an ArgumentNullException if one of the given attributes or the compilation is null.
/// </summary>
public static QsCompilation AddToCallables(QsCompilation compilation, params (QsDeclarationAttribute, CallablePredicate)[] attributes) =>
public static QsCompilation AddToCallables(QsCompilation compilation, params (QsDeclarationAttribute, CallablePredicate?)[] attributes) =>
new AddAttributes(attributes).OnCompilation(compilation);

/// <summary>
/// Adds the given attribute(s) to all callables in the given compilation.
/// Throws an ArgumentNullException if one of the given attributes or the compilation is null.
/// </summary>
public static QsCompilation AddToCallables(QsCompilation compilation, params QsDeclarationAttribute[] attributes) =>
new AddAttributes(attributes.Select(att => (att, (CallablePredicate)null))).OnCompilation(compilation);
new AddAttributes(attributes.Select(att => (att, (CallablePredicate?)null))).OnCompilation(compilation);

/// <summary>
/// Adds the given attribute to all callables in the given namespace that satisfy the given predicate
/// - if the predicate is specified and not null.
/// Throws an ArgumentNullException if the given attribute or namespace is null.
/// </summary>
public static QsNamespace AddToCallables(QsNamespace ns, QsDeclarationAttribute attribute, CallablePredicate predicate = null) =>
public static QsNamespace AddToCallables(QsNamespace ns, QsDeclarationAttribute attribute, CallablePredicate? predicate = null) =>
new AddAttributes(new[] { (attribute, predicate) }).Namespaces.OnNamespace(ns);

/// <summary>
/// Adds the given attribute(s) to all callables in the given namespace that satisfy the given predicate
/// - if the predicate is specified and not null.
/// Throws an ArgumentNullException if one of the given attributes or the namespace is null.
/// </summary>
public static QsNamespace AddToCallables(QsNamespace ns, params (QsDeclarationAttribute, CallablePredicate)[] attributes) =>
public static QsNamespace AddToCallables(QsNamespace ns, params (QsDeclarationAttribute, CallablePredicate?)[] attributes) =>
new AddAttributes(attributes).Namespaces.OnNamespace(ns);

/// <summary>
/// Adds the given attribute(s) to all callables in the given namespace.
/// Throws an ArgumentNullException if one of the given attributes or the namespace is null.
/// </summary>
public static QsNamespace AddToCallables(QsNamespace ns, params QsDeclarationAttribute[] attributes) =>
new AddAttributes(attributes.Select(att => (att, (CallablePredicate)null))).Namespaces.OnNamespace(ns);
new AddAttributes(attributes.Select(att => (att, (CallablePredicate?)null))).Namespaces.OnNamespace(ns);

// private transformation class(es)

Expand All @@ -104,14 +104,14 @@ private class AddAttributes
{
internal class TransformationState
{
internal readonly ImmutableArray<(QsDeclarationAttribute, Func<QsCallable, bool>)> AttributeSelection;
internal readonly ImmutableArray<(QsDeclarationAttribute, CallablePredicate)> AttributeSelection;

/// <exception cref="ArgumentNullException">Thrown when the given selection is null.</exception>
internal TransformationState(IEnumerable<(QsDeclarationAttribute, Func<QsCallable, bool>)> selections) =>
internal TransformationState(IEnumerable<(QsDeclarationAttribute, CallablePredicate)>? selections) =>
this.AttributeSelection = selections?.ToImmutableArray() ?? throw new ArgumentNullException(nameof(selections));
}

internal AddAttributes(IEnumerable<(QsDeclarationAttribute, CallablePredicate)> attributes)
internal AddAttributes(IEnumerable<(QsDeclarationAttribute, CallablePredicate?)> attributes)
: base(new TransformationState(attributes?.Select(entry => (entry.Item1, entry.Item2 ?? (_ => true)))))
{
if (attributes == null || attributes.Any(entry => entry.Item1 == null))
Expand Down
6 changes: 3 additions & 3 deletions src/QsCompiler/Transformations/BasicTransformations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private GetSourceFiles()
/// </summary>
public static ImmutableHashSet<NonNullable<string>> Apply(IEnumerable<QsNamespace> namespaces)
{
if (namespaces == null || namespaces.Contains(null))
if (namespaces == null || namespaces.Contains(null!))
{
throw new ArgumentNullException(nameof(namespaces));
}
Expand Down Expand Up @@ -235,7 +235,7 @@ public SelectByFoldingOverExpressions(Func<TypedExpression, bool> condition, Fun
public class StatementTransformation<TSelector>
: Core.StatementTransformation<TransformationState> where TSelector : SelectByFoldingOverExpressions
{
protected TSelector SubSelector;
protected TSelector? SubSelector;
protected readonly Func<TransformationState, TSelector> CreateSelector;

/// <summary>
Expand Down Expand Up @@ -267,7 +267,7 @@ public override QsScope OnScope(QsScope scope)
// StatementKind.Transform sets a new Subselector that walks all expressions contained in statement,
// and sets its satisfiesCondition to true if one of them satisfies the condition given on initialization
var transformed = this.OnStatement(statement);
if (this.SubSelector.SharedState.SatisfiesCondition)
if (this.SubSelector?.SharedState.SatisfiesCondition ?? false)
{
statements.Add(transformed);
}
Expand Down
Loading