Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CodeEditor/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory.CSharp-5.5.0/Ast/TypeMembers/EntityDeclaration.cs @ 11700

Last change on this file since 11700 was 11700, checked in by jkarder, 9 years ago

#2077: created branch and added first version

File size: 4.2 KB
Line 
1// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4// software and associated documentation files (the "Software"), to deal in the Software
5// without restriction, including without limitation the rights to use, copy, modify, merge,
6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7// to whom the Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.Collections.Generic;
21using System.Linq;
22
23namespace ICSharpCode.NRefactory.CSharp
24{
25  public abstract class EntityDeclaration : AstNode
26  {
27    public static readonly Role<AttributeSection> AttributeRole = new Role<AttributeSection>("Attribute");
28    public static readonly Role<AttributeSection> UnattachedAttributeRole = new Role<AttributeSection>("UnattachedAttribute");
29    public static readonly Role<CSharpModifierToken> ModifierRole = new Role<CSharpModifierToken>("Modifier");
30    public static readonly Role<AstType> PrivateImplementationTypeRole = new Role<AstType>("PrivateImplementationType", AstType.Null);
31   
32    public override NodeType NodeType {
33      get { return NodeType.Member; }
34    }
35   
36    public abstract NRefactory.TypeSystem.SymbolKind SymbolKind { get; }
37   
38    public AstNodeCollection<AttributeSection> Attributes {
39      get { return base.GetChildrenByRole (AttributeRole); }
40    }
41   
42    public Modifiers Modifiers {
43      get { return GetModifiers(this); }
44      set { SetModifiers(this, value); }
45    }
46   
47    public bool HasModifier (Modifiers mod)
48    {
49      return (Modifiers & mod) == mod;
50    }
51   
52    public IEnumerable<CSharpModifierToken> ModifierTokens {
53      get { return GetChildrenByRole (ModifierRole); }
54    }
55   
56    public virtual string Name {
57      get {
58        return GetChildByRole (Roles.Identifier).Name;
59      }
60      set {
61        SetChildByRole (Roles.Identifier, Identifier.Create (value, TextLocation.Empty));
62      }
63    }
64   
65    public virtual Identifier NameToken {
66      get { return GetChildByRole (Roles.Identifier); }
67      set { SetChildByRole (Roles.Identifier, value); }
68    }
69   
70    public virtual AstType ReturnType {
71      get { return GetChildByRole (Roles.Type); }
72      set { SetChildByRole(Roles.Type, value); }
73    }
74
75    public CSharpTokenNode SemicolonToken {
76      get { return GetChildByRole (Roles.Semicolon); }
77    }
78
79    internal static Modifiers GetModifiers(AstNode node)
80    {
81      Modifiers m = 0;
82      foreach (CSharpModifierToken t in node.GetChildrenByRole (ModifierRole)) {
83        m |= t.Modifier;
84      }
85      return m;
86    }
87   
88    internal static void SetModifiers(AstNode node, Modifiers newValue)
89    {
90      Modifiers oldValue = GetModifiers(node);
91      AstNode insertionPos = node.GetChildrenByRole(AttributeRole).LastOrDefault();
92      foreach (Modifiers m in CSharpModifierToken.AllModifiers) {
93        if ((m & newValue) != 0) {
94          if ((m & oldValue) == 0) {
95            // Modifier was added
96            var newToken = new CSharpModifierToken(TextLocation.Empty, m);
97            node.InsertChildAfter(insertionPos, newToken, ModifierRole);
98            insertionPos = newToken;
99          } else {
100            // Modifier already exists
101            insertionPos = node.GetChildrenByRole(ModifierRole).First(t => t.Modifier == m);
102          }
103        } else {
104          if ((m & oldValue) != 0) {
105            // Modifier was removed
106            node.GetChildrenByRole (ModifierRole).First(t => t.Modifier == m).Remove();
107          }
108        }
109      }
110    }
111   
112    protected bool MatchAttributesAndModifiers (EntityDeclaration o, PatternMatching.Match match)
113    {
114      return (this.Modifiers == Modifiers.Any || this.Modifiers == o.Modifiers) && this.Attributes.DoMatch (o.Attributes, match);
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.