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/PrimitiveType.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.3 KB
Line 
1//
2// FullTypeName.cs
3//
4// Author:
5//       Mike Krüger <mkrueger@novell.com>
6//
7// Copyright (c) 2010 Novell, Inc (http://www.novell.com)
8//
9// Permission is hereby granted, free of charge, to any person obtaining a copy
10// of this software and associated documentation files (the "Software"), to deal
11// in the Software without restriction, including without limitation the rights
12// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13// copies of the Software, and to permit persons to whom the Software is
14// furnished to do so, subject to the following conditions:
15//
16// The above copyright notice and this permission notice shall be included in
17// all copies or substantial portions of the Software.
18//
19// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25// THE SOFTWARE.
26using System;
27using System.Collections.Generic;
28using System.Linq;
29using ICSharpCode.NRefactory.CSharp.Resolver;
30using ICSharpCode.NRefactory.TypeSystem;
31using ICSharpCode.NRefactory.TypeSystem.Implementation;
32
33namespace ICSharpCode.NRefactory.CSharp
34{
35  public class PrimitiveType : AstType
36  {
37    TextLocation location;
38    string keyword = string.Empty;
39   
40    public string Keyword {
41      get { return keyword; }
42      set {
43        if (value == null)
44          throw new ArgumentNullException();
45        ThrowIfFrozen();
46        keyword = value;
47      }
48    }
49   
50    public KnownTypeCode KnownTypeCode {
51      get { return GetTypeCodeForPrimitiveType(this.Keyword); }
52    }
53   
54    public PrimitiveType()
55    {
56    }
57   
58    public PrimitiveType(string keyword)
59    {
60      this.Keyword = keyword;
61    }
62   
63    public PrimitiveType(string keyword, TextLocation location)
64    {
65      this.Keyword = keyword;
66      this.location = location;
67    }
68   
69    public override TextLocation StartLocation {
70      get {
71        return location;
72      }
73    }
74   
75    internal void SetStartLocation(TextLocation value)
76    {
77      ThrowIfFrozen();
78      this.location = value;
79    }
80   
81    public override TextLocation EndLocation {
82      get {
83        return new TextLocation (location.Line, location.Column + keyword.Length);
84      }
85    }
86   
87    public override void AcceptVisitor (IAstVisitor visitor)
88    {
89      visitor.VisitPrimitiveType (this);
90    }
91     
92    public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
93    {
94      return visitor.VisitPrimitiveType (this);
95    }
96   
97    public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
98    {
99      return visitor.VisitPrimitiveType (this, data);
100    }
101   
102    protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
103    {
104      PrimitiveType o = other as PrimitiveType;
105      return o != null && MatchString(this.Keyword, o.Keyword);
106    }
107
108    public override string ToString(CSharpFormattingOptions formattingOptions)
109    {
110      return Keyword;
111    }
112   
113    public override ITypeReference ToTypeReference(NameLookupMode lookupMode, InterningProvider interningProvider = null)
114    {
115      KnownTypeCode typeCode = GetTypeCodeForPrimitiveType(this.Keyword);
116      if (typeCode == KnownTypeCode.None)
117        return new UnknownType(null, this.Keyword);
118      else
119        return KnownTypeReference.Get(typeCode);
120    }
121   
122    public static KnownTypeCode GetTypeCodeForPrimitiveType(string keyword)
123    {
124      switch (keyword) {
125        case "string":
126          return KnownTypeCode.String;
127        case "int":
128          return KnownTypeCode.Int32;
129        case "uint":
130          return KnownTypeCode.UInt32;
131        case "object":
132          return KnownTypeCode.Object;
133        case "bool":
134          return KnownTypeCode.Boolean;
135        case "sbyte":
136          return KnownTypeCode.SByte;
137        case "byte":
138          return KnownTypeCode.Byte;
139        case "short":
140          return KnownTypeCode.Int16;
141        case "ushort":
142          return KnownTypeCode.UInt16;
143        case "long":
144          return KnownTypeCode.Int64;
145        case "ulong":
146          return KnownTypeCode.UInt64;
147        case "float":
148          return KnownTypeCode.Single;
149        case "double":
150          return KnownTypeCode.Double;
151        case "decimal":
152          return KnownTypeCode.Decimal;
153        case "char":
154          return KnownTypeCode.Char;
155        case "void":
156          return KnownTypeCode.Void;
157        default:
158          return KnownTypeCode.None;
159      }
160    }
161  }
162}
163
Note: See TracBrowser for help on using the repository browser.