extending the xbase typesystem

33
Extending the Xbase Typesystem Sebastian Zarnekow

Upload: sebastian-zarnekow

Post on 20-Aug-2015

304 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Extending the Xbase Typesystem

Extending the Xbase TypesystemSebastian Zarnekow

Page 2: Extending the Xbase Typesystem

Understanding the Xbase TS

Page 3: Extending the Xbase Typesystem

LazyLinking vs BatchLinking

Page 4: Extending the Xbase Typesystem

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

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

LightweightTypeReference

Page 7: Extending the Xbase Typesystem

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

ITypeReferenceOwner

Provides Context: Current ResourceSet

Provides CommonTypeComputationServices

Factory API to Create Valid Type References

Page 9: Extending the Xbase Typesystem

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

IBatchTypeResolver

IReentrantTypeResolver

ITypeComputer

BatchLinkableResource

IJvmModelInferrer

InferredTypeIndicator

<<installs>> <<detects>>

<<initialized by>>

Page 11: Extending the Xbase Typesystem

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

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

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

Cookbook: Extending the Xbase TS

Page 15: Extending the Xbase Typesystem

DateLiteral

DateLiteral returns xbase::XExpression: day=INT ’.’ month=INT ’.’ year=INT;

Page 16: Extending the Xbase Typesystem

DateLiteral

Use java.util.Date (at your own risk)

Page 17: Extending the Xbase Typesystem

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

DateLiteral

Support Different Date Implementations

Page 19: Extending the Xbase Typesystem

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

DateLiteral

Validate Literal Values

Page 21: Extending the Xbase Typesystem

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

Library TypesGlobally Available Types

XImportSectionNamespaceScopeProvider and IImportsConfiguration

Globally Available Features

ImplicitlyImportedFeatures

Locally Available Features

ITypeComputationState.addImports

Page 23: Extending the Xbase Typesystem

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

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

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

Custom Assignability Rules

TypeConformanceComputer

Native Assignability Rules

Uses Custom SynonymTypesProvider

Common SuperType Computation

Usually not Customized

Page 27: Extending the Xbase Typesystem

Recap: What to customize if …

JvmModelInferrer

Always

Put XExpression into “some” Context

Mark Types as Inferred

Page 28: Extending the Xbase Typesystem

Recap: What to customize if …

XbaseTypeComputer

Newly Introduced Expression

Modified Expression Semantics

Page 29: Extending the Xbase Typesystem

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

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

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

Unit Test Utility

@RunWith(XtextSmokeTestRunner) @ProcessedBy( value = TypeSystemSmokeTester, processInParallel = true) @SuiteClasses(ParserTest, ValidationTest) class SmokeTest {}

Page 33: Extending the Xbase Typesystem

Q & A