/* Ċ Ġ* ĠCopyright Ġ 2 0 1 0 - 2 0 2 0 ĠJetBrains Ġs . r . o . Ġand ĠKotlin ĠProgramming ĠLanguage Ġcontributors . Ċ Ġ* ĠUse Ġof Ġthis Ġsource Ġcode Ġis Ġgoverned Ġby Ġthe ĠApache Ġ 2 . 0 Ġlicense Ġthat Ġcan Ġbe Ġfound Ġin Ġthe Ġlicense / LICENSE . txt Ġfile . Ċ Ġ*/ Ċ Ċ package Ġorg . jetbrains . kotlin . backend . jvm . codegen Ċ Ċ import Ġorg . jetbrains . kotlin . backend . common . Code gen Util Ċ import Ġorg . jetbrains . kotlin . backend . common . lower . all Over ridden Ċ import Ġorg . jetbrains . kotlin . backend . jvm . Jvm Backend Context Ċ import Ġorg . jetbrains . kotlin . backend . jvm . Jvm Lower ed Declaration Origin Ċ import Ġorg . jetbrains . kotlin . backend . jvm . lower . suspend Function Original Ċ import Ġorg . jetbrains . kotlin . codegen . Class Builder Ċ import Ġorg . jetbrains . kotlin . codegen . coroutines . Coroutine Transformer Method Visitor Ċ import Ġorg . jetbrains . kotlin . codegen . coroutines . IN VOKE _ SUS PEND _ METHOD _ NAME Ċ import Ġorg . jetbrains . kotlin . codegen . coroutines . report Sus pension Point Inside Monitor Ċ import Ġorg . jetbrains . kotlin . codegen . inline . add Fake Continuation Constructor Call Marker Ċ import Ġorg . jetbrains . kotlin . config . is Release Cor outines Ċ import Ġorg . jetbrains . kotlin . ir . UNDEFINED _ OFFSET Ċ import Ġorg . jetbrains . kotlin . ir . declarations . Ir Declaration Origin Ċ import Ġorg . jetbrains . kotlin . ir . declarations . Ir Function Ċ import Ġorg . jetbrains . kotlin . ir . declarations . Ir Simple Function Ċ import Ġorg . jetbrains . kotlin . ir . declarations . name Ċ import Ġorg . jetbrains . kotlin . ir . expressions . Ir Expression Ċ import Ġorg . jetbrains . kotlin . ir . expressions . impl . Ir Error Expression Impl Ċ import Ġorg . jetbrains . kotlin . ir . types . create Type Ċ import Ġorg . jetbrains . kotlin . ir . types . impl . make Type Projection Ċ import Ġorg . jetbrains . kotlin . ir . types . is Unit Ċ import Ġorg . jetbrains . kotlin . ir . util . file Ċ import Ġorg . jetbrains . kotlin . ir . util . is Suspend Ċ import Ġorg . jetbrains . kotlin . ir . util . parent As Class Ċ import Ġorg . jetbrains . kotlin . psi . Kt Element Ċ import Ġorg . jetbrains . kotlin . resolve . jvm . Asm Types Ċ import Ġorg . jetbrains . kotlin . resolve . jvm . jvm Signature . Jvm Method Generic Signature Ċ import Ġorg . jetbrains . kotlin . types . Variance Ċ import Ġorg . jetbrains . org . object web . asm . Method Visitor Ċ import Ġorg . jetbrains . org . object web . asm . Type Ċ import Ġorg . jetbrains . org . object web . asm . commons . Instruction Adapter Ċ Ċ internal Ġfun Ġgenerate StateMachine For Named Function ( ĊĠĠĠ Ġir Function : ĠIr Function , ĊĠĠĠ Ġclass Code gen : ĠClass Code gen , ĊĠĠĠ Ġmethod Visitor : ĠMethod Visitor , ĊĠĠĠ Ġaccess : ĠInt , ĊĠĠĠ Ġsignature : ĠJ vm Method Generic Signature , ĊĠĠĠ Ġobtain Continuation Class Builder : Ġ() Ġ-> ĠClass Builder , ĊĠĠĠ Ġelement : ĠK t Element Ċ ): ĠMethod Visitor Ġ{ ĊĠĠĠ Ġassert ( ir Function . is Suspend ) ĊĠĠĠ Ġval Ġstate Ġ= Ġclass Code gen . state ĊĠĠĠ Ġval Ġlanguage Version Settings Ġ= Ġstate . language Version Settings ĊĠĠĠ Ġassert ( language Version Settings . is Release Cor outines ()) Ġ{ Ġ" Experimental Ġcor outines Ġare Ġunsupported Ġin ĠJVM _ IR Ġbackend " Ġ} ĊĠĠĠ Ġreturn ĠCor outine Transformer Method Visitor ( ĊĠĠĠĠĠĠĠ Ġmethod Visitor , Ġaccess , Ġsignature . asm Method . name , Ġsignature . asm Method . descriptor , Ġnull , Ġnull , ĊĠĠĠĠĠĠĠ Ġobtain Class Builder For Coroutine State Ġ= Ġobtain Continuation Class Builder , ĊĠĠĠĠĠĠĠ Ġreport Sus pension Point Inside Monitor Ġ= Ġ{ Ġreport Sus pension Point Inside Monitor ( element , Ġstate , Ġit ) Ġ}, ĊĠĠĠĠĠĠĠ ĠlineNumber Ġ= ĠCode gen Util . get LineNumber For Element ( element , Ġfalse ) Ġ?: Ġ 0 , ĊĠĠĠĠĠĠĠ Ġsource File Ġ= Ġclass Code gen . ir Class . file . name , ĊĠĠĠĠĠĠĠ Ġlanguage Version Settings Ġ= Ġlanguage Version Settings , ĊĠĠĠĠĠĠĠ Ġshould Preserve Class Initialization Ġ= Ġstate . constructor Call Normalization Mode . should Preserve Class Initialization , ĊĠĠĠĠĠĠĠ Ġcontaining Class Internal Name Ġ= Ġclass Code gen . visitor . this Name , ĊĠĠĠĠĠĠĠ Ġis For Named Function Ġ= Ġtrue , ĊĠĠĠĠĠĠĠ Ġneed Dispatch Receiver Ġ= Ġir Function . dispatch Receiver Parameter Ġ!= Ġnull ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ|| Ġir Function . origin Ġ== ĠJ vm Lower ed Declaration Origin . SUS PEND _ IMPL _ STATIC _ FUNCTION , ĊĠĠĠĠĠĠĠ Ġinternal Name For Dispatch Receiver Ġ= Ġclass Code gen . visitor . this Name , ĊĠĠĠĠĠĠĠ Ġput Continuation Parameter To L vt Ġ= Ġfalse , ĊĠĠĠĠĠĠĠ Ġdisable Tail Call Optimization For Function Return ing Unit Ġ= Ġir Function . suspend Function Original (). let Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġit . returnType . is Unit () Ġ&& Ġit . any Of Over ridden Functions Returns Non Unit () ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ) Ċ } Ċ Ċ internal Ġfun ĠIr Function . any Of Over ridden Functions Returns Non Unit (): ĠBoolean Ġ{ ĊĠĠĠ Ġreturn Ġ( this Ġas ? ĠIr Simple Function )?. all Over ridden ()?. toList ()?. let Ġ{ Ġfunctions Ġ-> ĊĠĠĠĠĠĠĠ Ġfunctions . isNotEmpty () Ġ&& Ġfunctions . any Ġ{ Ġ! it . returnType . is Unit () Ġ} ĊĠĠĠ Ġ} Ġ== Ġtrue Ċ } Ċ Ċ internal Ġfun Ġgenerate StateMachine For Lambda ( ĊĠĠĠ Ġclass Code gen : ĠClass Code gen , ĊĠĠĠ Ġmethod Visitor : ĠMethod Visitor , ĊĠĠĠ Ġaccess : ĠInt , ĊĠĠĠ Ġsignature : ĠJ vm Method Generic Signature , ĊĠĠĠ Ġelement : ĠK t Element Ċ ): ĠMethod Visitor Ġ{ ĊĠĠĠ Ġval Ġstate Ġ= Ġclass Code gen . state ĊĠĠĠ Ġval Ġlanguage Version Settings Ġ= Ġstate . language Version Settings ĊĠĠĠ Ġassert ( language Version Settings . is Release Cor outines ()) Ġ{ Ġ" Experimental Ġcor outines Ġare Ġunsupported Ġin ĠJVM _ IR Ġbackend " Ġ} ĊĠĠĠ Ġreturn ĠCor outine Transformer Method Visitor ( ĊĠĠĠĠĠĠĠ Ġmethod Visitor , Ġaccess , Ġsignature . asm Method . name , Ġsignature . asm Method . descriptor , Ġsignature . gener ics Signature , Ġnull , ĊĠĠĠĠĠĠĠ Ġobtain Class Builder For Coroutine State Ġ= Ġ{ Ġclass Code gen . visitor Ġ}, ĊĠĠĠĠĠĠĠ Ġreport Sus pension Point Inside Monitor Ġ= Ġ{ Ġreport Sus pension Point Inside Monitor ( element , Ġstate , Ġit ) Ġ}, ĊĠĠĠĠĠĠĠ ĠlineNumber Ġ= ĠCode gen Util . get LineNumber For Element ( element , Ġfalse ) Ġ?: Ġ 0 , ĊĠĠĠĠĠĠĠ Ġsource File Ġ= Ġclass Code gen . ir Class . file . name , ĊĠĠĠĠĠĠĠ Ġlanguage Version Settings Ġ= Ġlanguage Version Settings , ĊĠĠĠĠĠĠĠ Ġshould Preserve Class Initialization Ġ= Ġstate . constructor Call Normalization Mode . should Preserve Class Initialization , ĊĠĠĠĠĠĠĠ Ġcontaining Class Internal Name Ġ= Ġclass Code gen . visitor . this Name , ĊĠĠĠĠĠĠĠ Ġis For Named Function Ġ= Ġfalse , ĊĠĠĠĠĠĠĠ Ġdisable Tail Call Optimization For Function Return ing Unit Ġ= Ġfalse ĊĠĠĠ Ġ) Ċ } Ċ Ċ internal Ġfun ĠIr Function . is Invoke Suspend Of Lambda (): ĠBoolean Ġ= ĊĠĠĠ Ġname . asString () Ġ== ĠIN VOKE _ SUS PEND _ METHOD _ NAME Ġ&& Ġparent As Class . origin Ġ== ĠJ vm Lower ed Declaration Origin . SUS PEND _ L AMBDA Ċ Ċ internal Ġfun ĠIr Function . is Invoke Suspend For Inline Of Lambda (): ĠBoolean Ġ= ĊĠĠĠ Ġorigin Ġ== ĠJ vm Lower ed Declaration Origin . FOR _ INLINE _ STATE _ MACHINE _ TEMPLATE ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ&& Ġparent As Class . origin Ġ== ĠJ vm Lower ed Declaration Origin . SUS PEND _ L AMBDA Ċ Ċ internal Ġfun ĠIr Function . is Invoke Suspend Of Continuation (): ĠBoolean Ġ= ĊĠĠĠ Ġname . asString () Ġ== ĠIN VOKE _ SUS PEND _ METHOD _ NAME Ġ&& Ġparent As Class . origin Ġ== ĠJ vm Lower ed Declaration Origin . CONT IN UATION _ CLASS Ċ Ċ internal Ġfun ĠIr Function . is Invoke Of Suspend Callable Reference (): ĠBoolean Ġ= Ġis Suspend Ġ&& Ġname . asString () Ġ== Ġ" invoke " Ġ&& ĊĠĠĠĠĠĠĠ Ġparent As Class . origin Ġ== ĠJ vm Lower ed Declaration Origin . FUNCTION _ REFERENCE _ IMPL Ċ Ċ internal Ġfun ĠIr Function . is Known ToBe Tail Call (): ĠBoolean Ġ= ĊĠĠĠ Ġwhen Ġ( origin ) Ġ{ ĊĠĠĠĠĠĠĠ ĠIr Declaration Origin . FUNCTION _ FOR _ DEFAULT _ PARAMETER , ĊĠĠĠĠĠĠĠ ĠJ vm Lower ed Declaration Origin . SYN TH ET IC _ ACCESS OR , ĊĠĠĠĠĠĠĠ ĠJ vm Lower ed Declaration Origin . DEFAULT _ IMP LS _ BR ID GE , ĊĠĠĠĠĠĠĠ ĠJ vm Lower ed Declaration Origin . DEFAULT _ IMP LS _ BR ID GE _ TO _ SYN TH ET IC , ĊĠĠĠĠĠĠĠ ĠJ vm Lower ed Declaration Origin . GENERATED _ MEMBER _ IN _ CALL ABLE _ REFERENCE , ĊĠĠĠĠĠĠĠ ĠIr Declaration Origin . BR ID GE , ĊĠĠĠĠĠĠĠ ĠIr Declaration Origin . BR ID GE _ SPECIAL , ĊĠĠĠĠĠĠĠ ĠIr Declaration Origin . DE LEG ATED _ MEMBER Ġ-> Ġtrue ĊĠĠĠĠĠĠĠ Ġelse Ġ-> Ġis Invoke Of Suspend Callable Reference () ĊĠĠĠ Ġ} Ċ Ċ internal Ġfun ĠIr Function . should Not Contain Suspend Markers (): ĠBoolean Ġ= ĊĠĠĠ Ġis Invoke Suspend Of Continuation () Ġ|| Ġis Known ToBe Tail Call () Ċ Ċ internal Ġfun Ġcreate Fake Continuation ( context : ĠJ vm Backend Context ): ĠIr Expression Ġ= ĠIr Error Expression Impl ( ĊĠĠĠ ĠUN DEFINED _ OFFSET , ĊĠĠĠ ĠUN DEFINED _ OFFSET , ĊĠĠĠ Ġcontext . ir . symbols . continu ation Class . create Type ( true , ĠlistOf ( make Type Projection ( context . ir Built Ins . any N Type , ĠVari ance . IN VARIANT ))), ĊĠĠĠ Ġ" FA KE _ CONT IN UATION " Ċ ) Ċ Ċ internal Ġfun Ġgenerate Fake Continuation Constructor Call ( ĊĠĠĠ Ġv : ĠInstruction Adapter , ĊĠĠĠ Ġcontaining Class Builder : ĠClass Builder , ĊĠĠĠ Ġclass Builder : ĠClass Builder , ĊĠĠĠ Ġir Function : ĠIr Function Ċ ) Ġ{ ĊĠĠĠ Ġval Ġcontinuation Type Ġ= ĠType . get ObjectType ( class Builder . this Name ) ĊĠĠĠ Ġ// ĠTODO : ĠThis Ġis Ġdifferent Ġin Ġcase Ġof ĠDefault Imp ls ĊĠĠĠ Ġval Ġthis Name Type Ġ= ĠType . get ObjectType ( containing Class Builder . this Name . replace (".", Ġ"/ ")) ĊĠĠĠ Ġval Ġcontinuation Index Ġ= ĠlistOf NotNull ( ir Function . dispatch Receiver Parameter , Ġir Function . extension Receiver Parameter ). size Ġ+ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġir Function . value Parameters . size Ġ- Ġ 1 ĊĠĠĠ Ġwith ( v ) Ġ{ ĊĠĠĠĠĠĠĠ Ġadd Fake Continuation Constructor Call Marker ( this , Ġtrue ) ĊĠĠĠĠĠĠĠ Ġan ew ( continu ation Type ) ĊĠĠĠĠĠĠĠ Ġdup () ĊĠĠĠĠĠĠĠ Ġif Ġ( ir Function . dispatch Receiver Parameter Ġ!= Ġnull ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġload ( 0 , ĠA sm Types . OBJECT _ TYPE ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġload ( continu ation Index , ĠType . get ObjectType (" kotlin / coroutines / Continuation ")) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġinvok espec ial ( continu ation Type . internal Name , Ġ"< init >", Ġ" (${ this Name Type } L kotlin / coroutines / Continuation ;) V ", Ġfalse ) ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġload ( continu ation Index , ĠType . get ObjectType (" kotlin / coroutines / Continuation ")) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġinvok espec ial ( continu ation Type . internal Name , Ġ"< init >", Ġ"( L kotlin / coroutines / Continuation ;) V ", Ġfalse ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġadd Fake Continuation Constructor Call Marker ( this , Ġfalse ) ĊĠĠĠĠĠĠĠ Ġpop () ĊĠĠĠ Ġ} Ċ }