Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Extending the Xbase Typesystem


Published on

Overview on the architecture of the Xbase type inferrer including different examples for common customization scenarios.

Published in: Software
  • Be the first to comment

Extending the Xbase Typesystem

  1. 1. Extending the Xbase Typesystem Sebastian Zarnekow
  2. 2. Understanding the Xbase TS
  3. 3. LazyLinking vs BatchLinking
  4. 4. IBatchTypeResolver Cancelable 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
  5. 5. Actual Type vs Return Type Type: What would ‘Extract Local Var’ do? == boolean Return Type: What would ‘Extract Method’ yield? == void {
 if (!isValid(value))
  6. 6. LightweightTypeReference
  7. 7. LightweightTypeReference Solve the EMF Dilemma Rich API getSuperTypes, getTypeArguments, isAssignableFrom, … Context and Service Access via getOwner() Lossless conversion from/to JvmTypeReference
  8. 8. ITypeReferenceOwner Provides Context: Current ResourceSet Provides CommonTypeComputationServices Factory API to Create Valid Type References
  9. 9. 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
  10. 10. IBatchTypeResolver IReentrantTypeResolver ITypeComputer
 BatchLinkableResource IJvmModelInferrer InferredTypeIndicator <<installs>> <<detects>> <<initialized by>>
  11. 11. 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

  12. 12. ITypeComputer
 computeTypes(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>>
  13. 13. Type System Invariants All Expressions Must be Visited All InferredTypes Must be Resolved JvmMember Signatures Must be Complete Explicit Tree Traversal
 No eAllContents.forEach in Framework Code
  14. 14. Cookbook:
 Extending the Xbase TS
  15. 15. DateLiteral DateLiteral returns xbase::XExpression: day=INT ’.’ month=INT ’.’ year=INT;
  16. 16. DateLiteral Use java.util.Date (at your own risk)
  17. 17. DateLiteral class MyDslTypeComputer extends XbaseTypeComputer { def dispatch computeTypes(DateLiteral date, ITCState s) { s.acceptActualType(getTypeForName(j.u.Date, s)) } }
  18. 18. DateLiteral Support Different Date Implementations
  19. 19. 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))
  20. 20. DateLiteral Validate Literal Values
  21. 21. Validate Expressions def dispatch computeTypes(DateLiteral date, ITCState s) { if (isInvalid(, date.month, date.year)) { s.addDiagnostic(new EObjectDiagnosticImpl(
 ’’’«day».«month».«year» is not a valid date’’’,
 date, null, -1, Strings.EMPTY_ARRAY));
 } ..
  22. 22. Library Types Globally Available Types XImportSectionNamespaceScopeProvider and IImportsConfiguration Globally Available Features ImplicitlyImportedFeatures Locally Available Features ITypeComputationState.addImports
  23. 23. Library Types def 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(..)
  24. 24. 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
  25. 25. class MySynonymTypesProvider extends SynonymTypesProvider { @Override
 def boolean collectCustomSynonymTypes(LightweightTypeReference type,
 Acceptor acceptor) { if (type.invariantBoundSubstitute.isType(j.t.LocalDate)) { return announceSynonym(
 acceptor); }
 return true; }
 } Custom Assignability Rules
  26. 26. Custom Assignability Rules TypeConformanceComputer Native Assignability Rules Uses Custom SynonymTypesProvider Common SuperType Computation Usually not Customized
  27. 27. Recap: What to customize if … JvmModelInferrer Always Put XExpression into “some” Context Mark Types as Inferred
  28. 28. Recap: What to customize if … XbaseTypeComputer Newly Introduced Expression Modified Expression Semantics
  29. 29. Recap: What to customize if … DefaultReentrantTypeResolver iff Expressions without JVM Model (Discouraged) and Expressions not at Root Level LogicalContainerAwareReentrantTypeResolver Handle AntLR Error Recovery Situations
  30. 30. Recap: What to customize if … *ReentrantTypeResolver Prepare Special Cases of InferredTypeIndicators
 e.g. InferredType 1:n or 1:0 XExpression
  31. 31. Recap: What to customize if … DefaultBatchTypeResolver Non-JVM Model Entry Points on Resource Level LogicalContainerAwareBatchTypeResolver Handle AntLR Error Recovery Situations Usually Not Necessary
  32. 32. Unit Test Utility @RunWith(XtextSmokeTestRunner) @ProcessedBy(
 value = TypeSystemSmokeTester,
 processInParallel = true)
 @SuiteClasses(ParserTest, ValidationTest)
 class SmokeTest {}
  33. 33. Q & A