Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Resolving Symbolic References in a CodeDOM (Part 7)

, 2 Dec 2012
Resolving symbolic references in a CodeDOM.
Nova.0.6.exe.zip
Nova.0.6.zip
Nova.CLI
Properties
Nova.CodeDOM
CodeDOM
Annotations
Base
Comments
Base
DocComments
CodeRef
Base
List
Name
Base
Other
Simple
CompilerDirectives
Base
Conditionals
Base
Messages
Base
Pragmas
Base
Symbols
Base
Base
Interfaces
Expressions
AnonymousMethods
Base
Operators
Base
Binary
Arithmetic
Base
Assignment
Base
Bitwise
Base
Conditional
Relational
Base
Shift
Base
Other
Base
Unary
Base
Other
References
Base
GotoTargets
Base
Methods
Namespaces
Other
Properties
Types
Base
Variables
Base
Projects
Assemblies
Namespaces
References
Base
Statements
Base
Conditionals
Base
Exceptions
Generics
Constraints
Base
Iterators
Base
Jumps
Loops
Methods
OperatorDecls
Miscellaneous
Namespaces
Properties
Base
Events
Types
Base
Variables
Base
Parsing
Base
Properties
Rendering
Resolving
Utilities
Mono.Cecil
Reflection
Nova.Examples
Properties
Nova.Studio
Images
About.png
Configuration.png
EditCopy.png
EditCut.png
EditDelete.png
EditPaste.png
EditRedo.png
EditUndo.png
Error.png
Exit.png
FileNew.png
FileOpen.png
FileSave.png
FileSaveAll.png
FileSaveAs.png
Find.png
Help.png
Info.png
Logo.png
Options.png
Print.png
PrintPreview.png
Properties.png
Todo.png
Warning.png
Objects.ico
Properties
Settings.settings
Nova.Test
Properties
Nova.UI
CodeDOM
Annotations
Base
Comments
Base
DocComments
CodeRef
Base
List
Name
Base
Other
Simple
CompilerDirectives
Base
Conditionals
Base
Messages
Base
Pragmas
Base
Symbols
Base
Base
Expressions
AnonymousMethods
Base
Operators
Base
Binary
Arithmetic
Base
Assignment
Base
Bitwise
Base
Conditional
Relational
Base
Shift
Base
Other
Base
Unary
Base
Other
References
Base
GotoTargets
Base
Methods
Namespaces
Other
Properties
Types
Base
Variables
Base
Projects
Namespaces
References
Base
Statements
Base
Conditionals
Base
Exceptions
Generics
Constraints
Base
Iterators
Base
Jumps
Loops
Methods
OperatorDecls
Miscellaneous
Namespaces
Properties
Base
Events
Types
Base
Variables
Base
Properties
Resolving
Utilties
// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using Nova.Parsing;
using Nova.Rendering;
using Nova.Resolving;

namespace Nova.CodeDOM
{
    /// <summary>
    /// Represents one or more contraints on a type parameter.
    /// </summary>
    /// <remarks>
    /// The constraints may consist of:
    ///    - a <see cref="ClassConstraint"/>
    ///    - a <see cref="StructConstraint"/>
    ///    - a <see cref="TypeConstraint"/> for a base class
    ///    - one or more <see cref="TypeConstraint"/>s for interfaces (which can also be generic)
    ///    - a <see cref="TypeConstraint"/> for a base class followed by one or more <see cref="TypeConstraint"/>s for interfaces
    ///    - a <see cref="TypeConstraint"/> for another <see cref="TypeParameter"/> (a "naked type constraint")
    ///    - a <see cref="NewConstraint"/> by itself or in addition to any of the above (must be at the end)
    /// </remarks>
    public class ConstraintClause : CodeObject
    {
        #region /* FIELDS */

        protected SymbolicRef _typeParameter;
        protected ChildList<TypeParameterConstraint> _constraints;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a <see cref="ConstraintClause"/>.
        /// </summary>
        public ConstraintClause(SymbolicRef symbolicRef, params TypeParameterConstraint[] constraints)
        {
            TypeParameter = symbolicRef;
            CreateConstraints().AddRange(constraints);
        }

        /// <summary>
        /// Create a <see cref="ConstraintClause"/>.
        /// </summary>
        public ConstraintClause(TypeParameterRef typeParameterRef, params TypeParameterConstraint[] constraints)
            : this((SymbolicRef)typeParameterRef, constraints)
        { }

        /// <summary>
        /// Create a <see cref="ConstraintClause"/>.
        /// </summary>
        public ConstraintClause(TypeParameter typeParameter, params TypeParameterConstraint[] constraints)
            : this(typeParameter.CreateRef(), constraints)
        { }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The <see cref="TypeParameter"/> being constrained.
        /// </summary>
        public SymbolicRef TypeParameter
        {
            get { return _typeParameter; }
            set { SetField(ref _typeParameter, value, true); }
        }

        /// <summary>
        /// The list of <see cref="TypeParameterConstraint"/>s.
        /// </summary>
        public ChildList<TypeParameterConstraint> Constraints
        {
            get { return _constraints; }
        }

        #endregion

        #region /* METHODS */

        /// <summary>
        /// Create the list of <see cref="TypeParameterConstraint"/>s, or return the existing one.
        /// </summary>
        public ChildList<TypeParameterConstraint> CreateConstraints()
        {
            if (_constraints == null)
                _constraints = new ChildList<TypeParameterConstraint>(this);
            return _constraints;
        }

        /// <summary>
        /// Deep-clone the code object.
        /// </summary>
        public override CodeObject Clone()
        {
            ConstraintClause clone = (ConstraintClause)base.Clone();
            clone.CloneField(ref clone._typeParameter, _typeParameter);
            clone._constraints = ChildListHelpers.Clone(_constraints, clone);
            return clone;
        }

        #endregion

        #region /* PARSING */

        /// <summary>
        /// The token used to parse the code object.
        /// </summary>
        public const string ParseToken = "where";

        /// <summary>
        /// The token used to parse between the constraint type parameter and constraints.
        /// </summary>
        public const string ParseTokenSeparator = ":";

        /// <summary>
        /// Parse a <see cref="ConstraintClause"/>.
        /// </summary>
        public ConstraintClause(Parser parser, CodeObject parent)
            : base(parser, parent)
        {
            // Get any regular comments from before the 'where'
            MoveComments(parser.LastToken);

            parser.NextToken();  // Move past 'where'
            SetField(ref _typeParameter, UnresolvedRef.Create(parser.GetIdentifier()), false);
            ParseExpectedToken(parser, ParseTokenSeparator);  // Move past ':'

            // Parse list of TypeParameterConstraints
            while (true)
            {
                CreateConstraints().Add(TypeParameterConstraint.Parse(parser, this));
                if (parser.TokenText == TypeParameterConstraint.ParseTokenSeparator)
                    parser.NextToken();  // Move past ','
                else
                    break;
            }

            // Get the EOL comment from the Type expression of the last constraint if it's a TypeConstraint
            // (it will end up there since they use an Expression), otherwise from the LastToken.
            if (_constraints != null && _constraints.Last is TypeConstraint)
                MoveEOLComment(((TypeConstraint)_constraints.Last).Type);
            else
                MoveEOLComment(parser.LastToken);
        }

        /// <summary>
        /// Parse a list of constraint clauses.
        /// </summary>
        public static ChildList<ConstraintClause> ParseList(Parser parser, CodeObject parent)
        {
            ChildList<ConstraintClause> constraints = null;
            while (parser.TokenText == ParseToken)
            {
                if (constraints == null)
                    constraints = new ChildList<ConstraintClause>(parent);
                constraints.Add(new ConstraintClause(parser, parent));
            }
            return constraints;
        }

        #endregion

        #region /* RESOLVING */

        /// <summary>
        /// Resolve all child symbolic references, using the specified <see cref="ResolveCategory"/> and <see cref="ResolveFlags"/>.
        /// </summary>
        public override CodeObject Resolve(ResolveCategory resolveCategory, ResolveFlags flags)
        {
            if (_typeParameter != null)
                _typeParameter = (SymbolicRef)_typeParameter.Resolve(ResolveCategory.LocalTypeParameter, flags);
            ChildListHelpers.Resolve(_constraints, ResolveCategory.CodeObject, flags);
            return this;
        }

        /// <summary>
        /// Returns true if the code object is an <see cref="UnresolvedRef"/> or has any <see cref="UnresolvedRef"/> children.
        /// </summary>
        public override bool HasUnresolvedRef()
        {
            if (ChildListHelpers.HasUnresolvedRef(_constraints))
                return true;
            if (_typeParameter != null && _typeParameter.HasUnresolvedRef())
                return true;
            return base.HasUnresolvedRef();
        }

        #endregion

        #region /* FORMATTING */

        /// <summary>
        /// True if the code object defaults to starting on a new line.
        /// </summary>
        public override bool IsFirstOnLineDefault
        {
            get { return false; }
        }

        /// <summary>
        /// Determines if the code object only requires a single line for display.
        /// </summary>
        public override bool IsSingleLine
        {
            get
            {
                return (base.IsSingleLine && (_typeParameter == null || (!_typeParameter.IsFirstOnLine && _typeParameter.IsSingleLine))
                    && (_constraints == null || _constraints.Count == 0 || (!_constraints[0].IsFirstOnLine && _constraints.IsSingleLine)));
            }
            set
            {
                base.IsSingleLine = value;
                if (value)
                {
                    if (_typeParameter != null)
                    {
                        _typeParameter.IsFirstOnLine = false;
                        _typeParameter.IsSingleLine = true;
                    }
                    if (_constraints != null && _constraints.Count > 0)
                    {
                        _constraints[0].IsFirstOnLine = false;
                        _constraints.IsSingleLine = true;
                    }
                }
            }
        }

        #endregion

        #region /* RENDERING */

        public override void AsText(CodeWriter writer, RenderFlags flags)
        {
            int newLines = NewLines;
            if (newLines > 0)
            {
                if (!flags.HasFlag(RenderFlags.SuppressNewLine))
                    writer.WriteLines(newLines);
            }
            else if (flags.HasFlag(RenderFlags.PrefixSpace))
                writer.Write(" ");
            flags &= ~RenderFlags.SuppressNewLine;

            RenderFlags passFlags = (flags & RenderFlags.PassMask);
            AsTextBefore(writer, passFlags | RenderFlags.IsPrefix);
            UpdateLineCol(writer, flags);

            // Increase the indent level for any newlines that occur within the statement if the flag is set
            bool increaseIndent = flags.HasFlag(RenderFlags.IncreaseIndent);
            if (increaseIndent)
                writer.BeginIndentOnNewLine(this);

            writer.Write(ParseToken);
            RenderFlags noDescFlags = passFlags & ~(RenderFlags.Description | RenderFlags.ShowParentTypes);
            if (_typeParameter != null)
                _typeParameter.AsText(writer, noDescFlags | RenderFlags.IsPrefix | RenderFlags.PrefixSpace);
            writer.Write(ParseTokenSeparator + " ");
            writer.WriteList(_constraints, noDescFlags, this);
            AsTextEOLComments(writer, flags);

            if (increaseIndent)
                writer.EndIndentation(this);

            AsTextAfter(writer, passFlags | (flags & RenderFlags.NoPostAnnotations));
        }

        public static void AsTextConstraints(CodeWriter writer, ChildList<ConstraintClause> constraints, RenderFlags flags)
        {
            if (constraints != null && constraints.Count > 0)
                writer.WriteList(constraints, flags | RenderFlags.NoItemSeparators | (constraints[0].IsFirstOnLine ? 0 : RenderFlags.PrefixSpace), constraints.Parent);
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)

About the Author

KenBeckett
Software Developer (Senior)
United States United States
I've been writing software since the late 70's, currently focusing mainly on C#.NET. I also like to travel around the world, and I own a Chocolate Factory (sadly, none of my employees are oompa loompas).

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 2 Dec 2012
Article Copyright 2012 by KenBeckett
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid