extending the xbase typesystem
TRANSCRIPT
![Page 1: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/1.jpg)
Extending the Xbase TypesystemSebastian Zarnekow
![Page 2: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/2.jpg)
Understanding the Xbase TS
![Page 3: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/3.jpg)
LazyLinking vs BatchLinking
![Page 4: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/4.jpg)
IBatchTypeResolverCancelable Type Resolution for a Resource
Returns IResolvedTypes
Type for each JvmIdentifiableElement (Local Var, Param, ..)
Resolved Generics
For each XExpression:
Actual Type and Expected Type
Return Type and Expected Return Type
![Page 5: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/5.jpg)
Actual Type vs Return Type
Type: What would ‘Extract Local Var’ do? == boolean
Return Type: What would ‘Extract Method’ yield? == void
{ if (!isValid(value)) return; hashSet.add(value);}
![Page 6: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/6.jpg)
LightweightTypeReference
![Page 7: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/7.jpg)
LightweightTypeReference
Solve the EMF Dilemma
Rich API
getSuperTypes, getTypeArguments, isAssignableFrom, …
Context and Service Access via getOwner()
Lossless conversion from/to JvmTypeReference
![Page 8: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/8.jpg)
ITypeReferenceOwner
Provides Context: Current ResourceSet
Provides CommonTypeComputationServices
Factory API to Create Valid Type References
![Page 9: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/9.jpg)
Facade for Clients
Find Relevant Root Elements in Resource
Traverse Root Instance Tree
Guard Against Infinite Recursion
Prepare and Compute
Traverse Expression Trees
First Candidate for Customizing
IBatchTypeResolver
IReentrantTypeResolver
ITypeComputer
IBatchTypeResolver
![Page 10: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/10.jpg)
IBatchTypeResolver
IReentrantTypeResolver
ITypeComputer
BatchLinkableResource
IJvmModelInferrer
InferredTypeIndicator
<<installs>> <<detects>>
<<initialized by>>
![Page 11: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/11.jpg)
XbaseTypeComputer
computeTypes(XExpression, ITypeComputationState)
MyDslTypeComputer
Called by the Framework
Never Invoke computeTypes(..) Manually
Implements Default XExpression Typing
Dispatches for given XExpression
Optional Customization
Required for New Expressions
ITypeComputer
![Page 12: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/12.jpg)
ITypeComputercomputeTypes(XExpression, ITypeComputationState)
ITypeComputationState
computeTypes(XExpression): ITypeComputationResult withExpectation(LightweightTypeReference): ITCState
getExpectations(): List<ITypeExpectation> acceptActualType(LightweightTypeReference, Hints)
assignTypes(..): ITypeComputationState
ITypeExpectation ITypeComputationResult
<<yields>>
<<calls>>
<<uses>>
<<creates>>
<<influences>>
<<calls>>
<<uses>>
![Page 13: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/13.jpg)
Type System Invariants
All Expressions Must be Visited
All InferredTypes Must be Resolved
JvmMember Signatures Must be Complete
Explicit Tree TraversalNo eAllContents.forEach in Framework Code
![Page 14: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/14.jpg)
Cookbook: Extending the Xbase TS
![Page 15: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/15.jpg)
DateLiteral
DateLiteral returns xbase::XExpression: day=INT ’.’ month=INT ’.’ year=INT;
![Page 16: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/16.jpg)
DateLiteral
Use java.util.Date (at your own risk)
![Page 17: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/17.jpg)
DateLiteral
class MyDslTypeComputer extends XbaseTypeComputer { def dispatch computeTypes(DateLiteral date, ITCState s) { s.acceptActualType(getTypeForName(j.u.Date, s)) } }
![Page 18: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/18.jpg)
DateLiteral
Support Different Date Implementations
![Page 19: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/19.jpg)
Exploit Type Expectation
def dispatch computeTypes(DateLiteral date, ITCState s) { for(e: s.expectations) e.acceptActualType(getTypeForName(switch it: e.expectedType { case it === null: java.util.Date // no expected type given case isType(java.sql.Date): java.sql.Date case isType(LocalDate): LocalDate // fancy Java8 impl default: java.util.Date // eeek }, s))}
![Page 20: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/20.jpg)
DateLiteral
Validate Literal Values
![Page 21: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/21.jpg)
Validate Expressions
def dispatch computeTypes(DateLiteral date, ITCState s) { if (isInvalid(date.day, date.month, date.year)) { s.addDiagnostic(new EObjectDiagnosticImpl( Severity.ERROR, IssueCodes.INVALID_DATE_LITERAL, ’’’«day».«month».«year» is not a valid date’’’, date, null, -1, Strings.EMPTY_ARRAY)); } ..}
![Page 22: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/22.jpg)
Library TypesGlobally Available Types
XImportSectionNamespaceScopeProvider and IImportsConfiguration
Globally Available Features
ImplicitlyImportedFeatures
Locally Available Features
ITypeComputationState.addImports
![Page 23: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/23.jpg)
Library Typesdef dispatch computeTypes(MyExpression e, ITCState s) { s.addImports [ ITypeImporter it | for(e: s.expectations) { if (isEnum(e.expectedType)) { it.importStatic( e.expectedType.type as JvmEnumerationType) } } ] // see also s.addTypeToStaticImportScope(..) s.computeTypes(e.childExpression)}
![Page 24: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/24.jpg)
Different Qualities of Assignability
Synonyms
Type A is Convertable to Type B
Compiler / Interpreter Need Special Treatment
Native Assignability
Only Reasonable with Custom Interpreter / Runtime
Assignable According to Runtime
Custom Assignability Rules
![Page 25: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/25.jpg)
class MySynonymTypesProvider extends SynonymTypesProvider { @Override def boolean collectCustomSynonymTypes(LightweightTypeReference type, Acceptor acceptor) { if (type.invariantBoundSubstitute.isType(j.t.LocalDate)) { return announceSynonym( getUtilDate(type), ConformanceFlags.DEMAND_CONVERSION, acceptor); } return true; }}
Custom Assignability Rules
![Page 26: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/26.jpg)
Custom Assignability Rules
TypeConformanceComputer
Native Assignability Rules
Uses Custom SynonymTypesProvider
Common SuperType Computation
Usually not Customized
![Page 27: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/27.jpg)
Recap: What to customize if …
JvmModelInferrer
Always
Put XExpression into “some” Context
Mark Types as Inferred
![Page 28: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/28.jpg)
Recap: What to customize if …
XbaseTypeComputer
Newly Introduced Expression
Modified Expression Semantics
![Page 29: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/29.jpg)
Recap: What to customize if …
DefaultReentrantTypeResolver
iff Expressions without JVM Model (Discouraged)
and Expressions not at Root Level
LogicalContainerAwareReentrantTypeResolver
Handle AntLR Error Recovery Situations
![Page 30: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/30.jpg)
Recap: What to customize if …
*ReentrantTypeResolver
Prepare Special Cases of InferredTypeIndicators e.g. InferredType 1:n or 1:0 XExpression
![Page 31: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/31.jpg)
Recap: What to customize if …
DefaultBatchTypeResolver
Non-JVM Model Entry Points on Resource Level
LogicalContainerAwareBatchTypeResolver
Handle AntLR Error Recovery Situations
Usually Not Necessary
![Page 32: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/32.jpg)
Unit Test Utility
@RunWith(XtextSmokeTestRunner) @ProcessedBy( value = TypeSystemSmokeTester, processInParallel = true) @SuiteClasses(ParserTest, ValidationTest) class SmokeTest {}
![Page 33: Extending the Xbase Typesystem](https://reader034.vdocuments.site/reader034/viewer/2022052414/55d59708bb61ebc8138b4647/html5/thumbnails/33.jpg)
Q & A