-
Notifications
You must be signed in to change notification settings - Fork 317
/
XtendGenerator.xtend
405 lines (376 loc) · 15.4 KB
/
XtendGenerator.xtend
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*******************************************************************************
* Copyright (c) 2013, 2019 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtend.core.compiler
import com.google.common.collect.Lists
import com.google.inject.Inject
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.emf.ecore.util.EcoreUtil
import org.eclipse.xtend.core.macro.ActiveAnnotationContexts
import org.eclipse.xtend.core.macro.CodeGenerationContextImpl
import org.eclipse.xtend.core.xtend.AnonymousClass
import org.eclipse.xtend.core.xtend.XtendFunction
import org.eclipse.xtend.core.xtend.XtendMember
import org.eclipse.xtend.core.xtend.XtendTypeDeclaration
import org.eclipse.xtend.lib.macro.CodeGenerationParticipant
import org.eclipse.xtend.lib.macro.declaration.NamedElement
import org.eclipse.xtext.EcoreUtil2
import org.eclipse.xtext.common.types.JvmDeclaredType
import org.eclipse.xtext.common.types.JvmExecutable
import org.eclipse.xtext.common.types.JvmFeature
import org.eclipse.xtext.common.types.JvmField
import org.eclipse.xtext.common.types.JvmGenericType
import org.eclipse.xtext.common.types.JvmMember
import org.eclipse.xtext.common.types.JvmOperation
import org.eclipse.xtext.common.types.JvmTypeReference
import org.eclipse.xtext.common.types.JvmVisibility
import org.eclipse.xtext.generator.IFileSystemAccess
import org.eclipse.xtext.generator.IFileSystemAccess2
import org.eclipse.xtext.generator.IGenerator2
import org.eclipse.xtext.generator.IGeneratorContext
import org.eclipse.xtext.service.OperationCanceledManager
import org.eclipse.xtext.util.Strings
import org.eclipse.xtext.xbase.XAbstractFeatureCall
import org.eclipse.xtext.xbase.XClosure
import org.eclipse.xtext.xbase.XExpression
import org.eclipse.xtext.xbase.compiler.ElementIssueProvider
import org.eclipse.xtext.xbase.compiler.GeneratorConfig
import org.eclipse.xtext.xbase.compiler.JvmModelGenerator
import org.eclipse.xtext.xbase.compiler.output.ITreeAppendable
import org.eclipse.xtext.xbase.compiler.output.SharedAppendableState
import org.eclipse.xtext.xbase.typesystem.IBatchTypeResolver
import org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner
import org.eclipse.xtext.xbase.compiler.ImportManager
import org.eclipse.xtext.generator.trace.ITraceURIConverter
import org.eclipse.xtext.resource.ILocationInFileProvider
import org.eclipse.xtext.xbase.jvmmodel.IJvmModelAssociations
import org.eclipse.emf.ecore.EObject
import org.eclipse.xtend.core.compiler.output.AnonymousClassAwareTreeAppendable
/**
* @author Sven Efftinge - Initial contribution and API
*/
class XtendGenerator extends JvmModelGenerator implements IGenerator2 {
@Inject IBatchTypeResolver typeResolver;
@Inject OperationCanceledManager operationCanceledManager
@Inject ElementIssueProvider.Factory issueProviderFactory
override doGenerate(Resource input, IFileSystemAccess fsa) {
super.doGenerate(input, fsa)
callMacroProcessors(input)
}
override beforeGenerate(Resource input, IFileSystemAccess2 fsa, IGeneratorContext context) {
issueProviderFactory.attachData(input)
}
override afterGenerate(Resource input, IFileSystemAccess2 fsa, IGeneratorContext context) {
issueProviderFactory.detachData(input)
}
override doGenerate(Resource input, IFileSystemAccess2 fsa, IGeneratorContext context) {
doGenerate(input, fsa as IFileSystemAccess)
}
def void callMacroProcessors(Resource input) {
val ctxs = ActiveAnnotationContexts.find(input);
if (ctxs === null)
return;
try {
ctxs.before(ActiveAnnotationContexts.AnnotationCallback.GENERATION);
for (context : ctxs.contexts.values) {
try {
switch processor : context.processorInstance {
CodeGenerationParticipant<NamedElement> : {
val codeGenServices = new CodeGenerationContextImpl => [
unit = context.compilationUnit
]
val elements = context.annotatedSourceElements.map[context.compilationUnit.toXtendMemberDeclaration(it as XtendMember)]
processor.doGenerateCode(elements, codeGenServices)
}
}
} catch (Throwable t) {
operationCanceledManager.propagateAsErrorIfCancelException(t)
context.handleProcessingError(input, t)
}
}
} finally {
ctxs.after(ActiveAnnotationContexts.AnnotationCallback.GENERATION);
}
}
override protected createImportingStringConcatenation(SharedAppendableState state, ITypeReferenceOwner owner) {
new MacroAwareStringConcatenation(state, owner)
}
/**
* Convert a given input string to a Java string.
*
* Unicode escaping is handled by the {@link UnicodeAwarePostProcessor}.
*/
override doConvertToJavaString(String input) {
Strings.convertToJavaString(input, false)
}
override protected _getMembersToBeCompiled(JvmGenericType it) {
if (it.local) {
members.filter[
it instanceof JvmOperation
]
} else {
_getMembersToBeCompiled(it as JvmDeclaredType)
}
}
def protected getAddedDeclarations(JvmGenericType it, AnonymousClass anonymousClass) {
val result = <JvmMember>newArrayList
val constructor = anonymousClass.constructorCall.constructor
if (constructor.parameters.size >= 1)
result.add(0, constructor)
result += declaredFields
result += declaredOperations.filter [
val function = sourceElements.head as XtendFunction
return !function.override
]
result += members.filter(JvmDeclaredType)
return result
}
override compile(JvmExecutable executable, XExpression expression, JvmTypeReference returnType, ITreeAppendable appendable, GeneratorConfig config) {
compileLocalTypeStubs(executable, appendable, config)
super.compile(executable, expression, returnType, appendable, config)
}
override reassignThisType(ITreeAppendable b, JvmDeclaredType declaredType) {
if (b.hasObject('this')) {
val element = b.getObject('this')
if (element instanceof JvmDeclaredType) {
if (element.local) {
if (b.hasName('this'->element)) {
b.declareVariable(element, b.getName('this'->element))
} else {
b.declareVariable(element, '')
}
} else {
val proposedName = element.simpleName+'.this'
b.declareVariable(element, proposedName)
}
}
}
if (declaredType !== null)
b.declareVariable(declaredType, 'this');
}
def compileLocalTypeStubs(JvmFeature feature, ITreeAppendable appendable, GeneratorConfig config) {
feature.localClasses.forEach[
val anonymousClass = sourceElements.head as AnonymousClass
if (XtendCompilerUtil.canCompileToJavaAnonymousClass(anonymousClass)) {
return
}
appendable.newLine
val childAppendable = appendable.trace(anonymousClass)
childAppendable.append('abstract class ')
childAppendable.traceSignificant(anonymousClass).append(simpleName)
if (typeParameters.empty)
childAppendable.append(" ")
generateExtendsClause(childAppendable, null)
childAppendable.append('{').increaseIndentation
if (needSyntheticThisVariable(anonymousClass, it)) {
val thisName = childAppendable.declareSyntheticVariable('this' -> it, '_this' + it.simpleName)
childAppendable.newLine.append("final ").append(simpleName).append(' ').append(thisName).append(' = this;')
// avoid generating empty lines with just two spaces
// https://github.com/eclipse/xtext-extras/issues/772
childAppendable.blankLine
}
childAppendable.forEach(getAddedDeclarations(anonymousClass), [
separator = memberSeparator()
], [
val memberAppendable = childAppendable.traceWithComments(it)
memberAppendable.openScope
if (it instanceof JvmOperation) {
val tracedAppendable = childAppendable.trace(it)
tracedAppendable.newLine
generateJavaDoc(tracedAppendable, config)
generateVisibilityModifier(tracedAppendable)
tracedAppendable.append('abstract ')
generateTypeParameterDeclaration(tracedAppendable, null)
if (it.returnType === null) {
tracedAppendable.append("void")
} else {
it.returnType.serializeSafely("Object", tracedAppendable)
}
tracedAppendable.append(" ")
tracedAppendable.traceSignificant(it).append(simpleName)
tracedAppendable.append("(")
generateParameters(tracedAppendable, null)
tracedAppendable.append(")")
generateThrowsClause(tracedAppendable, null)
tracedAppendable.append(";")
} else if (it instanceof JvmField) {
val tracedAppendable = childAppendable.trace(it)
tracedAppendable.newLine
generateJavaDoc(tracedAppendable, config)
annotations.generateAnnotations(tracedAppendable, true, config)
if (isStatic)
tracedAppendable.append("static ")
if (isFinal && isStatic)
tracedAppendable.append("final ")
if (isTransient)
tracedAppendable.append("transient ")
if (isVolatile)
tracedAppendable.append("volatile ")
type.serializeSafely("Object", tracedAppendable)
tracedAppendable.append(" ")
tracedAppendable.traceSignificant(it).append(simpleName)
if (isFinal && isStatic) {
if (constantValue !== null) {
tracedAppendable.append(" = ")
generateJavaConstant(constantValue, tracedAppendable)
} else {
generateInitialization(tracedAppendable, config)
}
}
tracedAppendable.append(";")
} else {
generateMember(memberAppendable, config)
}
memberAppendable.closeScope
])
childAppendable.decreaseIndentation.newLine.append('}')
// avoid generating empty lines with just two spaces
// https://github.com/eclipse/xtext-extras/issues/772
appendable.blankLine
]
}
private def generateJavaConstant(Object value, ITreeAppendable appendable) {
if (value instanceof Float)
appendable.append(value.toString).append('f')
else if (value instanceof Long)
appendable.append(value.toString).append('l')
else if (value instanceof Character)
appendable.append(Integer.toString(value.charValue))
else if (value instanceof CharSequence)
appendable.append('"').append(doConvertToJavaString(value.toString)).append('"')
else if (value instanceof Number || value instanceof Boolean)
appendable.append(value.toString)
else
appendable.append('null /* ERROR: illegal constant value */')
}
private def boolean needSyntheticThisVariable(AnonymousClass anonymousClass, JvmDeclaredType localType) {
val references = Lists.newArrayListWithCapacity(1)
try {
EcoreUtil2.findCrossReferences(anonymousClass, newImmutableSet(localType), [
referrer, referenced, reference, index |
if (referrer.eContainer instanceof XAbstractFeatureCall) {
val containingFeature = referrer.eContainer as XAbstractFeatureCall
if (containingFeature.actualReceiver == referrer && containingFeature.feature instanceof JvmMember
&& !isVisible(containingFeature.feature as JvmMember, localType)) {
// We reference a feature that would not be visible if accessed through a synthetic _this variable
references.clear
throw new StopCollecting
}
}
val enclosingType = EcoreUtil2.getContainerOfType(referrer, XtendTypeDeclaration)
if (enclosingType !== null && enclosingType != anonymousClass) {
if (references.empty)
references.add(referrer)
return
}
val enclosingLambda = EcoreUtil2.getContainerOfType(referrer, XClosure)
if (enclosingLambda !== null && EcoreUtil.isAncestor(anonymousClass, enclosingLambda)) {
if (references.empty)
references.add(referrer)
}
])
} catch(StopCollecting e) {
// ok
}
return !references.empty
}
/**
* Determine whether the given member is visible without considering the class hierarchy.
*/
private def isVisible(JvmMember member, JvmDeclaredType context) {
val visibility = member.visibility
if (visibility == JvmVisibility.PUBLIC)
return true
val type = if (member instanceof JvmDeclaredType) member else member.declaringType
if (type == context || EcoreUtil.isAncestor(context, type))
return true
if (type !== null && (visibility == JvmVisibility.DEFAULT || visibility == JvmVisibility.PROTECTED)) {
if (Strings.isEmpty(context.packageName) && Strings.isEmpty(type.packageName)
|| context.packageName == type.packageName)
return true
}
}
private static class StopCollecting extends Exception {
static final long serialVersionUID=-6188090786919774877L
}
override generateVisibilityModifier(JvmMember it, ITreeAppendable result) {
if (visibility == JvmVisibility.PRIVATE) {
if (declaringType === null) {
return result
}
if (declaringType.local && it instanceof JvmOperation) {
val declarator = declaringType as JvmGenericType
if (!declarator.anonymous ||
!XtendCompilerUtil.canCompileToJavaAnonymousClass(declarator.sourceElements.head as AnonymousClass)
) {
return result
}
}
} else if (visibility == JvmVisibility.PUBLIC) {
if (declaringType instanceof JvmGenericType && (declaringType as JvmGenericType).isInterface) {
return result
}
}
super.generateVisibilityModifier(it, result)
}
override generateMembersInBody(JvmDeclaredType it, ITreeAppendable appendable, GeneratorConfig config) {
if (it.local) {
appendable.append('{').increaseIndentation
val anonymousClass = sourceElements.head as AnonymousClass
if (!appendable.hasName('this' -> it) && needSyntheticThisVariable(anonymousClass, it)) {
val resolvedTypes = typeResolver.resolveTypes(anonymousClass)
val actualType = resolvedTypes.getActualType(anonymousClass)
val thisName = appendable.declareSyntheticVariable('this' -> it, '_this')
appendable.newLine.append("final ").append(actualType).append(' ').append(thisName).append(' = this;')
}
val fieldsWithInitializer = declaredFields.filter [
if (compilationStrategy !== null) {
return true
} else if (compilationTemplate !== null) {
return true
} else if (!(isFinal && isStatic)) {
val expression = associatedExpression
if (expression !== null && config.generateExpressions) {
return true
}
}
false
]
if (!fieldsWithInitializer.empty) {
appendable.newLine.append('{').increaseIndentation
appendable.forEach(fieldsWithInitializer, [
separator = memberSeparator()
], [
val memberAppendable = appendable.traceWithComments(it)
memberAppendable.openScope
appendable.newLine
val tracedAppendable = appendable.trace(it)
tracedAppendable.traceSignificant(it).append(simpleName)
generateInitialization(tracedAppendable, config)
tracedAppendable.append(";")
memberAppendable.closeScope
])
appendable.decreaseIndentation.newLine.append('}')
}
appendable.forEach(membersToBeCompiled, [
separator = memberSeparator()
], [
val memberAppendable = appendable.traceWithComments(it)
memberAppendable.openScope
generateMember(memberAppendable, config)
memberAppendable.closeScope
])
appendable.decreaseIndentation.newLine.append('}')
} else {
super.generateMembersInBody(it, appendable, config)
}
}
override createAppendable(ImportManager importManager, ITraceURIConverter converter, ILocationInFileProvider locationProvider, IJvmModelAssociations jvmModelAssociations, EObject source, String indentation, String lineSeparator) {
return new AnonymousClassAwareTreeAppendable(importManager, converter, locationProvider, jvmModelAssociations, source, indentation, lineSeparator)
}
}