| 1 |
/* |
| 2 |
* Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without modification, |
| 6 |
* are permitted provided that the following conditions are met: |
| 7 |
* |
| 8 |
* 1. Redistributions of source code must retain the above copyright notice, |
| 9 |
* this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 |
* this list of conditions and the following disclaimer in the documentation |
| 12 |
* and/or other materials provided with the distribution. |
| 13 |
* 3. The name of the author may not be used to endorse or promote products |
| 14 |
* derived from this software without specific prior written permission. |
| 15 |
* |
| 16 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 19 |
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 21 |
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 24 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
| 25 |
* OF SUCH DAMAGE. |
| 26 |
* |
| 27 |
* This file is part of the lwIP TCP/IP stack. |
| 28 |
* |
| 29 |
* Author: Martin Hentschel <[email protected]> |
| 30 |
* |
| 31 |
*/ |
| 32 |
|
| 33 |
using System; |
| 34 |
using System.Text; |
| 35 |
|
| 36 |
namespace CCodeGeneration |
| 37 |
{ |
| 38 |
public enum ConstType |
| 39 |
{ |
| 40 |
None, |
| 41 |
Value, |
| 42 |
Indirection, |
| 43 |
Both |
| 44 |
} |
| 45 |
|
| 46 |
public class VariableType : ICloneable |
| 47 |
{ |
| 48 |
public const string VoidString = "void"; |
| 49 |
public static readonly VariableType Void = new VariableType(null, "void"); |
| 50 |
|
| 51 |
public string Name { get; set; } |
| 52 |
public string Type { get; set; } |
| 53 |
public string Indirection { get; set; } |
| 54 |
public ConstType Const { get; set; } |
| 55 |
public string ArraySpecifier { get; set; } |
| 56 |
|
| 57 |
public VariableType() |
| 58 |
{ |
| 59 |
} |
| 60 |
|
| 61 |
public VariableType(string name, string type, string indirection = null, ConstType const_ = ConstType.None, string arraySpecifier = null) |
| 62 |
{ |
| 63 |
this.Name = name; |
| 64 |
this.Type = type; |
| 65 |
this.Indirection = indirection; |
| 66 |
this.Const = const_; |
| 67 |
this.ArraySpecifier = arraySpecifier; |
| 68 |
} |
| 69 |
|
| 70 |
public void GenerateCode(CGenerator generator) |
| 71 |
{ |
| 72 |
if (!String.IsNullOrWhiteSpace(this.Type)) |
| 73 |
{ |
| 74 |
generator.OutputStream.Write(this.ToString().Trim()); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
public override string ToString() |
| 79 |
{ |
| 80 |
if (!String.IsNullOrWhiteSpace(this.Type)) |
| 81 |
{ |
| 82 |
StringBuilder vt = new StringBuilder(); |
| 83 |
|
| 84 |
if ((this.Const == ConstType.Value) || (this.Const == ConstType.Both)) |
| 85 |
{ |
| 86 |
vt.Append("const "); |
| 87 |
} |
| 88 |
|
| 89 |
vt.Append(this.Type); |
| 90 |
vt.Append(" "); |
| 91 |
|
| 92 |
if (!String.IsNullOrWhiteSpace(this.Indirection)) |
| 93 |
{ |
| 94 |
vt.Append(this.Indirection); |
| 95 |
} |
| 96 |
|
| 97 |
if ((this.Const == ConstType.Indirection) || (this.Const == ConstType.Both)) |
| 98 |
{ |
| 99 |
vt.Append("const "); |
| 100 |
} |
| 101 |
|
| 102 |
if (!String.IsNullOrWhiteSpace(this.Name)) |
| 103 |
{ |
| 104 |
vt.Append(this.Name); |
| 105 |
} |
| 106 |
|
| 107 |
if (this.ArraySpecifier != null) |
| 108 |
{ |
| 109 |
vt.Append("["); |
| 110 |
vt.Append(this.ArraySpecifier); |
| 111 |
vt.Append("]"); |
| 112 |
} |
| 113 |
|
| 114 |
return vt.ToString().Trim(); |
| 115 |
} |
| 116 |
|
| 117 |
return base.ToString(); |
| 118 |
} |
| 119 |
|
| 120 |
#region ICloneable Member |
| 121 |
|
| 122 |
public object Clone() |
| 123 |
{ |
| 124 |
// we only have value types as members -> simply use .net base function |
| 125 |
return this.MemberwiseClone(); |
| 126 |
} |
| 127 |
|
| 128 |
#endregion |
| 129 |
} |
| 130 |
} |