-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add standard definitions on Tasty reflect #4895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nicolasstucki
merged 3 commits into
scala:master
from
dotty-staging:add-tasty-reflect-std-definitions
Aug 8, 2018
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
33 changes: 18 additions & 15 deletions
33
compiler/src/dotty/tools/dotc/tastyreflect/FromSymbol.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,40 @@ | ||
package dotty.tools.dotc.tastyreflect | ||
|
||
import dotty.tools.dotc.ast.tpd | ||
import dotty.tools.dotc.core.Contexts.Context | ||
import dotty.tools.dotc.core.Flags._ | ||
import dotty.tools.dotc.core.StdNames._ | ||
import dotty.tools.dotc.core.Symbols._ | ||
import dotty.tools.dotc.core.Types._ | ||
|
||
object FromSymbol { | ||
trait FromSymbol { tasty: TastyImpl => | ||
|
||
def definition(sym: Symbol)(implicit ctx: Context): tpd.Tree = { | ||
if (sym.is(Package)) packageDef(sym) | ||
def definitionFromSym(sym: Symbol)(implicit ctx: Context): tpd.Tree = { | ||
if (sym.is(Package)) packageDefFromSym(sym) | ||
else if (sym == defn.AnyClass) tpd.EmptyTree // FIXME | ||
else if (sym == defn.NothingClass) tpd.EmptyTree // FIXME | ||
else if (sym.isClass) classDef(sym.asClass) | ||
else if (sym.isType) typeDef(sym.asType) | ||
else if (sym.is(Method)) defDef(sym.asTerm) | ||
else valDef(sym.asTerm) | ||
else if (sym.isType) typeDefFromSym(sym.asType) | ||
else if (sym.is(Method)) defDefFromSym(sym.asTerm) | ||
else valDefFromSym(sym.asTerm) | ||
} | ||
|
||
def packageDef(sym: Symbol)(implicit ctx: Context): PackageDefinition = PackageDefinitionImpl(sym) | ||
def packageDefFromSym(sym: Symbol)(implicit ctx: Context): PackageDefinition = PackageDefinitionImpl(sym) | ||
|
||
def classDef(cls: ClassSymbol)(implicit ctx: Context): tpd.Tree = { | ||
val constrSym = cls.unforcedDecls.find(_.isPrimaryConstructor) | ||
if (!constrSym.exists) return tpd.EmptyTree | ||
def classDef(cls: ClassSymbol)(implicit ctx: Context): ClassDef = { | ||
val constrSym = cls.unforcedDecls.find(_.isPrimaryConstructor).orElse( | ||
// Dummy constructor for classes such as `<refinement>` | ||
ctx.newSymbol(cls, nme.CONSTRUCTOR, EmptyFlags, NoType) | ||
) | ||
val constr = tpd.DefDef(constrSym.asTerm) | ||
val parents = cls.classParents.map(tpd.TypeTree(_)) | ||
val body = cls.unforcedDecls.filter(!_.isPrimaryConstructor).map(s => definition(s)) | ||
val body = cls.unforcedDecls.filter(!_.isPrimaryConstructor).map(s => definitionFromSym(s)) | ||
tpd.ClassDefWithParents(cls, constr, parents, body) | ||
} | ||
|
||
def typeDef(sym: TypeSymbol)(implicit ctx: Context): tpd.TypeDef = tpd.TypeDef(sym) | ||
def typeDefFromSym(sym: TypeSymbol)(implicit ctx: Context): tpd.TypeDef = tpd.TypeDef(sym) | ||
|
||
def defDef(sym: TermSymbol)(implicit ctx: Context): tpd.DefDef = tpd.DefDef(sym) | ||
def defDefFromSym(sym: TermSymbol)(implicit ctx: Context): tpd.DefDef = tpd.DefDef(sym) | ||
|
||
def valDef(sym: TermSymbol)(implicit ctx: Context): tpd.ValDef = tpd.ValDef(sym) | ||
def valDefFromSym(sym: TermSymbol)(implicit ctx: Context): tpd.ValDef = tpd.ValDef(sym) | ||
|
||
} |
78 changes: 78 additions & 0 deletions
78
compiler/src/dotty/tools/dotc/tastyreflect/StandardDefinitions.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package dotty.tools.dotc.tastyreflect | ||
|
||
import dotty.tools.dotc.core.Symbols._ | ||
|
||
trait StandardDefinitions extends scala.tasty.reflect.StandardDefinitions with FromSymbol { | ||
tasty: TastyImpl => | ||
|
||
private implicit def ctx: Context = rootContext | ||
|
||
val definitions: DefinitionsApi = new DefinitionsApi { | ||
|
||
def RootPackage: PackageDef = packageDefFromSym(defn.RootPackage) | ||
|
||
def ScalaPackage: PackageDef = packageDefFromSym(defn.ScalaPackageVal) | ||
|
||
def AnyClass: ClassDef = classDef(defn.AnyClass) | ||
def AnyValClass: ClassDef = classDef(defn.AnyValClass) | ||
def ObjectClass: ClassDef = classDef(defn.ObjectClass) | ||
def AnyRefClass: TypeDef = typeDefFromSym(defn.AnyRefAlias) | ||
def NullClass: ClassDef = classDef(defn.AnyClass) | ||
def NothingClass: ClassDef = classDef(defn.NothingClass) | ||
def UnitClass: ClassDef = classDef(defn.UnitClass) | ||
def ByteClass: ClassDef = classDef(defn.ByteClass) | ||
def ShortClass: ClassDef = classDef(defn.ShortClass) | ||
def CharClass: ClassDef = classDef(defn.CharClass) | ||
def IntClass: ClassDef = classDef(defn.IntClass) | ||
def LongClass: ClassDef = classDef(defn.LongClass) | ||
def FloatClass: ClassDef = classDef(defn.FloatClass) | ||
def DoubleClass: ClassDef = classDef(defn.DoubleClass) | ||
def BooleanClass: ClassDef = classDef(defn.BooleanClass) | ||
def StringClass: ClassDef = classDef(defn.StringClass) | ||
def ClassClass: ClassDef = classDef(defn.ClassClass) | ||
def ArrayClass: ClassDef = classDef(defn.ArrayClass) | ||
def PredefModule: ValDef = valDefFromSym(defn.ScalaPredefModule.asTerm) | ||
|
||
def JavaLangPackage: PackageDef = packageDefFromSym(defn.JavaLangPackageVal) | ||
|
||
def ArrayModule: ValDef = valDefFromSym(defn.ArrayClass.companionModule.asTerm) | ||
|
||
def Array_apply: DefDef = defDefFromSym(defn.Array_apply.asTerm) | ||
def Array_clone: DefDef = defDefFromSym(defn.Array_clone.asTerm) | ||
def Array_length: DefDef = defDefFromSym(defn.Array_length.asTerm) | ||
def Array_update: DefDef = defDefFromSym(defn.Array_update.asTerm) | ||
|
||
def RepeatedParamClass: ClassDef = classDef(defn.RepeatedParamClass) | ||
|
||
def OptionClass: TypeDef = classDef(defn.OptionClass) | ||
def NoneModule: ValDef = valDefFromSym(defn.NoneClass.companionModule.asTerm) | ||
def SomeModule: ValDef = valDefFromSym(defn.SomeClass.companionModule.asTerm) | ||
|
||
def ProductClass: ClassDef = classDef(defn.ProductClass) | ||
def FunctionClass(arity: Int, isImplicit: Boolean = false, isErased: Boolean = false): ClassDef = | ||
classDef(defn.FunctionClass(arity, isImplicit, isErased).asClass) | ||
def TupleClass(arity: Int): ClassDef = classDef(defn.TupleType(arity).classSymbol.asClass) | ||
|
||
|
||
def ScalaPrimitiveValueClasses: List[ClassDef] = | ||
UnitClass :: BooleanClass :: ScalaNumericValueClasses | ||
def ScalaNumericValueClasses: List[ClassDef] = | ||
ByteClass :: ShortClass :: IntClass :: LongClass :: FloatClass :: DoubleClass :: CharClass :: Nil | ||
|
||
def UnitTpe: Type = defn.UnitType | ||
def ByteTpe: Type = defn.ByteType | ||
def ShortTpe: Type = defn.ShortType | ||
def CharTpe: Type = defn.CharType | ||
def IntTpe: Type = defn.IntType | ||
def LongTpe: Type = defn.LongType | ||
def FloatTpe: Type = defn.FloatType | ||
def DoubleTpe: Type = defn.DoubleType | ||
def BooleanTpe: Type = defn.BooleanType | ||
def AnyTpe: Type = defn.AnyType | ||
def AnyValTpe: Type = defn.AnyValType | ||
def AnyRefTpe: Type = defn.AnyRefType | ||
def ObjectTpe: Type = defn.ObjectType | ||
def NothingTpe: Type = defn.NothingType | ||
def NullTpe: Type = defn.NullType | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems
tasty: TastyImpl
is not used in the implementation? What's the motivation to change fromobject
totrait
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is not needed, I will revert it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted