Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e461a45fd78740e3588a6ac075760584f522a499 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*******************************************************************************
 * Copyright (c) 2010 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * CONTRIBUTORS:
 * 		Thomas Schuetz and Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

/*
	collection of convenience functions for code generation
*/


package org.eclipse.etrice.generator.java.gen

import com.google.inject.Singleton
import java.util.List
import org.eclipse.etrice.core.room.DataType
import org.eclipse.etrice.core.room.ExternalType
import org.eclipse.etrice.core.room.Message
import org.eclipse.etrice.core.room.PrimitiveType
import org.eclipse.etrice.core.room.RoomClass
import org.eclipse.etrice.core.room.VarDecl
import org.eclipse.etrice.generator.generic.ILanguageExtension
import org.eclipse.xtext.util.Pair

@Singleton
class JavaExtensions implements ILanguageExtension {

	override String getTypedDataDefinition(Message m) {
		generateArglistAndTypedData(m.data).get(1)
	}

	def String getJavaFileName(RoomClass rc) {rc.name+".java"}
	
	def String toWrapper(String type){
		switch(type){
			case "int": "Integer"
			case "char" : "Character"
			default: type.toFirstUpper
		}
	}
	
	override String accessLevelPrivate() {"private "}
	override String accessLevelProtected() {"protected "}
	override String accessLevelPublic() {"public "}
	
	override String memberAccess() {"this."} 	 
	override String selfPointer(String classname, boolean hasArgs) {""}
	override String selfPointer(boolean hasArgs) { "" }
	
	override String operationScope(String classname, boolean isDeclaration) {""}
	

	override String memberInDeclaration(String namespace, String member) {
		return member
	}
	
	override String memberInUse(String namespace, String member) {
		return namespace+"."+member
	}
	
	override boolean usesInheritance() {
		return true
	}
	
	override String genEnumeration(String name, List<Pair<String, String>> entries) {
		'''
		«FOR entry: entries»
			public static final int «entry.first» = «entry.second»;
		«ENDFOR»
		'''.toString
	}

	override String booleanConstant(boolean b) {
		b.toString
	}
	
	override String pointerLiteral() { "" }
	override String nullPointer() { "null" }
	override String voidPointer() { "Object" }

	override String arrayDeclaration(String type, int size, String name, boolean isRef) {
		type+" "+name+"[]"
	}
	
	override String constructorName(String cls) {
		cls
	}
	override String destructorName(String cls) {
		cls+"_dtor"
	}
	override String constructorReturnType() {
		""
	}
	override String destructorReturnType() {
		"void"
	}
	override String toCharArrayExpr(String s){
		"\"" + s + "\".toCharArray()"
	}
	override String superCall(String baseClassName, String method, String args) {
		"super."+method+"("+args+");"
	}
	override String toValueLiteral(PrimitiveType type, String value){
		switch(type.targetName){
			case "boolean":
				return value
			case "byte":
				return "(byte)"+value
			case "short":
				return "(short)"+value
			case "int":
				return value
			case "long":
				return value+"L"
			case "float":
				return value+"f"
			case "double":
				return value+"d"
			case "char":{
				if(value.length == 1)
					return "'"+value+"'"
				else
					return value.toCharArrayExpr
			}
			case "String":
				return "\""+value+"\""
		}
		
		throw new UnsupportedOperationException(type.targetName)
	}

	override defaultValue(DataType dt) {
		if (dt instanceof PrimitiveType) {
			return (dt as PrimitiveType).getDefaultValueLiteral
		}
		else if (dt instanceof ExternalType)
			return "new "+(dt as ExternalType).targetName+"()"
		else
			return "new "+dt.name+"()"
	}
	

	override initializationWithDefaultValues(DataType dt, int size) {
		val dv = dt.defaultValue
		if (size>1) {
			var res = "{"
			var i = 0
			while (i<size) {
				res = res + dv
				i = i+1
				if (i<size)
					res = res + ","
			}
			res+"}"
		}
		else
			dv
	}

	override generateArglistAndTypedData(VarDecl data) {
		if (data==null)
			return newArrayList("", "", "")
		
		var typeName = data.getRefType().getType().getName();
		var castTypeName = typeName;
		if (data.getRefType().getType() instanceof PrimitiveType) {
			typeName = (data.getRefType().getType() as PrimitiveType).getTargetName();
			val ct = (data.getRefType().getType() as PrimitiveType).getCastName();
			if (ct!=null && !ct.isEmpty())
				castTypeName = ct;
		}

		val typedData = typeName+" "+data.getName() + " = ("+castTypeName+") generic_data;\n";
		val dataArg = ", "+data.getName();
		val typedArgList = ", "+typeName+" "+data.getName();
		
		return newArrayList(dataArg, typedData, typedArgList);
	}
	
}

Back to the top