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/BondSchemas/CompilerObjectTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ private static SyntaxTree.QsDeclarationAttribute ToCompilerObject(this QsDeclara
typeId: bondQsDeclarationAttribute.TypeId != null ?
bondQsDeclarationAttribute.TypeId.ToCompilerObject().ToQsNullableGeneric() :
QsNullable<SyntaxTree.UserDefinedType>.Null,
typeIdRange: QsNullable<DataTypes.Range>.Null,
argument: bondQsDeclarationAttribute.Argument.ToCompilerObject(),
offset: bondQsDeclarationAttribute.Offset.ToCompilerObject(),
comments: bondQsDeclarationAttribute.Comments.ToCompilerObject());
Expand Down
13 changes: 6 additions & 7 deletions src/QsCompiler/CompilationManager/CompilationUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,14 @@ private static Headers LoadTestNames(string source, Headers headers)

static QsDeclarationAttribute Renamed(QsQualifiedName originalName, Position declLocation)
{
var attName = new UserDefinedType(
GeneratedAttributes.Namespace,
GeneratedAttributes.LoadedViaTestNameInsteadOf,
QsNullable<Range>.Null);
var attArg = SyntaxGenerator.StringLiteral(
originalName.ToString(),
ImmutableArray<TypedExpression>.Empty);
var attName =
UserDefinedType.New(GeneratedAttributes.Namespace, GeneratedAttributes.LoadedViaTestNameInsteadOf);
var attArg =
SyntaxGenerator.StringLiteral(originalName.ToString(), ImmutableArray<TypedExpression>.Empty);

return new QsDeclarationAttribute(
QsNullable<UserDefinedType>.NewValue(attName),
QsNullable<Range>.Null,
attArg,
declLocation,
QsComments.Empty);
Expand Down
19 changes: 5 additions & 14 deletions src/QsCompiler/Core/ConstructorExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ type QsQualifiedName with
static member New(nsName, cName) = { Namespace = nsName; Name = cName }

type UserDefinedType with
static member New(ns, name) =
{
Namespace = ns
Name = name
Range = Null
}

// TODO: RELEASE 2021-10: Remove member.
[<Obsolete "Use UserDefinedType.New(string, string) instead.">]
static member New(nsName, tName, range) =
{
Expand All @@ -35,13 +29,7 @@ type UserDefinedType with
}

type QsTypeParameter with
static member New(origin, name) =
{
Origin = origin
TypeName = name
Range = Null
}

// TODO: RELEASE 2021-10: Remove member.
[<Obsolete "Use QsTypeParameter.New(string, string) instead.">]
static member New(origin, tName, range) =
{
Expand Down Expand Up @@ -248,9 +236,12 @@ type QsCustomType with
}

type QsDeclarationAttribute with
// TODO: RELEASE 2021-10: Remove member.
[<Obsolete "Use the overload of New with 5 parameters.">]
static member New(typeId, arg, pos, comments) =
{
TypeId = typeId
TypeIdRange = Null
Argument = arg
Offset = pos
Comments = comments
Expand Down
1 change: 1 addition & 0 deletions src/QsCompiler/Core/SymbolResolution.fs
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@ module SymbolResolution =
let buildAttribute id =
{
TypeId = id
TypeIdRange = attribute.Id.Range
Argument = resArg
Offset = attribute.Position
Comments = attribute.Comments
Expand Down
29 changes: 15 additions & 14 deletions src/QsCompiler/Core/SymbolTable/NamespaceManager.fs
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,9 @@ type NamespaceManager(syncRoot: IReaderWriterLock,
/// </summary>
/// <exception cref="NotSupportedException"><paramref name="qsType"/> contains a <see cref="MissingType"/>.</exception>
let resolveType (parent: QsQualifiedName, tpNames, source) qsType checkUdt =
let processUDT =
tryResolveTypeName (parent.Namespace, source)
>> function
| Some (udt, _, access), errs -> UserDefinedType udt, Array.append errs (checkUdt (udt, access))
let processUDT (name, range) =
match tryResolveTypeName (parent.Namespace, source) (name, range) with
| Some (udt, _, access), errs -> UserDefinedType udt, Array.append errs (checkUdt (udt, range, access))
| None, errs -> InvalidType, errs

let processTP (symName, symRange) =
Expand Down Expand Up @@ -274,10 +273,12 @@ type NamespaceManager(syncRoot: IReaderWriterLock,
/// Compares the accessibility of the parent declaration with the accessibility of the UDT being referenced. If the
/// accessibility of a referenced type is less than the accessibility of the parent, returns a diagnostic using the
/// given error code. Otherwise, returns an empty array.
let checkUdtAccess code (parent, parentAccess) (udt: UserDefinedType, udtAccess) =
let checkUdtAccess code (parent, parentAccess) (udt: UserDefinedType, udtRange, udtAccess) =
let udtRange = udtRange |> QsNullable.defaultValue Range.Zero

[|
if udtAccess < parentAccess
then yield QsCompilerDiagnostic.Error (code, [ udt.Name; parent ]) (udt.Range.ValueOr Range.Zero)
then yield QsCompilerDiagnostic.Error (code, [ udt.Name; parent ]) udtRange
|]

/// <summary>
Expand Down Expand Up @@ -498,15 +499,15 @@ type NamespaceManager(syncRoot: IReaderWriterLock,
// the attribute is a duplication of another attribute on this declaration
if alreadyDefined.Contains attributeHash then
(att.Offset,
tId.Range
att.TypeIdRange
|> orDefault
|> QsCompilerDiagnostic.Warning(WarningCode.DuplicateAttribute, [ tId.Name ]))
|> Seq.singleton
|> returnInvalid

// the attribute marks an entry point
elif tId |> isBuiltIn BuiltIn.EntryPoint then
let register, msgs = validateEntryPoint parent (att.Offset, tId.Range) decl
let register, msgs = validateEntryPoint parent (att.Offset, att.TypeIdRange) decl
errs.AddRange msgs

if register
Expand Down Expand Up @@ -550,7 +551,7 @@ type NamespaceManager(syncRoot: IReaderWriterLock,
|> returnInvalid
| _ ->
(att.Offset,
tId.Range
att.TypeIdRange
|> orDefault
|> QsCompilerDiagnostic.Error(ErrorCode.InvalidTestAttributePlacement, []))
|> Seq.singleton
Expand All @@ -563,7 +564,7 @@ type NamespaceManager(syncRoot: IReaderWriterLock,
match box decl.Defined with
| :? QsSpecializationGenerator ->
(att.Offset,
tId.Range
att.TypeIdRange
|> orDefault
|> QsCompilerDiagnostic.Error(ErrorCode.AttributeInvalidOnSpecialization, [ tId.Name ]))
|> Seq.singleton
Expand All @@ -572,7 +573,7 @@ type NamespaceManager(syncRoot: IReaderWriterLock,
attributeHash :: alreadyDefined, att :: resAttr
| _ ->
(att.Offset,
tId.Range
att.TypeIdRange
|> orDefault
|> QsCompilerDiagnostic.Error(ErrorCode.ExpectingFullNameAsAttributeArgument, [ tId.Name ]))
|> Seq.singleton
Expand All @@ -583,14 +584,14 @@ type NamespaceManager(syncRoot: IReaderWriterLock,
match box decl.Defined with
| :? CallableSignature ->
(att.Offset,
tId.Range
att.TypeIdRange
|> orDefault
|> QsCompilerDiagnostic.Error(ErrorCode.AttributeInvalidOnCallable, [ tId.Name ]))
|> Seq.singleton
|> returnInvalid
| :? QsSpecializationGenerator ->
(att.Offset,
tId.Range
att.TypeIdRange
|> orDefault
|> QsCompilerDiagnostic.Error(ErrorCode.AttributeInvalidOnSpecialization, [ tId.Name ]))
|> Seq.singleton
Expand All @@ -602,7 +603,7 @@ type NamespaceManager(syncRoot: IReaderWriterLock,
match box decl.Defined with
| :? QsSpecializationGenerator ->
(att.Offset,
tId.Range
att.TypeIdRange
|> orDefault
|> QsCompilerDiagnostic.Error(ErrorCode.AttributeInvalidOnSpecialization, [ tId.Name ]))
|> Seq.singleton
Expand Down
3 changes: 2 additions & 1 deletion src/QsCompiler/Core/Transformations/TypeTransformation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ type TypeTransformationBase(options: TransformationOptions) =

// transformation root called on each node

member this.OnType(t: ResolvedType) =
abstract OnType: ResolvedType -> ResolvedType
default this.OnType(t: ResolvedType) =
if not options.Enable then
t
else
Expand Down
63 changes: 60 additions & 3 deletions src/QsCompiler/DataStructures/SyntaxTree.fs
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,26 @@ type QsTypeParameter =
override param.GetHashCode() = hash (param.Origin, param.TypeName)

/// <summary>
/// Returns this type parameter with the given <paramref name="origin"/>.
/// Returns a copy of this <see cref="QsTypeParameter"/> with updated fields.
/// </summary>
member param.WithOrigin origin = { param with Origin = origin }
member param.With([<Optional; DefaultParameterValue null>] ?origin,
[<Optional; DefaultParameterValue null>] ?typeName,
[<Optional; DefaultParameterValue null>] ?range) =
{ param with
Origin = defaultArg origin param.Origin
TypeName = defaultArg typeName param.TypeName
Range = defaultArg range param.Range
}

/// <summary>
/// Creates a new <see cref="QsTypeParameter"/>.
/// </summary>
static member New(origin, name) =
{
Origin = origin
TypeName = name
Range = Null
}

/// used to represent the use of a user defined type within a fully resolved Q# type
[<CustomEquality>]
Expand Down Expand Up @@ -189,6 +206,28 @@ type UserDefinedType =
member this.GetFullName() =
{ Namespace = this.Namespace; Name = this.Name }

/// <summary>
/// Returns a copy of this <see cref="UserDefinedType"/> with updated fields.
/// </summary>
member udt.With([<Optional; DefaultParameterValue null>] ?ns,
[<Optional; DefaultParameterValue null>] ?name,
[<Optional; DefaultParameterValue null>] ?range) =
{ udt with
Namespace = defaultArg ns udt.Namespace
Name = defaultArg name udt.Name
Range = defaultArg range udt.Range
}

/// <summary>
/// Creates a new <see cref="UserDefinedType"/>.
/// </summary>
static member New(ns, name) =
{
Namespace = ns
Name = name
Range = Null
}

/// Fully resolved operation characteristics used to describe the properties of a Q# callable.
/// A resolved characteristic expression by construction never contains an empty or invalid set as inner expressions,
/// and necessarily contains the property "Adjointable" if it contains the property "SelfAdjoint".
Expand Down Expand Up @@ -876,17 +915,35 @@ type QsLocalSymbol =
type QsDeclarationAttribute =
{
/// Identifies the user defined type that the attribute instantiates.
/// The range information describes the range occupied by the attribute identifier relative to the attribute offset.
/// Is Null only if the correct attribute could not be determined. Attributes set to Null should be ignored.
TypeId: QsNullable<UserDefinedType>

/// <summary>
/// The range of <see cref="TypeId"/> relative to <see cref="Offset"/>.
/// </summary>
TypeIdRange: QsNullable<Range>

/// Contains the argument with which the attribute is instantiated.
Argument: TypedExpression

/// Represents the position in the source file where the attribute is used.
Offset: Position

/// contains comments in the code associated with the attached attribute
Comments: QsComments
}

/// <summary>
/// Creates a new <see cref="QsDeclarationAttribute"/>.
/// </summary>
static member New(typeId, typeIdRange, argument, offset, comments) =
{
TypeId = typeId
TypeIdRange = typeIdRange
Argument = argument
Offset = offset
Comments = comments
}

/// Fully resolved Q# callable signature
type ResolvedSignature =
Expand Down
2 changes: 1 addition & 1 deletion src/QsCompiler/Transformations/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private static AttributeId BuildId(QsQualifiedName name) =>
/// The attribute argument is set to an invalid expression if the given argument is null.
/// </summary>
public static QsDeclarationAttribute BuildAttribute(QsQualifiedName name, TypedExpression arg) =>
new QsDeclarationAttribute(BuildId(name), arg ?? SyntaxGenerator.InvalidExpression, Position.Zero, QsComments.Empty);
new QsDeclarationAttribute(BuildId(name), QsNullable<Range>.Null, arg, Position.Zero, QsComments.Empty);

/// <summary>
/// Builds a string literal with the given content that can be used as argument to a Q# attribute.
Expand Down
2 changes: 1 addition & 1 deletion src/QsCompiler/Transformations/ContentLifting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ public TypeTransformation(SyntaxTreeTransformation<TransformationState> parent)
public override ResolvedTypeKind OnTypeParameter(QsTypeParameter tp) =>
// Reroute a type parameter's origin to the newly generated operation
!this.SharedState.IsRecursiveIdentifier && this.SharedState.OldName.Equals(tp.Origin)
? base.OnTypeParameter(tp.WithOrigin(this.SharedState.NewName))
? base.OnTypeParameter(tp.With(this.SharedState.NewName))
: base.OnTypeParameter(tp);
}
}
Expand Down
Loading