• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <info@cl-soft.de>
30  *
31  */
32 
33 using System;
34 using System.Collections.Generic;
35 
36 namespace CCodeGeneration
37 {
38 	public class Function: CodeContainerBase
39 	{
40 		public string Name { get; set; }
41 		public bool IsStatic { get; set; }
42 
43 		private readonly List<VariableType> parameter = new List<VariableType>();
44 		private VariableType returnType = VariableType.Void;
45 
Function()46 		public Function()
47 		{
48 		}
49 
Function(string name, bool isStatic = false)50 		public Function(string name, bool isStatic = false)
51 		{
52 			this.Name = name;
53 			this.IsStatic = isStatic;
54 		}
55 
56 		public List<VariableType> Parameter
57 		{
58 			get { return this.parameter; }
59 		}
60 
61 		public VariableType ReturnType
62 		{
63 			get { return this.returnType; }
64 			set
65 			{
66 				if (value == null)
67 				{
68 					throw new ArgumentNullException("ReturnValue");
69 				}
70 				this.returnType = value;
71 			}
72 		}
73 
FromDeclaration(FunctionDeclaration decl)74 		public static Function FromDeclaration(FunctionDeclaration decl)
75 		{
76 			Function result = new Function(decl.Name, decl.IsStatic);
77 			result.ReturnType = decl.ReturnType.Clone() as VariableType;
78 
79 			foreach (VariableType param in decl.Parameter)
80 			{
81 				result.parameter.Add(param.Clone() as VariableType);
82 			}
83 
84 			return result;
85 		}
86 
GenerateCode(int level, CGenerator generator)87 		public override void GenerateCode(int level, CGenerator generator)
88 		{
89 			generator.IndentLine(level);
90 
91 			if (this.IsStatic)
92 			{
93 				generator.OutputStream.Write("static ");
94 			}
95 
96 			this.returnType.GenerateCode(generator);
97 			generator.OutputStream.Write(" " + this.Name + "(");
98 
99 			if (this.Parameter.Count > 0)
100 			{
101 				for (int i = 0; i < this.parameter.Count; i++)
102 				{
103 					this.parameter[i].GenerateCode(generator);
104 
105 					if (i < (this.parameter.Count - 1))
106 					{
107 						generator.OutputStream.Write(", ");
108 					}
109 				}
110 			}
111 			else
112 			{
113 				generator.OutputStream.Write("void");
114 			}
115 
116 			generator.OutputStream.Write(")");
117 			generator.WriteNewLine();
118 			generator.IndentLine(level);
119 			generator.OutputStream.Write("{");
120 			generator.WriteNewLine();
121 
122 			base.GenerateCode(level, generator);
123 
124 			generator.IndentLine(level);
125 			generator.OutputStream.Write("}");
126 			generator.WriteNewLine();
127 		}
128 	}
129 }
130