Beispiel #1
0
 public TemplateIntermediateType(DClassLike dc, ISyntaxRegion td,
                                 AbstractType baseType = null, InterfaceType[] baseInterfaces = null,
                                 ReadOnlyCollection <TemplateParameterSymbol> deducedTypes = null)
     : base(dc, baseType, deducedTypes, td)
 {
     this.BaseInterfaces = baseInterfaces;
 }
Beispiel #2
0
            public AbstractType Visit(DClassLike dc)
            {
                var invisibleTypeParams = GetInvisibleTypeParameters(dc);

                switch (dc.ClassType)
                {
                case DTokens.Struct:
                    return(new StructType(dc, typeBase, invisibleTypeParams));

                case DTokens.Union:
                    return(new UnionType(dc, typeBase, invisibleTypeParams));

                case DTokens.Interface:
                case DTokens.Class:
                    return(DResolver.ResolveClassOrInterface(dc, ctxt, typeBase, false, invisibleTypeParams));

                case DTokens.Template:
                    if (dc.ContainsAttribute(DTokens.Mixin))
                    {
                        return(new MixinTemplateType(dc, typeBase, invisibleTypeParams));
                    }
                    return(new TemplateType(dc, typeBase, invisibleTypeParams));

                default:
                    ctxt.LogError(new ResolutionError(dc, "Unknown type (" + DTokens.GetTokenString(dc.ClassType) + ")"));
                    return(null);
                }
            }
Beispiel #3
0
 public TemplateIntermediateType(DClassLike dc, ISyntaxRegion td,
                                 AbstractType baseType, InterfaceType[] baseInterfaces,
                                 IEnumerable <TemplateParameterSymbol> deducedTypes)
     : this(dc, td, baseType, baseInterfaces,
            deducedTypes != null ? new ReadOnlyCollection <TemplateParameterSymbol>(deducedTypes.ToArray()) : null)
 {
 }
		public virtual void Visit(DClassLike n)
		{
			VisitBlock(n);

			foreach (var bc in n.BaseClasses)
				bc.Accept(this);
		}
Beispiel #5
0
 public TemplateIntermediateType(DClassLike dc, ISyntaxRegion td,
                                 AbstractType baseType, InterfaceType[] baseInterfaces,
                                 Dictionary <string, TemplateParameterSymbol> deducedTypes)
     : this(dc, td, baseType, baseInterfaces,
            deducedTypes != null && deducedTypes.Count != 0 ? new ReadOnlyCollection <KeyValuePair <string, TemplateParameterSymbol> >(deducedTypes.ToArray()) : null)
 {
 }
Beispiel #6
0
 public override void Visit(DClassLike n)
 {
     if (CheckNode(n))
     {
         base.Visit(n);
     }
 }
Beispiel #7
0
        bool IterateThrough(DClassLike cls, MemberFilter VisibleMembers, ref bool breakOnNextScope)
        {
            var curWatchedClass = cls;

            // MyClass > BaseA > BaseB > Object
            while (curWatchedClass != null)
            {
                if (curWatchedClass.TemplateParameters != null &&
                    (breakOnNextScope = HandleItems(curWatchedClass.TemplateParameterNodes as IEnumerable <INode>)) && breakImmediately)
                {
                    return(true);
                }

                var ch = PrefilterSubnodes(curWatchedClass);
                if (ch != null)
                {
                    foreach (var m in ch)
                    {
                        var dm2 = m as DNode;
                        var dm3 = m as DMethod;                         // Only show normal & delegate methods
                        if (!CanAddMemberOfType(VisibleMembers, m) || dm2 == null ||
                            (dm3 != null && !(dm3.SpecialType == DMethod.MethodType.Normal || dm3.SpecialType == DMethod.MethodType.Delegate)))
                        {
                            continue;
                        }

                        // Add static and non-private members of all base classes;
                        // Add everything if we're still handling the currently scoped class
                        if ((curWatchedClass == cls || dm2.IsStatic || (!(m is DVariable) || ((DVariable)dm2).IsConst) || !dm2.ContainsAttribute(DTokens.Private)) &&
                            (breakOnNextScope = HandleItem(m)) &&
                            breakImmediately)
                        {
                            return(true);
                        }
                    }
                }

                // 3)
                if (cls.ClassType == DTokens.Class)
                {
                    var tr = DResolver.ResolveBaseClasses(new ClassType(curWatchedClass, curWatchedClass, null), ctxt, true);

                    if (tr.Base is TemplateIntermediateType)
                    {
                        curWatchedClass = ((TemplateIntermediateType)tr.Base).Definition;
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
            return(false);
        }
        // Only for parsing the base class identifiers!
        public override void Visit(DClassLike dc)
        {
            foreach (var bc in dc.BaseClasses)
            {
                bc.Accept(this);
            }

            PushBlock(dc);
        }
Beispiel #9
0
        public override void Visit(DClassLike n)
        {
            if (DoPrimaryIdCheck(n.NameHash))
            {
                AddResult(n);
            }

            base.VisitDNode(n);
        }
Beispiel #10
0
        public override void Visit(DClassLike n)
        {
            byte type;

            if (DoPrimaryIdCheck(n.NameHash, out type))
            {
                AddResult(n, type);
            }

            base.Visit(n);
        }
Beispiel #11
0
        static IEnumerable <AbstractType> GetStructMembers(StructType t, ResolutionContext ctxt)
        {
            if (lastStructMembersEnlisted == null ||
                lastStructHandled != t.Definition)
            {
                lastStructHandled = t.Definition;
                var children = ItemEnumeration.EnumChildren(t, ctxt, MemberFilter.Variables);
                lastStructMembersEnlisted = TypeDeclarationResolver.HandleNodeMatches(children, ctxt, t);
            }

            return(lastStructMembersEnlisted);
        }
Beispiel #12
0
        // Only for parsing the base class identifiers!
        public override void Visit(DClassLike dc)
        {
            var back = ctxt.ScopedBlock;

            if (back != dc)
            {
                ctxt.PushNewScope(dc);
                OnScopedBlockChanged(dc);
            }
            base.Visit(dc);
            if (back != dc)
            {
                ctxt.Pop();
            }
        }
Beispiel #13
0
        public CompletionItemKind Visit(DClassLike n)
        {
            switch (n.ClassType)
            {
            case DTokens.Interface:
                return(CompletionItemKind.Interface);

            case DTokens.Struct:
            case DTokens.Union:
                return(CompletionItemKind.Struct);

            default:
                return(CompletionItemKind.Class);
            }
        }
Beispiel #14
0
        static AbstractType HandleClassLikeMatch(DClassLike dc, ResolutionContext ctxt, object typeBase, bool canResolveBase)
        {
            AbstractType    ret;
            UserDefinedType udt = null;
            var             invisibleTypeParams = GetInvisibleTypeParameters(dc, ctxt);

            switch (dc.ClassType)
            {
            case DTokens.Struct:
                ret = new StructType(dc, typeBase as ISyntaxRegion, invisibleTypeParams);
                break;

            case DTokens.Union:
                ret = new UnionType(dc, typeBase as ISyntaxRegion, invisibleTypeParams);
                break;

            case DTokens.Class:
                udt = new ClassType(dc, typeBase as ISyntaxRegion, null, null, invisibleTypeParams);
                ret = null;
                break;

            case DTokens.Interface:
                udt = new InterfaceType(dc, typeBase as ISyntaxRegion, null, invisibleTypeParams);
                ret = null;
                break;

            case DTokens.Template:
                if (dc.ContainsAttribute(DTokens.Mixin))
                {
                    ret = new MixinTemplateType(dc, typeBase as ISyntaxRegion, invisibleTypeParams);
                }
                else
                {
                    ret = new TemplateType(dc, typeBase as ISyntaxRegion, invisibleTypeParams);
                }
                break;

            default:
                ret = null;
                ctxt.LogError(new ResolutionError(dc, "Unknown type (" + DTokens.GetTokenString(dc.ClassType) + ")"));
                break;
            }
            if (dc.ClassType == DTokens.Class || dc.ClassType == DTokens.Interface)
            {
                ret = canResolveBase ? DResolver.ResolveBaseClasses(udt, ctxt) : udt;
            }
            return(ret);
        }
        public override void Visit(DClassLike n)
        {
            if (!halt)
            {
                handlesBaseClasses = true;
                handledClass       = n;
                foreach (var bc in n.BaseClasses)
                {
                    bc.Accept(this);
                }
                handlesBaseClasses = false;

                if (!halt)
                {
                    VisitBlock(n);
                }
            }
        }
Beispiel #16
0
        public IconId Visit(DClassLike n)
        {
            switch (n.ClassType)
            {
            case DTokens.Template:
                return(iconIdWithProtectionAttr(n, "template"));

            default:
                return(classIconIdWithProtectionAttr(n));

            case DTokens.Union:
                return(iconIdWithProtectionAttr(n, "union"));

            case DTokens.Struct:
                return(iconIdWithProtectionAttr(n, "struct"));

            case DTokens.Interface:
                return(iconIdWithProtectionAttr(n, "interface"));
            }
        }
Beispiel #17
0
            public string Visit(DClassLike dClassLike)
            {
                switch (dClassLike.ClassType)
                {
                case DTokens.Struct:
                    return("STRU");

                default:
                    return("CLSS");

                case DTokens.Interface:
                    return("IFAC");

                case DTokens.Template:
                    return(TemplateType);

                case DTokens.Union:
                    return("UNIO");
                }
            }
        void S(DClassLike dc, StringBuilder sb, DeducedTypeDictionary deducedTypes = null)
        {
            AppendAttributes(dc, sb);

            sb.Append(DCodeToMarkup(DTokens.GetTokenString(dc.ClassType))).Append(' ');

            sb.Append(DCodeToMarkup(dc.Name));

            AppendTemplateParams(dc, sb, -1, deducedTypes);

            if (dc.BaseClasses != null && dc.BaseClasses.Count != 0)
            {
                sb.AppendLine(" : ");
                sb.Append(" ");
                foreach (var bc in dc.BaseClasses)
                {
                    sb.Append(' ').Append(DCodeToMarkup(bc.ToString())).Append(',');
                }

                RemoveLastChar(sb, ',');
            }

            AppendConstraint(dc, sb);
        }
Beispiel #19
0
 public MixinTemplateType(DClassLike dc, ISyntaxRegion td, IEnumerable <TemplateParameterSymbol> inheritedTypeParams = null) : base(dc, td, inheritedTypeParams)
 {
 }
Beispiel #20
0
 public InterfaceType(DClassLike dc, ISyntaxRegion td,
                      InterfaceType[] baseInterfaces = null,
                      IEnumerable <TemplateParameterSymbol> deducedTypes = null)
     : base(dc, td, null, baseInterfaces, deducedTypes)
 {
 }
Beispiel #21
0
 public InterfaceType(DClassLike dc, ISyntaxRegion td,
                      InterfaceType[] baseInterfaces,
                      ReadOnlyCollection <TemplateParameterSymbol> deducedTypes)
     : base(dc, td, null, baseInterfaces, deducedTypes)
 {
 }
Beispiel #22
0
		IExpression NewExpression(IBlockNode Scope = null)
		{
			Expect(New);
			var startLoc = t.Location;

			IExpression[] newArgs = null;
			// NewArguments
			if (laKind == (OpenParenthesis))
			{
				Step();
				if (laKind != (CloseParenthesis))
					newArgs = ArgumentList(Scope).ToArray();
				Expect(CloseParenthesis);
			}

			/*
			 * If there occurs a class keyword here, interpretate it as an anonymous class definition
			 * http://digitalmars.com/d/2.0/expression.html#NewExpression
			 * 
			 * NewArguments ClassArguments BaseClasslist_opt { DeclDefs } 
			 * 
			 * http://digitalmars.com/d/2.0/class.html#anonymous
			 * 
				NewAnonClassExpression:
					new PerenArgumentListopt class PerenArgumentList_opt SuperClass_opt InterfaceClasses_opt ClassBody

				PerenArgumentList:
					(ArgumentList)
			 * 
			 */
			if (laKind == (Class))
			{
				Step();
				var ac = new AnonymousClassExpression(); LastParsedObject = ac;
				ac.NewArguments = newArgs;

				// ClassArguments
				if (laKind == (OpenParenthesis))
				{
					Step();
					if (laKind == (CloseParenthesis))
						Step();
					else
						ac.ClassArguments = ArgumentList(Scope).ToArray();
				}

				var anclass = new DClassLike(Class) { IsAnonymousClass=true };
				LastParsedObject = anclass;

				anclass.Name = "(Anonymous Class)";

				// BaseClasslist_opt
				if (laKind == (Colon))
				{
					//TODO : Add base classes to expression
					BaseClassList(anclass);
				}
				// SuperClass_opt InterfaceClasses_opt
				else if (laKind != OpenCurlyBrace)
					BaseClassList(anclass,false);

				ClassBody(anclass);

				ac.AnonymousClass = anclass;

				ac.Location = startLoc;
				ac.EndLocation = t.EndLocation;

				if (Scope != null)
					Scope.Add(ac.AnonymousClass);

				return ac;
			}

			// NewArguments Type
			else
			{
				var nt = BasicType();

				while (IsBasicType2())
				{
					var bt=BasicType2();
					if (bt == null)
						break;
					bt.InnerDeclaration = nt;
					nt = bt;
				}

				var initExpr = new NewExpression()
				{
					NewArguments = newArgs,
					Type=nt,
					Location=startLoc
				};
				if(!IsEOF)
					LastParsedObject = initExpr;

				var args = new List<IExpression>();

				var ad=nt as ArrayDecl;

				if ((ad == null || ad.ClampsEmpty) && laKind == OpenParenthesis)
				{
					Step();
					if (laKind != CloseParenthesis)
						args = ArgumentList(Scope);

					if (Expect(CloseParenthesis))
						initExpr.EndLocation = t.EndLocation;
					else
						initExpr.EndLocation = CodeLocation.Empty;

					if (ad != null)
					{
						if (args.Count == 0)
						{
							SemErr(CloseParenthesis, "Size for the rightmost array dimension needed");

							initExpr.EndLocation = t.EndLocation;
							return initExpr;
						}

						while (ad != null)
						{
							if (args.Count == 0)
								break;

							ad.ClampsEmpty = false;
							ad.KeyType = null;
							ad.KeyExpression = args[args.Count - 1];

							args.RemoveAt(args.Count - 1);

							ad = ad.InnerDeclaration as ArrayDecl;
						}
					}
				}
				else
					initExpr.EndLocation = t.EndLocation;

				ad = nt as ArrayDecl;

				if (ad != null && ad.KeyExpression == null)
				{
					if (ad.KeyType != null)
						SemErr(ad.KeyType is DTokenDeclaration ? (ad.KeyType as DTokenDeclaration).Token : CloseSquareBracket, "Size of array expected, not type " + ad.KeyType);
				}

				initExpr.Arguments = args.ToArray();

				return initExpr;
			}
		}
Beispiel #23
0
 public InterfaceType(DClassLike dc, ISyntaxRegion td,
                      InterfaceType[] baseInterfaces = null,
                      Dictionary <string, TemplateParameterSymbol> deducedTypes = null)
     : base(dc, td, null, baseInterfaces, deducedTypes)
 {
 }
		public static ISyntaxRegion FindCurrentCaretContext(IEditorData editor, 
			out ParserTrackerVariables trackerVariables, 
			ref IBlockNode currentScope, 
			out IStatement currentStatement)
		{
			if(currentScope == null)
				currentScope = DResolver.SearchBlockAt (editor.SyntaxTree, editor.CaretLocation, out currentStatement);

			if (currentScope == null) {
				trackerVariables = null;
				currentStatement = null;
				return null;
			}

			bool ParseDecl = false;

			int blockStart = 0;
			var blockStartLocation = currentScope != null ? currentScope.BlockStartLocation : editor.CaretLocation;

			if (currentScope is DMethod)
			{
				var block = (currentScope as DMethod).GetSubBlockAt(editor.CaretLocation);

				if (block != null)
					blockStart = DocumentHelper.GetOffsetByRelativeLocation (editor.ModuleCode, editor.CaretLocation, editor.CaretOffset, blockStartLocation = block.Location);
				else {
					currentScope = currentScope.Parent as IBlockNode;
					return FindCurrentCaretContext (editor, out trackerVariables, ref currentScope, out currentStatement);
				}
			}
			else if (currentScope != null)
			{
				if (currentScope.BlockStartLocation.IsEmpty || (editor.CaretLocation < currentScope.BlockStartLocation && editor.CaretLocation > currentScope.Location))
				{
					ParseDecl = true;
					blockStart = DocumentHelper.GetOffsetByRelativeLocation(editor.ModuleCode, editor.CaretLocation, editor.CaretOffset, blockStartLocation = currentScope.Location);
				}
				else
					blockStart = DocumentHelper.GetOffsetByRelativeLocation(editor.ModuleCode, editor.CaretLocation, editor.CaretOffset, currentScope.BlockStartLocation);
			}

			if (blockStart >= 0 && editor.CaretOffset - blockStart > 0)
				using (var sr = new Misc.StringView(editor.ModuleCode, blockStart, editor.CaretOffset - blockStart))
				{
					var psr = DParser.Create(sr);

					/*					 Deadly important! For correct resolution behaviour, 
					 * it is required to set the parser virtually to the blockStart position, 
					 * so that everything using the returned object is always related to 
					 * the original code file, not our code extraction!
					 */
					psr.Lexer.SetInitialLocation(blockStartLocation);

					ISyntaxRegion ret = null;

					if (currentScope == null)
						ret = psr.Parse();
					else if (currentScope is DMethod)
					{
						psr.Step();
						var dm = currentScope as DMethod;
						dm.Clear();

						if ((dm.SpecialType & DMethod.MethodType.Lambda) != 0 &&
							psr.Lexer.LookAhead.Kind != DTokens.OpenCurlyBrace) {
							psr.LambdaSingleStatementBody (dm);
							ret = dm.Body;
						} else {
							var methodRegion = DTokens.Body;

							if (dm.In != null && blockStartLocation == dm.In.Location)
								methodRegion = DTokens.In;

							if (dm.Out != null && blockStartLocation == dm.Out.Location)
								methodRegion = DTokens.Out;

							var newBlock = psr.BlockStatement (currentScope);
							ret = newBlock;

							switch (methodRegion) {
								case DTokens.Body:
									newBlock.EndLocation = dm.Body.EndLocation;
									dm.Body = newBlock;
									break;
								case DTokens.In:
									newBlock.EndLocation = dm.In.EndLocation;
									dm.In = newBlock;
									break;
								case DTokens.Out:
									newBlock.EndLocation = dm.Out.EndLocation;
									dm.Out = newBlock;
									break;
							}
						}
					}
					else if (currentScope is DModule)
						ret = psr.Root();
					else
					{
						psr.Step();
						if (ParseDecl)
						{
							var ret2 = psr.Declaration(currentScope);

							if (ret2 != null && ret2.Length > 0)
								ret = ret2[0];
						}
						else if (currentScope is DClassLike)
						{
							var t = new DClassLike((currentScope as DClassLike).ClassType);
							t.AssignFrom(currentScope);
							t.Clear();
							psr.ClassBody(t);
							ret = t;
						}
						else if (currentScope is DEnum)
						{
							var t = new DEnum();
							t.AssignFrom(currentScope);
							t.Clear();
							psr.EnumBody(t);
							ret = t;
						}
					}

					currentScope = DResolver.SearchBlockAt (currentScope, 
						psr.Lexer.CurrentToken != null ? psr.Lexer.CurrentToken.EndLocation : editor.CaretLocation, 
						out currentStatement);
					trackerVariables = psr.TrackerVariables;
					return ret;
				}

			trackerVariables = null;
			currentStatement = null;
			return null;
		}
Beispiel #25
0
        protected void SerializeDNode(DNode node, StringBuilder sb, int level)
        {
            StringBuilder indent = new StringBuilder();

            for (int i = 0; i < level; i++)
            {
                indent.Append('\t');
            }
            SerializeCodeLocation(node.StartLocation, sb);
            sb.Append("-");
            SerializeCodeLocation(node.EndLocation, sb);
            sb.Append(" ");

            if (node is DModule)
            {
                DModule module = node as DModule;
                sb.Append(indent).Append("ML:").Append(module.ModuleName);
                foreach (ITypeDeclaration td in module.Imports.Keys)
                {
                    sb.Append(indent).Append("~IM:").Append(td.ToString());
                }
                sb.AppendLine();
            }
            else if (node is DMethod)
            {
                DMethod method = node as DMethod;
                sb.Append(indent).Append("MD:").Append(method.Type != null ? method.Type.ToString() : "<NULL>").Append("~").AppendLine(method.Name);
                foreach (DNode paramNode in method.Parameters)
                {
                    SerializeDNode(paramNode, sb, level + 1);
                }
            }
            else if (node is DClassLike)
            {
                DClassLike clss = node as DClassLike;
                sb.Append(indent).Append("CLS:").Append(clss.ClassType).Append("~").AppendLine(clss.Name);
            }
            else if (node is DVariable)
            {
                DVariable variable = node as DVariable;
                sb.Append(indent).Append("VAR:").Append((variable.Type == null) ? "<NULL>" : variable.Type.ToString()).Append("~").Append(variable.Name);
                if (variable.Initializer != null)
                {
                    sb.Append(indent).Append("~INIT:").Append(variable.Initializer.ToString());
                }
                sb.AppendLine();
            }
            else
            {
                object o = node;
            }

            if (node is DStatementBlock)
            {
                DStatementBlock block = node as DStatementBlock;
                //if (block.Expression != null)
                //    sb.Append(indent).Append("EXP:").Append(block.Expression.ToString());
                foreach (DNode childNode in block)
                {
                    SerializeDNode(childNode, sb, level + 1);
                }
            }
            else if (node is DBlockStatement)
            {
                DBlockStatement block = node as DBlockStatement;
                foreach (DNode childNode in block.Children)
                {
                    SerializeDNode(childNode, sb, level + 1);
                }
            }
        }
Beispiel #26
0
 public MixinTemplateType(DClassLike dc, ISyntaxRegion td, ReadOnlyCollection <TemplateParameterSymbol> inheritedTypeParams = null) : base(dc, td, inheritedTypeParams)
 {
 }
Beispiel #27
0
 public TemplateType(DClassLike dc, ISyntaxRegion td, Dictionary <string, TemplateParameterSymbol> inheritedTypeParams = null) : base(dc, td, null, null, inheritedTypeParams)
 {
 }
Beispiel #28
0
        /// <summary>
        /// Takes the class passed via the tr, and resolves its base class and/or implemented interfaces.
        /// Also usable for enums.
        ///
        /// Never returns null. Instead, the original 'tr' object will be returned if no base class was resolved.
        /// Will clone 'tr', whereas the new object will contain the base class.
        /// </summary>
        public static TemplateIntermediateType ResolveClassOrInterface(DClassLike dc, ResolutionContext ctxt, ISyntaxRegion instanceDeclaration, bool ResolveFirstBaseIdOnly = false, IEnumerable <TemplateParameterSymbol> extraDeducedTemplateParams = null)
        {
            if (parsedClassInstanceDecls == null)
            {
                parsedClassInstanceDecls = new List <ISyntaxRegion> ();
            }

            switch (dc.ClassType)
            {
            case DTokens.Class:
            case DTokens.Interface:
                break;

            default:
                if (dc.BaseClasses.Count != 0)
                {
                    ctxt.LogError(dc, "Only classes and interfaces may inherit from other classes/interfaces");
                }
                return(null);
            }

            bool isClass = dc.ClassType == DTokens.Class;

            if (bcStack > 6 || (instanceDeclaration != null && parsedClassInstanceDecls.Contains(instanceDeclaration)))
            {
                return(isClass ? new ClassType(dc, instanceDeclaration, null) as TemplateIntermediateType : new InterfaceType(dc, instanceDeclaration));
            }

            if (instanceDeclaration != null)
            {
                parsedClassInstanceDecls.Add(instanceDeclaration);
            }
            bcStack++;

            var deducedTypes = new DeducedTypeDictionary(dc);
            var tix          = instanceDeclaration as TemplateInstanceExpression;

            if (tix != null && (ctxt.Options & ResolutionOptions.NoTemplateParameterDeduction) == 0)
            {
                bool hasUndeterminedArgs;
                var  givenTemplateArguments = TemplateInstanceHandler.PreResolveTemplateArgs(tix, ctxt, out hasUndeterminedArgs);

                if (!TemplateInstanceHandler.DeduceParams(givenTemplateArguments, false, ctxt, null, dc, deducedTypes))
                {
                    parsedClassInstanceDecls.Remove(instanceDeclaration);
                    bcStack--;
                    return(null);
                }
            }

            if (extraDeducedTemplateParams != null)
            {
                foreach (var tps in extraDeducedTemplateParams)
                {
                    deducedTypes[tps.Parameter] = tps;
                }
            }


            if (dc.BaseClasses == null || dc.BaseClasses.Count < 1)
            {
                parsedClassInstanceDecls.Remove(instanceDeclaration);
                bcStack--;

                // The Object class has no further base class;
                // Normal class instances have the object as base class;
                // Interfaces must not have any default base class/interface
                return(isClass ? new ClassType(dc, instanceDeclaration, dc.NameHash != ObjectNameHash ? ctxt.ParseCache.ObjectClassResult : null, null, deducedTypes.Count != 0 ? deducedTypes.ToReadonly() : null) :
                       new InterfaceType(dc, instanceDeclaration, null, deducedTypes.Count != 0 ? deducedTypes.ToReadonly() : null) as TemplateIntermediateType);
            }


            #region Base class & interface resolution
            AbstractType[] res;
            var            pop = ctxt.ScopedBlock != dc.Parent;
            if (pop)
            {
                ctxt.PushNewScope(dc.Parent as IBlockNode);
            }

            foreach (var kv in deducedTypes)
            {
                ctxt.CurrentContext.DeducedTemplateParameters[kv.Key] = kv.Value;
            }

            TemplateIntermediateType baseClass = null;
            var interfaces = new List <InterfaceType>();
            try
            {
                for (int i = 0; i < (ResolveFirstBaseIdOnly ? 1 : dc.BaseClasses.Count); i++)
                {
                    var type = dc.BaseClasses[i];

                    // If there's an explicit 'Object' inheritance, also return the pre-resolved object class
                    if (type is IdentifierDeclaration &&
                        (type as IdentifierDeclaration).IdHash == ObjectNameHash)
                    {
                        if (baseClass != null)
                        {
                            ctxt.LogError(new ResolutionError(dc, "Class must not have two base classes"));
                            continue;
                        }
                        else if (i != 0)
                        {
                            ctxt.LogError(new ResolutionError(dc, "The base class name must preceed base interfaces"));
                            continue;
                        }

                        baseClass = ctxt.ParseCache.ObjectClassResult;
                        continue;
                    }

                    if (type == null || (type is IdentifierDeclaration && (type as IdentifierDeclaration).IdHash == dc.NameHash) || dc.NodeRoot == dc)
                    {
                        ctxt.LogError(new ResolutionError(dc, "A class cannot inherit from itself"));
                        continue;
                    }

                    res = DResolver.StripAliasSymbols(TypeDeclarationResolver.Resolve(type, ctxt));

                    ctxt.CheckForSingleResult(res, type);

                    if (res != null && res.Length != 0)
                    {
                        var r = res[0];
                        if (r is ClassType || r is TemplateType)
                        {
                            if (!isClass)
                            {
                                ctxt.LogError(new ResolutionError(type, "An interface cannot inherit from non-interfaces"));
                            }
                            else if (i == 0)
                            {
                                baseClass = r as TemplateIntermediateType;
                            }
                            else
                            {
                                ctxt.LogError(new ResolutionError(dc, "The base " + (r is ClassType ?  "class" : "template") + " name must preceed base interfaces"));
                            }
                        }
                        else if (r is InterfaceType)
                        {
                            interfaces.Add(r as InterfaceType);

                            if (isClass && dc.NameHash != ObjectNameHash && baseClass == null)
                            {
                                baseClass = ctxt.ParseCache.ObjectClassResult;
                            }
                        }
                        else
                        {
                            ctxt.LogError(new ResolutionError(type, "Resolved class is neither a class nor an interface"));
                            continue;
                        }
                    }
                }
            }
            finally
            {
                bcStack--;
                parsedClassInstanceDecls.Remove(instanceDeclaration);
            }

            if (pop)
            {
                ctxt.Pop();
            }
            else
            {
                foreach (var kv in deducedTypes)                 // May be backup old tps?
                {
                    ctxt.CurrentContext.DeducedTemplateParameters.Remove(kv.Key);
                }
            }

            #endregion

            if (isClass)
            {
                return(new ClassType(dc, instanceDeclaration, baseClass, interfaces.Count == 0 ? null : interfaces.ToArray(), deducedTypes.Count != 0 ? deducedTypes.ToReadonly() : null));
            }

            return(new InterfaceType(dc, instanceDeclaration, interfaces.Count == 0 ? null : interfaces.ToArray(), deducedTypes.Count != 0 ? deducedTypes.ToReadonly() : null));
        }
		static AbstractType HandleClassLikeMatch (DClassLike dc, ResolutionContext ctxt, object typeBase, bool canResolveBase)
		{
			AbstractType ret;
			UserDefinedType udt = null;
			var invisibleTypeParams = GetInvisibleTypeParameters (dc, ctxt);

			switch (dc.ClassType) {
				case DTokens.Struct:
					ret = new StructType (dc, typeBase as ISyntaxRegion, invisibleTypeParams);
					break;
				case DTokens.Union:
					ret = new UnionType (dc, typeBase as ISyntaxRegion, invisibleTypeParams);
					break;
				case DTokens.Class:
					udt = new ClassType (dc, typeBase as ISyntaxRegion, null, null, invisibleTypeParams);
					ret = null;
					break;
				case DTokens.Interface:
					udt = new InterfaceType (dc, typeBase as ISyntaxRegion, null, invisibleTypeParams);
					ret = null;
					break;
				case DTokens.Template:
					if (dc.ContainsAttribute (DTokens.Mixin))
						ret = new MixinTemplateType (dc, typeBase as ISyntaxRegion, invisibleTypeParams);
					else
						ret = new TemplateType (dc, typeBase as ISyntaxRegion, invisibleTypeParams);
					break;
				default:
					ret = null;
					ctxt.LogError (new ResolutionError (dc, "Unknown type (" + DTokens.GetTokenString (dc.ClassType) + ")"));
					break;
			}
			if (dc.ClassType == DTokens.Class || dc.ClassType == DTokens.Interface)
				ret = canResolveBase ? DResolver.ResolveBaseClasses (udt, ctxt) : udt;
			return ret;
		}
Beispiel #30
0
		/*
         * American beer is like sex on a boat - F*****g close to water;)
         */

		private INode TemplateDeclaration(INode Parent)
		{
			var startLoc = la.Location;
			
			// TemplateMixinDeclaration
			Modifier mixinMod;
			if (laKind == Mixin){
				Step();
				mixinMod = new Modifier(Mixin){ Location = t.Location, EndLocation = t.EndLocation };
			}
			else
				mixinMod = null;
			
			Expect(Template);
			var dc = new DClassLike(Template) {
				Description=GetComments(),
				Location=startLoc,
				Parent=Parent
			};
			LastParsedObject = dc;

			ApplyAttributes(dc);

			if (mixinMod != null)
				dc.Attributes.Add(mixinMod);

			if (Expect(Identifier))
			{
				dc.Name = t.Value;
				dc.NameLocation = t.Location;
			}
			else if (IsEOF)
				ExpectingNodeName = true;

			TemplateParameterList(dc);

			if (laKind == (If))
				Constraint(dc);

			// [Must not contain a base class list]

			ClassBody(dc);

			return dc;
		}
Beispiel #31
0
		private IBlockNode InterfaceDeclaration(INode Parent)
		{
			Expect(Interface);
			var dc = new DClassLike() { 
				Location = t.Location, 
				Description = GetComments(),
                ClassType= DTokens.Interface,
				Parent=Parent
			};
			LastParsedObject = dc;

			ApplyAttributes(dc);

			if (!Expect(Identifier))
			{
				if (IsEOF)
					ExpectingNodeName = true;
				return dc;
			}
			dc.Name = t.Value;
			dc.NameLocation = t.Location;

			if (laKind == (OpenParenthesis))
				TemplateParameterList(dc);

			if (laKind == (If))
				Constraint(dc);

			if (laKind == (Colon))
				BaseClassList(dc);

			// Empty interfaces are allowed
			if (laKind == Semicolon)
				Step();
			else
				ClassBody(dc);

			dc.EndLocation = t.EndLocation;
			return dc;
		}
Beispiel #32
0
		private void BaseClassList(DClassLike dc,bool ExpectColon=true)
		{
			if (ExpectColon) Expect(Colon);

			var ret = new List<ITypeDeclaration>();
			dc.BaseClasses = ret;

			bool init = true;
			while (init || laKind == (Comma))
			{
				if (!init) Step();
				init = false;
				if (IsProtectionAttribute() && laKind != (Protected))
					Step();

				var ids=IdentifierList();
				if (ids != null)
					ret.Add(ids);
			}

			if (IsEOF)
			{
				if (ret.Count != 0)
					LastParsedObject = ret[ret.Count - 1];
				TrackerVariables.IsParsingBaseClassList = true;
				TrackerVariables.InitializedNode = dc;
			}
		}
Beispiel #33
0
		private INode ClassDeclaration(INode Parent)
		{
			Expect(Class);

			var dc = new DClassLike(Class) { 
				Location = t.Location,
				Description=GetComments(),
				Parent=Parent
			};
			LastParsedObject = dc;

			ApplyAttributes(dc);

			if (Expect(Identifier))
			{
				dc.Name = t.Value;
				dc.NameLocation = t.Location;
			}
			else if (IsEOF)
				ExpectingNodeName = true;

			if (laKind == (OpenParenthesis))
				TemplateParameterList(dc);

			// Constraints
			// http://dlang.org/template.html#ClassTemplateDeclaration
			if (Constraint (dc)) { // Constraint_opt BaseClassList_opt
				if (laKind == (Colon))
					BaseClassList (dc);
			} else if (laKind == (Colon)) { // Constraint_opt BaseClassList_opt
				BaseClassList (dc);
				Constraint (dc);
			}

			ClassBody(dc);

			dc.EndLocation = t.EndLocation;
			return dc;
		}
Beispiel #34
0
		private INode AggregateDeclaration(INode Parent)
		{
			var classType = laKind;
			if (!(classType == Union || classType == Struct))
				SynErr(t.Kind, "union or struct required");
			Step();

			var ret = new DClassLike(t.Kind) { 
				Location = t.Location, 
				Description = GetComments(),
                ClassType=classType,
				Parent=Parent
			};
			LastParsedObject = ret;
			ApplyAttributes(ret);

			// Allow anonymous structs&unions
			if (laKind == Identifier)
			{
				Expect(Identifier);
				ret.Name = t.Value;
				ret.NameLocation = t.Location;
			}
			else if (IsEOF)
				ExpectingNodeName = true;

			if (laKind == (Semicolon))
			{
				Step();
				return ret;
			}

			// StructTemplateDeclaration
			if (laKind == (OpenParenthesis))
			{
				TemplateParameterList(ret);

				// Constraint[opt]
				if (laKind == (If))
					Constraint(ret);
			}

			ClassBody(ret);

			return ret;
		}
Beispiel #35
0
 public TemplateType(DClassLike dc, ISyntaxRegion td, ReadOnlyCollection <KeyValuePair <string, TemplateParameterSymbol> > inheritedTypeParams = null) : base(dc, td, null, null, inheritedTypeParams)
 {
 }
Beispiel #36
0
		public InterfaceType(DClassLike dc, ISyntaxRegion td, 
			InterfaceType[] baseInterfaces=null,
			IEnumerable<TemplateParameterSymbol> deducedTypes = null) 
			: base(dc, td, null, baseInterfaces, deducedTypes) {}
Beispiel #37
0
		public MixinTemplateType(DClassLike dc, ISyntaxRegion td, IEnumerable<TemplateParameterSymbol> inheritedTypeParams = null) : base(dc, td, inheritedTypeParams) { }
Beispiel #38
0
		public TemplateIntermediateType(DClassLike dc, ISyntaxRegion td, 
			AbstractType baseType = null, InterfaceType[] baseInterfaces = null,
			ReadOnlyCollection<TemplateParameterSymbol> deducedTypes = null)
			: base(dc, baseType, deducedTypes, td)
		{
			this.BaseInterfaces = baseInterfaces;
		}
Beispiel #39
0
 public InstanceValue(DClassLike Class, AbstractType ClassType)
     : base(Class, ClassType)
 {
 }
		// Only for parsing the base class identifiers!
		public override void Visit (DClassLike dc)
		{
			var back = ctxt.ScopedBlock;
			if (back != dc) {
				ctxt.PushNewScope (dc);
				OnScopedBlockChanged (dc);
			}
			base.Visit (dc); 
			if(back != dc)
				ctxt.Pop ();
		}
Beispiel #41
0
		public UnionType(DClassLike dc, ISyntaxRegion td, IEnumerable<TemplateParameterSymbol> deducedTypes = null) : base(dc, td, null, null, deducedTypes) { }
		public override void Visit(DClassLike n)
		{
			base.Visit(n);
		}
Beispiel #43
0
		public InterfaceType(DClassLike dc, ISyntaxRegion td,
			InterfaceType[] baseInterfaces,
			ReadOnlyCollection<TemplateParameterSymbol> deducedTypes)
			: base(dc, td, null, baseInterfaces, deducedTypes) { }
		public override void Visit (DClassLike n)
		{
			if (DoPrimaryIdCheck (n.NameHash))
				AddResult (n);

			base.VisitDNode (n);
		}
Beispiel #45
0
		public MixinTemplateType(DClassLike dc, ISyntaxRegion td, ReadOnlyCollection<TemplateParameterSymbol> inheritedTypeParams = null) : base(dc, td, inheritedTypeParams) { }
		public InstanceValue(DClassLike Class, AbstractType ClassType)
			: base(Class, ClassType)
		{

		}
Beispiel #47
0
		public TemplateIntermediateType(DClassLike dc, ISyntaxRegion td, 
			AbstractType baseType, InterfaceType[] baseInterfaces,
			IEnumerable<TemplateParameterSymbol> deducedTypes)
			: this(dc,td, baseType,baseInterfaces,
			deducedTypes != null ? new ReadOnlyCollection<TemplateParameterSymbol>(deducedTypes.ToArray()) : null)
		{ }
Beispiel #48
0
 public UnionType(DClassLike dc, ISyntaxRegion td, IEnumerable <TemplateParameterSymbol> deducedTypes = null) : base(dc, td, null, null, deducedTypes)
 {
 }
        public static ISyntaxRegion FindCurrentCaretContext(IEditorData editor,
                                                            out ParserTrackerVariables trackerVariables,
                                                            ref IBlockNode currentScope,
                                                            out IStatement currentStatement)
        {
            if (currentScope == null)
            {
                currentScope = DResolver.SearchBlockAt(editor.SyntaxTree, editor.CaretLocation, out currentStatement);
            }

            if (currentScope == null)
            {
                trackerVariables = null;
                currentStatement = null;
                return(null);
            }

            bool ParseDecl = false;

            int blockStart         = 0;
            var blockStartLocation = currentScope != null ? currentScope.BlockStartLocation : editor.CaretLocation;

            if (currentScope is DMethod)
            {
                var block = (currentScope as DMethod).GetSubBlockAt(editor.CaretLocation);

                if (block != null)
                {
                    blockStart = DocumentHelper.GetOffsetByRelativeLocation(editor.ModuleCode, editor.CaretLocation, editor.CaretOffset, blockStartLocation = block.Location);
                }
                else
                {
                    currentScope = currentScope.Parent as IBlockNode;
                    return(FindCurrentCaretContext(editor, out trackerVariables, ref currentScope, out currentStatement));
                }
            }
            else if (currentScope != null)
            {
                if (currentScope.BlockStartLocation.IsEmpty || (editor.CaretLocation < currentScope.BlockStartLocation && editor.CaretLocation > currentScope.Location))
                {
                    ParseDecl  = true;
                    blockStart = DocumentHelper.GetOffsetByRelativeLocation(editor.ModuleCode, editor.CaretLocation, editor.CaretOffset, blockStartLocation = currentScope.Location);
                }
                else
                {
                    blockStart = DocumentHelper.GetOffsetByRelativeLocation(editor.ModuleCode, editor.CaretLocation, editor.CaretOffset, currentScope.BlockStartLocation);
                }
            }

            if (blockStart >= 0 && editor.CaretOffset - blockStart > 0)
            {
                using (var sr = new Misc.StringView(editor.ModuleCode, blockStart, editor.CaretOffset - blockStart))
                {
                    var psr = DParser.Create(sr);

                    /*					 Deadly important! For correct resolution behaviour,
                     * it is required to set the parser virtually to the blockStart position,
                     * so that everything using the returned object is always related to
                     * the original code file, not our code extraction!
                     */
                    psr.Lexer.SetInitialLocation(blockStartLocation);

                    ISyntaxRegion ret = null;

                    if (currentScope == null)
                    {
                        ret = psr.Parse();
                    }
                    else if (currentScope is DMethod)
                    {
                        psr.Step();
                        var dm = currentScope as DMethod;
                        dm.Clear();

                        if ((dm.SpecialType & DMethod.MethodType.Lambda) != 0 &&
                            psr.Lexer.LookAhead.Kind != DTokens.OpenCurlyBrace)
                        {
                            psr.LambdaSingleStatementBody(dm);
                            ret = dm.Body;
                        }
                        else
                        {
                            var methodRegion = DTokens.Body;

                            if (dm.In != null && blockStartLocation == dm.In.Location)
                            {
                                methodRegion = DTokens.In;
                            }

                            if (dm.Out != null && blockStartLocation == dm.Out.Location)
                            {
                                methodRegion = DTokens.Out;
                            }

                            var newBlock = psr.BlockStatement(currentScope);
                            ret = newBlock;

                            switch (methodRegion)
                            {
                            case DTokens.Body:
                                newBlock.EndLocation = dm.Body.EndLocation;
                                dm.Body = newBlock;
                                break;

                            case DTokens.In:
                                newBlock.EndLocation = dm.In.EndLocation;
                                dm.In = newBlock;
                                break;

                            case DTokens.Out:
                                newBlock.EndLocation = dm.Out.EndLocation;
                                dm.Out = newBlock;
                                break;
                            }
                        }
                    }
                    else if (currentScope is DModule)
                    {
                        ret = psr.Root();
                    }
                    else
                    {
                        psr.Step();
                        if (ParseDecl)
                        {
                            var ret2 = psr.Declaration(currentScope);

                            if (ret2 != null && ret2.Length > 0)
                            {
                                ret = ret2[0];
                            }
                        }
                        else if (currentScope is DClassLike)
                        {
                            var t = new DClassLike((currentScope as DClassLike).ClassType);
                            t.AssignFrom(currentScope);
                            t.Clear();
                            psr.ClassBody(t);
                            ret = t;
                        }
                        else if (currentScope is DEnum)
                        {
                            var t = new DEnum();
                            t.AssignFrom(currentScope);
                            t.Clear();
                            psr.EnumBody(t);
                            ret = t;
                        }
                    }

                    currentScope = DResolver.SearchBlockAt(currentScope,
                                                           psr.Lexer.CurrentToken != null ? psr.Lexer.CurrentToken.EndLocation : editor.CaretLocation,
                                                           out currentStatement);
                    trackerVariables = psr.TrackerVariables;
                    return(ret);
                }
            }

            trackerVariables = null;
            currentStatement = null;
            return(null);
        }
Beispiel #50
0
 public ClassType(DClassLike dc, ISyntaxRegion td,
                  TemplateIntermediateType baseType, InterfaceType[] baseInterfaces,
                  ReadOnlyCollection <KeyValuePair <string, TemplateParameterSymbol> > deducedTypes)
     : base(dc, td, baseType, baseInterfaces, deducedTypes)
 {
 }