ncnv.pas 125 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Type checking and register allocation for type converting nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncnv;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,
  22. symtype,
  23. defutil,defcmp,
  24. nld
  25. ;
  26. type
  27. ttypeconvnode = class(tunarynode)
  28. totypedef : tdef;
  29. totypedefderef : tderef;
  30. convtype : tconverttype;
  31. warn_pointer_to_signed: boolean;
  32. constructor create(node : tnode;def:tdef);virtual;
  33. constructor create_explicit(node : tnode;def:tdef);
  34. constructor create_internal(node : tnode;def:tdef);
  35. constructor create_proc_to_procvar(node : tnode);
  36. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  37. procedure ppuwrite(ppufile:tcompilerppufile);override;
  38. procedure buildderefimpl;override;
  39. procedure derefimpl;override;
  40. function dogetcopy : tnode;override;
  41. function actualtargetnode: tnode;override;
  42. procedure printnodeinfo(var t : text);override;
  43. function pass_1 : tnode;override;
  44. function pass_typecheck:tnode;override;
  45. function simplify:tnode; override;
  46. procedure mark_write;override;
  47. function docompare(p: tnode) : boolean; override;
  48. function assign_allowed:boolean;
  49. procedure second_call_helper(c : tconverttype);
  50. private
  51. function typecheck_int_to_int : tnode;
  52. function typecheck_cord_to_pointer : tnode;
  53. function typecheck_chararray_to_string : tnode;
  54. function typecheck_string_to_chararray : tnode;
  55. function typecheck_string_to_string : tnode;
  56. function typecheck_char_to_string : tnode;
  57. function typecheck_char_to_chararray : tnode;
  58. function typecheck_int_to_real : tnode;
  59. function typecheck_real_to_real : tnode;
  60. function typecheck_real_to_currency : tnode;
  61. function typecheck_cchar_to_pchar : tnode;
  62. function typecheck_cstring_to_pchar : tnode;
  63. function typecheck_cstring_to_int : tnode;
  64. function typecheck_char_to_char : tnode;
  65. function typecheck_arrayconstructor_to_set : tnode;
  66. function typecheck_set_to_set : tnode;
  67. function typecheck_pchar_to_string : tnode;
  68. function typecheck_interface_to_string : tnode;
  69. function typecheck_interface_to_guid : tnode;
  70. function typecheck_dynarray_to_openarray : tnode;
  71. function typecheck_pwchar_to_string : tnode;
  72. function typecheck_variant_to_dynarray : tnode;
  73. function typecheck_dynarray_to_variant : tnode;
  74. function typecheck_call_helper(c : tconverttype) : tnode;
  75. function typecheck_variant_to_enum : tnode;
  76. function typecheck_enum_to_variant : tnode;
  77. function typecheck_proc_to_procvar : tnode;
  78. function typecheck_variant_to_interface : tnode;
  79. function typecheck_interface_to_variant : tnode;
  80. function typecheck_array_2_dynarray : tnode;
  81. protected
  82. function first_int_to_int : tnode;virtual;
  83. function first_cstring_to_pchar : tnode;virtual;
  84. function first_cstring_to_int : tnode;virtual;
  85. function first_string_to_chararray : tnode;virtual;
  86. function first_char_to_string : tnode;virtual;
  87. function first_nothing : tnode;virtual;
  88. function first_array_to_pointer : tnode;virtual;
  89. function first_int_to_real : tnode;virtual;
  90. function first_real_to_real : tnode;virtual;
  91. function first_pointer_to_array : tnode;virtual;
  92. function first_cchar_to_pchar : tnode;virtual;
  93. function first_bool_to_int : tnode;virtual;
  94. function first_int_to_bool : tnode;virtual;
  95. function first_bool_to_bool : tnode;virtual;
  96. function first_proc_to_procvar : tnode;virtual;
  97. function first_nil_to_methodprocvar : tnode;virtual;
  98. function first_set_to_set : tnode;virtual;
  99. function first_cord_to_pointer : tnode;virtual;
  100. function first_ansistring_to_pchar : tnode;virtual;
  101. function first_arrayconstructor_to_set : tnode;virtual;
  102. function first_class_to_intf : tnode;virtual;
  103. function first_char_to_char : tnode;virtual;
  104. function first_call_helper(c : tconverttype) : tnode;
  105. { these wrapper are necessary, because the first_* stuff is called }
  106. { through a table. Without the wrappers override wouldn't have }
  107. { any effect }
  108. function _first_int_to_int : tnode;
  109. function _first_cstring_to_pchar : tnode;
  110. function _first_cstring_to_int : tnode;
  111. function _first_string_to_chararray : tnode;
  112. function _first_char_to_string : tnode;
  113. function _first_nothing : tnode;
  114. function _first_array_to_pointer : tnode;
  115. function _first_int_to_real : tnode;
  116. function _first_real_to_real: tnode;
  117. function _first_pointer_to_array : tnode;
  118. function _first_cchar_to_pchar : tnode;
  119. function _first_bool_to_int : tnode;
  120. function _first_int_to_bool : tnode;
  121. function _first_bool_to_bool : tnode;
  122. function _first_proc_to_procvar : tnode;
  123. function _first_nil_to_methodprocvar : tnode;
  124. function _first_cord_to_pointer : tnode;
  125. function _first_ansistring_to_pchar : tnode;
  126. function _first_arrayconstructor_to_set : tnode;
  127. function _first_class_to_intf : tnode;
  128. function _first_char_to_char : tnode;
  129. function _first_set_to_set : tnode;
  130. procedure _second_int_to_int;virtual;
  131. procedure _second_string_to_string;virtual;
  132. procedure _second_cstring_to_pchar;virtual;
  133. procedure _second_cstring_to_int;virtual;
  134. procedure _second_string_to_chararray;virtual;
  135. procedure _second_array_to_pointer;virtual;
  136. procedure _second_pointer_to_array;virtual;
  137. procedure _second_chararray_to_string;virtual;
  138. procedure _second_char_to_string;virtual;
  139. procedure _second_int_to_real;virtual;
  140. procedure _second_real_to_real;virtual;
  141. procedure _second_cord_to_pointer;virtual;
  142. procedure _second_proc_to_procvar;virtual;
  143. procedure _second_nil_to_methodprocvar;virtual;
  144. procedure _second_bool_to_int;virtual;
  145. procedure _second_int_to_bool;virtual;
  146. procedure _second_bool_to_bool;virtual;
  147. procedure _second_set_to_set;virtual;
  148. procedure _second_ansistring_to_pchar;virtual;
  149. procedure _second_class_to_intf;virtual;
  150. procedure _second_char_to_char;virtual;
  151. procedure _second_nothing; virtual;
  152. procedure second_int_to_int;virtual;abstract;
  153. procedure second_string_to_string;virtual;abstract;
  154. procedure second_cstring_to_pchar;virtual;abstract;
  155. procedure second_cstring_to_int;virtual;abstract;
  156. procedure second_string_to_chararray;virtual;abstract;
  157. procedure second_array_to_pointer;virtual;abstract;
  158. procedure second_pointer_to_array;virtual;abstract;
  159. procedure second_chararray_to_string;virtual;abstract;
  160. procedure second_char_to_string;virtual;abstract;
  161. procedure second_int_to_real;virtual;abstract;
  162. procedure second_real_to_real;virtual;abstract;
  163. procedure second_cord_to_pointer;virtual;abstract;
  164. procedure second_proc_to_procvar;virtual;abstract;
  165. procedure second_nil_to_methodprocvar;virtual;abstract;
  166. procedure second_bool_to_int;virtual;abstract;
  167. procedure second_int_to_bool;virtual;abstract;
  168. procedure second_bool_to_bool;virtual;abstract;
  169. procedure second_set_to_set;virtual;abstract;
  170. procedure second_ansistring_to_pchar;virtual;abstract;
  171. procedure second_class_to_intf;virtual;abstract;
  172. procedure second_char_to_char;virtual;abstract;
  173. procedure second_nothing; virtual;abstract;
  174. end;
  175. ttypeconvnodeclass = class of ttypeconvnode;
  176. tasnode = class(tbinarynode)
  177. constructor create(l,r : tnode);virtual;
  178. function pass_1 : tnode;override;
  179. function pass_typecheck:tnode;override;
  180. function dogetcopy: tnode;override;
  181. destructor destroy; override;
  182. call: tnode;
  183. end;
  184. tasnodeclass = class of tasnode;
  185. tisnode = class(tbinarynode)
  186. constructor create(l,r : tnode);virtual;
  187. function pass_1 : tnode;override;
  188. function pass_typecheck:tnode;override;
  189. procedure pass_generate_code;override;
  190. end;
  191. tisnodeclass = class of tisnode;
  192. var
  193. ctypeconvnode : ttypeconvnodeclass;
  194. casnode : tasnodeclass;
  195. cisnode : tisnodeclass;
  196. procedure inserttypeconv(var p:tnode;def:tdef);
  197. procedure inserttypeconv_internal(var p:tnode;def:tdef);
  198. procedure arrayconstructor_to_set(var p : tnode);
  199. procedure insert_varargstypeconv(var p : tnode; iscvarargs: boolean);
  200. implementation
  201. uses
  202. globtype,systems,constexp,
  203. cutils,verbose,globals,widestr,
  204. symconst,symdef,symsym,symbase,symtable,
  205. ncon,ncal,nset,nadd,ninl,nmem,nmat,nbas,nutils,
  206. cgbase,procinfo,
  207. htypechk,pass_1,cpuinfo;
  208. {*****************************************************************************
  209. Helpers
  210. *****************************************************************************}
  211. procedure inserttypeconv(var p:tnode;def:tdef);
  212. begin
  213. if not assigned(p.resultdef) then
  214. begin
  215. typecheckpass(p);
  216. if codegenerror then
  217. exit;
  218. end;
  219. { don't insert obsolete type conversions }
  220. if equal_defs(p.resultdef,def) then
  221. p.resultdef:=def
  222. else
  223. begin
  224. p:=ctypeconvnode.create(p,def);
  225. typecheckpass(p);
  226. end;
  227. end;
  228. procedure inserttypeconv_internal(var p:tnode;def:tdef);
  229. begin
  230. if not assigned(p.resultdef) then
  231. begin
  232. typecheckpass(p);
  233. if codegenerror then
  234. exit;
  235. end;
  236. { don't insert obsolete type conversions }
  237. if equal_defs(p.resultdef,def) then
  238. p.resultdef:=def
  239. else
  240. begin
  241. p:=ctypeconvnode.create_internal(p,def);
  242. typecheckpass(p);
  243. end;
  244. end;
  245. {*****************************************************************************
  246. Array constructor to Set Conversion
  247. *****************************************************************************}
  248. procedure arrayconstructor_to_set(var p : tnode);
  249. var
  250. constp : tsetconstnode;
  251. buildp,
  252. p2,p3,p4 : tnode;
  253. hdef : tdef;
  254. constset : Pconstset;
  255. constsetlo,
  256. constsethi : TConstExprInt;
  257. procedure update_constsethi(def:tdef; maybetruncenumrange: boolean);
  258. begin
  259. if (def.typ=orddef) and
  260. ((torddef(def).high>=constsethi) or
  261. (torddef(def).low <=constsetlo)) then
  262. begin
  263. if torddef(def).ordtype=uwidechar then
  264. begin
  265. constsethi:=255;
  266. constsetlo:=0;
  267. if hdef=nil then
  268. hdef:=def;
  269. end
  270. else
  271. begin
  272. if (torddef(def).high>=constsethi) then
  273. constsethi:=torddef(def).high;
  274. if (torddef(def).low<=constsetlo) then
  275. constsetlo:=torddef(def).low;
  276. if hdef=nil then
  277. begin
  278. if (constsethi>255) or
  279. (torddef(def).low<0) then
  280. hdef:=u8inttype
  281. else
  282. hdef:=def;
  283. end;
  284. if constsethi>255 then
  285. constsethi:=255;
  286. if constsetlo<0 then
  287. constsetlo:=0;
  288. end;
  289. end
  290. else if (def.typ=enumdef) and
  291. ((tenumdef(def).max>=constsethi) or
  292. (tenumdef(def).min<=constsetlo)) then
  293. begin
  294. if hdef=nil then
  295. hdef:=def;
  296. if (tenumdef(def).max>=constsethi) then
  297. constsethi:=tenumdef(def).max;
  298. if (tenumdef(def).min<=constsetlo) then
  299. constsetlo:=tenumdef(def).min;
  300. { for constant set elements, delphi allows the usage of elements of enumerations which
  301. have value>255 if there is no element with a value > 255 used }
  302. if (maybetruncenumrange) and
  303. (m_delphi in current_settings.modeswitches) then
  304. begin
  305. if constsethi>255 then
  306. constsethi:=255;
  307. if constsetlo<0 then
  308. constsetlo:=0;
  309. end;
  310. end;
  311. end;
  312. procedure do_set(pos : longint);
  313. begin
  314. if (pos and not $ff)<>0 then
  315. Message(parser_e_illegal_set_expr);
  316. if pos>constsethi then
  317. constsethi:=pos;
  318. if pos<constsetlo then
  319. constsetlo:=pos;
  320. if pos in constset^ then
  321. Message(parser_e_illegal_set_expr);
  322. include(constset^,pos);
  323. end;
  324. var
  325. l : Longint;
  326. lr,hr : TConstExprInt;
  327. hp : tarrayconstructornode;
  328. begin
  329. if p.nodetype<>arrayconstructorn then
  330. internalerror(200205105);
  331. new(constset);
  332. constset^:=[];
  333. hdef:=nil;
  334. { make sure to set constsetlo correctly for empty sets }
  335. if assigned(tarrayconstructornode(p).left) then
  336. constsetlo:=high(aint)
  337. else
  338. constsetlo:=0;
  339. constsethi:=0;
  340. constp:=csetconstnode.create(nil,hdef);
  341. constp.value_set:=constset;
  342. buildp:=constp;
  343. hp:=tarrayconstructornode(p);
  344. if assigned(hp.left) then
  345. begin
  346. while assigned(hp) do
  347. begin
  348. p4:=nil; { will contain the tree to create the set }
  349. {split a range into p2 and p3 }
  350. if hp.left.nodetype=arrayconstructorrangen then
  351. begin
  352. p2:=tarrayconstructorrangenode(hp.left).left;
  353. p3:=tarrayconstructorrangenode(hp.left).right;
  354. tarrayconstructorrangenode(hp.left).left:=nil;
  355. tarrayconstructorrangenode(hp.left).right:=nil;
  356. end
  357. else
  358. begin
  359. p2:=hp.left;
  360. hp.left:=nil;
  361. p3:=nil;
  362. end;
  363. typecheckpass(p2);
  364. set_varstate(p2,vs_read,[vsf_must_be_valid]);
  365. if assigned(p3) then
  366. begin
  367. typecheckpass(p3);
  368. set_varstate(p3,vs_read,[vsf_must_be_valid]);
  369. end;
  370. if codegenerror then
  371. break;
  372. current_filepos:=p2.fileinfo;
  373. case p2.resultdef.typ of
  374. enumdef,
  375. orddef:
  376. begin
  377. { widechars are not yet supported }
  378. if is_widechar(p2.resultdef) then
  379. begin
  380. inserttypeconv(p2,cchartype);
  381. if (p2.nodetype<>ordconstn) then
  382. incompatibletypes(cwidechartype,cchartype);
  383. end;
  384. getrange(p2.resultdef,lr,hr);
  385. if assigned(p3) then
  386. begin
  387. if is_widechar(p3.resultdef) then
  388. begin
  389. inserttypeconv(p3,cchartype);
  390. if (p3.nodetype<>ordconstn) then
  391. begin
  392. current_filepos:=p3.fileinfo;
  393. incompatibletypes(cwidechartype,cchartype);
  394. end;
  395. end;
  396. { this isn't good, you'll get problems with
  397. type t010 = 0..10;
  398. ts = set of t010;
  399. var s : ts;b : t010
  400. begin s:=[1,2,b]; end.
  401. if is_integer(p3^.resultdef) then
  402. begin
  403. inserttypeconv(p3,u8bitdef);
  404. end;
  405. }
  406. if assigned(hdef) and not(equal_defs(hdef,p3.resultdef)) then
  407. begin
  408. CGMessagePos(p3.fileinfo,type_e_typeconflict_in_set);
  409. end
  410. else
  411. begin
  412. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  413. begin
  414. if not(is_integer(p3.resultdef)) then
  415. hdef:=p3.resultdef
  416. else
  417. begin
  418. inserttypeconv(p3,u8inttype);
  419. inserttypeconv(p2,u8inttype);
  420. end;
  421. for l:=tordconstnode(p2).value.svalue to tordconstnode(p3).value.svalue do
  422. do_set(l);
  423. p2.free;
  424. p3.free;
  425. end
  426. else
  427. begin
  428. update_constsethi(p2.resultdef,false);
  429. inserttypeconv(p2,hdef);
  430. update_constsethi(p3.resultdef,false);
  431. inserttypeconv(p3,hdef);
  432. if assigned(hdef) then
  433. inserttypeconv(p3,hdef)
  434. else
  435. inserttypeconv(p3,u8inttype);
  436. p4:=csetelementnode.create(p2,p3);
  437. end;
  438. end;
  439. end
  440. else
  441. begin
  442. { Single value }
  443. if p2.nodetype=ordconstn then
  444. begin
  445. if not(is_integer(p2.resultdef)) then
  446. update_constsethi(p2.resultdef,true);
  447. if assigned(hdef) then
  448. inserttypeconv(p2,hdef)
  449. else
  450. inserttypeconv(p2,u8inttype);
  451. do_set(tordconstnode(p2).value.svalue);
  452. p2.free;
  453. end
  454. else
  455. begin
  456. update_constsethi(p2.resultdef,false);
  457. if assigned(hdef) then
  458. inserttypeconv(p2,hdef)
  459. else
  460. inserttypeconv(p2,u8inttype);
  461. p4:=csetelementnode.create(p2,nil);
  462. end;
  463. end;
  464. end;
  465. stringdef :
  466. begin
  467. { if we've already set elements which are constants }
  468. { throw an error }
  469. if ((hdef=nil) and assigned(buildp)) or
  470. not(is_char(hdef)) then
  471. CGMessage(type_e_typeconflict_in_set)
  472. else
  473. for l:=1 to length(pshortstring(tstringconstnode(p2).value_str)^) do
  474. do_set(ord(pshortstring(tstringconstnode(p2).value_str)^[l]));
  475. if hdef=nil then
  476. hdef:=cchartype;
  477. p2.free;
  478. end;
  479. else
  480. CGMessage(type_e_ordinal_expr_expected);
  481. end;
  482. { insert the set creation tree }
  483. if assigned(p4) then
  484. buildp:=caddnode.create(addn,buildp,p4);
  485. { load next and dispose current node }
  486. p2:=hp;
  487. hp:=tarrayconstructornode(tarrayconstructornode(p2).right);
  488. tarrayconstructornode(p2).right:=nil;
  489. p2.free;
  490. end;
  491. if (hdef=nil) then
  492. hdef:=u8inttype;
  493. end
  494. else
  495. begin
  496. { empty set [], only remove node }
  497. p.free;
  498. end;
  499. { set the initial set type }
  500. constp.resultdef:=tsetdef.create(hdef,constsetlo.svalue,constsethi.svalue);
  501. { determine the resultdef for the tree }
  502. typecheckpass(buildp);
  503. { set the new tree }
  504. p:=buildp;
  505. end;
  506. procedure insert_varargstypeconv(var p : tnode; iscvarargs: boolean);
  507. begin
  508. if not(iscvarargs) and
  509. (p.nodetype=stringconstn) then
  510. p:=ctypeconvnode.create_internal(p,cansistringtype)
  511. else
  512. case p.resultdef.typ of
  513. enumdef :
  514. p:=ctypeconvnode.create_internal(p,s32inttype);
  515. arraydef :
  516. begin
  517. if is_chararray(p.resultdef) then
  518. p:=ctypeconvnode.create_internal(p,charpointertype)
  519. else
  520. if is_widechararray(p.resultdef) then
  521. p:=ctypeconvnode.create_internal(p,widecharpointertype)
  522. else
  523. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  524. end;
  525. orddef :
  526. begin
  527. if is_integer(p.resultdef) and
  528. not(is_64bitint(p.resultdef)) then
  529. p:=ctypeconvnode.create(p,s32inttype)
  530. else if is_void(p.resultdef) then
  531. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename)
  532. else if iscvarargs and is_currency(p.resultdef)
  533. and (current_settings.fputype<>fpu_none) then
  534. p:=ctypeconvnode.create(p,s64floattype);
  535. end;
  536. floatdef :
  537. if not(iscvarargs) then
  538. begin
  539. if not(is_currency(p.resultdef)) then
  540. p:=ctypeconvnode.create(p,pbestrealtype^);
  541. end
  542. else
  543. begin
  544. if is_constrealnode(p) and
  545. not(nf_explicit in p.flags) then
  546. MessagePos(p.fileinfo,type_w_double_c_varargs);
  547. if (tfloatdef(p.resultdef).floattype in [s32real,s64currency]) or
  548. (is_constrealnode(p) and
  549. not(nf_explicit in p.flags)) then
  550. p:=ctypeconvnode.create(p,s64floattype);
  551. end;
  552. procvardef :
  553. p:=ctypeconvnode.create(p,voidpointertype);
  554. stringdef:
  555. if iscvarargs then
  556. p:=ctypeconvnode.create(p,charpointertype);
  557. variantdef:
  558. if iscvarargs then
  559. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  560. pointerdef:
  561. ;
  562. classrefdef:
  563. if iscvarargs then
  564. p:=ctypeconvnode.create(p,voidpointertype);
  565. objectdef :
  566. if iscvarargs or
  567. is_object(p.resultdef) then
  568. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  569. else
  570. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  571. end;
  572. typecheckpass(p);
  573. end;
  574. {*****************************************************************************
  575. TTYPECONVNODE
  576. *****************************************************************************}
  577. constructor ttypeconvnode.create(node : tnode;def:tdef);
  578. begin
  579. inherited create(typeconvn,node);
  580. convtype:=tc_none;
  581. totypedef:=def;
  582. if def=nil then
  583. internalerror(200103281);
  584. fileinfo:=node.fileinfo;
  585. {An attempt to convert the result of a floating point division
  586. (with the / operator) to an integer type will fail. Give a hint
  587. to use the div operator.}
  588. if (node.nodetype=slashn) and (def.typ=orddef) then
  589. cgmessage(type_h_use_div_for_int);
  590. {In expressions like int64:=longint+longint, an integer overflow could be avoided
  591. by simply converting the operands to int64 first. Give a hint to do this.}
  592. if (node.nodetype in [addn,subn,muln]) and
  593. (def.typ=orddef) and (node.resultdef<>nil) and (node.resultdef.typ=orddef) and
  594. ((Torddef(node.resultdef).low>=Torddef(def).low) and (Torddef(node.resultdef).high<=Torddef(def).high)) and
  595. ((Torddef(node.resultdef).low>Torddef(def).low) or (Torddef(node.resultdef).high<Torddef(def).high)) then
  596. case node.nodetype of
  597. addn:
  598. cgmessage1(type_h_convert_add_operands_to_prevent_overflow,def.gettypename);
  599. subn:
  600. cgmessage1(type_h_convert_sub_operands_to_prevent_overflow,def.gettypename);
  601. muln:
  602. cgmessage1(type_h_convert_mul_operands_to_prevent_overflow,def.gettypename);
  603. end;
  604. end;
  605. constructor ttypeconvnode.create_explicit(node : tnode;def:tdef);
  606. begin
  607. self.create(node,def);
  608. include(flags,nf_explicit);
  609. end;
  610. constructor ttypeconvnode.create_internal(node : tnode;def:tdef);
  611. begin
  612. self.create(node,def);
  613. { handle like explicit conversions }
  614. include(flags,nf_explicit);
  615. include(flags,nf_internal);
  616. end;
  617. constructor ttypeconvnode.create_proc_to_procvar(node : tnode);
  618. begin
  619. self.create(node,voidtype);
  620. convtype:=tc_proc_2_procvar;
  621. end;
  622. constructor ttypeconvnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  623. begin
  624. inherited ppuload(t,ppufile);
  625. ppufile.getderef(totypedefderef);
  626. convtype:=tconverttype(ppufile.getbyte);
  627. end;
  628. procedure ttypeconvnode.ppuwrite(ppufile:tcompilerppufile);
  629. begin
  630. inherited ppuwrite(ppufile);
  631. ppufile.putderef(totypedefderef);
  632. ppufile.putbyte(byte(convtype));
  633. end;
  634. procedure ttypeconvnode.buildderefimpl;
  635. begin
  636. inherited buildderefimpl;
  637. totypedefderef.build(totypedef);
  638. end;
  639. procedure ttypeconvnode.derefimpl;
  640. begin
  641. inherited derefimpl;
  642. totypedef:=tdef(totypedefderef.resolve);
  643. end;
  644. function ttypeconvnode.dogetcopy : tnode;
  645. var
  646. n : ttypeconvnode;
  647. begin
  648. n:=ttypeconvnode(inherited dogetcopy);
  649. n.convtype:=convtype;
  650. n.totypedef:=totypedef;
  651. dogetcopy:=n;
  652. end;
  653. procedure ttypeconvnode.printnodeinfo(var t : text);
  654. const
  655. convtyp2str : array[tconverttype] of pchar = (
  656. 'tc_none',
  657. 'tc_equal',
  658. 'tc_not_possible',
  659. 'tc_string_2_string',
  660. 'tc_char_2_string',
  661. 'tc_char_2_chararray',
  662. 'tc_pchar_2_string',
  663. 'tc_cchar_2_pchar',
  664. 'tc_cstring_2_pchar',
  665. 'tc_cstring_2_int',
  666. 'tc_ansistring_2_pchar',
  667. 'tc_string_2_chararray',
  668. 'tc_chararray_2_string',
  669. 'tc_array_2_pointer',
  670. 'tc_pointer_2_array',
  671. 'tc_int_2_int',
  672. 'tc_int_2_bool',
  673. 'tc_bool_2_bool',
  674. 'tc_bool_2_int',
  675. 'tc_real_2_real',
  676. 'tc_int_2_real',
  677. 'tc_real_2_currency',
  678. 'tc_proc_2_procvar',
  679. 'tc_nil_2_methodprocvar',
  680. 'tc_arrayconstructor_2_set',
  681. 'tc_set_2_set',
  682. 'tc_cord_2_pointer',
  683. 'tc_intf_2_string',
  684. 'tc_intf_2_guid',
  685. 'tc_class_2_intf',
  686. 'tc_char_2_char',
  687. 'tc_dynarray_2_openarray',
  688. 'tc_pwchar_2_string',
  689. 'tc_variant_2_dynarray',
  690. 'tc_dynarray_2_variant',
  691. 'tc_variant_2_enum',
  692. 'tc_enum_2_variant',
  693. 'tc_interface_2_variant',
  694. 'tc_variant_2_interface',
  695. 'tc_array_2_dynarray'
  696. );
  697. begin
  698. inherited printnodeinfo(t);
  699. write(t,', convtype = ',strpas(convtyp2str[convtype]));
  700. end;
  701. function ttypeconvnode.typecheck_cord_to_pointer : tnode;
  702. var
  703. t : tnode;
  704. begin
  705. result:=nil;
  706. if left.nodetype=ordconstn then
  707. begin
  708. { check if we have a valid pointer constant (JM) }
  709. {$if sizeof(pointer) > sizeof(TConstPtrUInt)}
  710. {$if sizeof(TConstPtrUInt) = 4}
  711. if (tordconstnode(left).value < int64(low(longint))) or
  712. (tordconstnode(left).value > int64(high(cardinal))) then
  713. CGMessage(parser_e_range_check_error);
  714. {$else} {$if sizeof(TConstPtrUInt) = 8}
  715. if (tordconstnode(left).value < int64(low(int64))) or
  716. (tordconstnode(left).value > int64(high(qword))) then
  717. CGMessage(parser_e_range_check_error);
  718. {$else}
  719. internalerror(2001020801);
  720. {$endif} {$endif}
  721. {$endif}
  722. t:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value.uvalue),resultdef);
  723. result:=t;
  724. end
  725. else
  726. internalerror(200104023);
  727. end;
  728. function ttypeconvnode.typecheck_chararray_to_string : tnode;
  729. var
  730. chartype : string[8];
  731. newblock : tblocknode;
  732. newstat : tstatementnode;
  733. restemp : ttempcreatenode;
  734. begin
  735. if is_widechar(tarraydef(left.resultdef).elementdef) then
  736. chartype:='widechar'
  737. else
  738. chartype:='char';
  739. if tstringdef(resultdef).stringtype=st_shortstring then
  740. begin
  741. newblock:=internalstatements(newstat);
  742. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  743. addstatement(newstat,restemp);
  744. addstatement(newstat,ccallnode.createintern('fpc_'+chartype+'array_to_shortstr',
  745. ccallparanode.create(cordconstnode.create(
  746. ord(tarraydef(left.resultdef).lowrange=0),booltype,false),
  747. ccallparanode.create(left,ccallparanode.create(
  748. ctemprefnode.create(restemp),nil)))));
  749. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  750. addstatement(newstat,ctemprefnode.create(restemp));
  751. result:=newblock;
  752. end
  753. else
  754. result:=ccallnode.createinternres(
  755. 'fpc_'+chartype+'array_to_'+tstringdef(resultdef).stringtypname,
  756. ccallparanode.create(cordconstnode.create(
  757. ord(tarraydef(left.resultdef).lowrange=0),booltype,false),
  758. ccallparanode.create(left,nil)),resultdef);
  759. left:=nil;
  760. end;
  761. function ttypeconvnode.typecheck_string_to_chararray : tnode;
  762. var
  763. newblock : tblocknode;
  764. newstat : tstatementnode;
  765. restemp : ttempcreatenode;
  766. pchtemp : pchar;
  767. arrsize : aint;
  768. chartype : string[8];
  769. begin
  770. result := nil;
  771. with tarraydef(resultdef) do
  772. begin
  773. if highrange<lowrange then
  774. internalerror(200501051);
  775. arrsize := highrange-lowrange+1;
  776. end;
  777. if (left.nodetype = stringconstn) and
  778. (tstringconstnode(left).cst_type=cst_conststring) then
  779. begin
  780. { if the array of char is large enough we can use the string
  781. constant directly. This is handled in ncgcnv }
  782. if (arrsize>=tstringconstnode(left).len) and
  783. is_char(tarraydef(resultdef).elementdef) then
  784. begin
  785. { pad the constant string with #0 to the array len }
  786. { (2.0.x compatible) }
  787. if (arrsize>tstringconstnode(left).len) then
  788. begin
  789. pchtemp:=concatansistrings(tstringconstnode(left).value_str,pchar(StringOfChar(#0,arrsize-tstringconstnode(left).len)),tstringconstnode(left).len,arrsize-tstringconstnode(left).len);
  790. left.free;
  791. left:=cstringconstnode.createpchar(pchtemp,arrsize);
  792. typecheckpass(left);
  793. end;
  794. exit;
  795. end;
  796. { Convert to wide/short/ansistring and call default helper }
  797. if is_widechar(tarraydef(resultdef).elementdef) then
  798. inserttypeconv(left,cwidestringtype)
  799. else
  800. begin
  801. if tstringconstnode(left).len>255 then
  802. inserttypeconv(left,cansistringtype)
  803. else
  804. inserttypeconv(left,cshortstringtype);
  805. end;
  806. end;
  807. if is_widechar(tarraydef(resultdef).elementdef) then
  808. chartype:='widechar'
  809. else
  810. chartype:='char';
  811. newblock:=internalstatements(newstat);
  812. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  813. addstatement(newstat,restemp);
  814. addstatement(newstat,ccallnode.createintern('fpc_'+tstringdef(left.resultdef).stringtypname+
  815. '_to_'+chartype+'array',ccallparanode.create(left,ccallparanode.create(
  816. ctemprefnode.create(restemp),nil))));
  817. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  818. addstatement(newstat,ctemprefnode.create(restemp));
  819. result:=newblock;
  820. left:=nil;
  821. end;
  822. function ttypeconvnode.typecheck_string_to_string : tnode;
  823. var
  824. procname: string[31];
  825. newblock : tblocknode;
  826. newstat : tstatementnode;
  827. restemp : ttempcreatenode;
  828. begin
  829. result:=nil;
  830. if (left.nodetype=stringconstn) and
  831. ((not is_widechararray(left.resultdef) and
  832. not is_widestring(left.resultdef)) or
  833. (tstringdef(resultdef).stringtype=st_widestring) or
  834. { non-ascii chars would be replaced with '?' -> loses info }
  835. not hasnonasciichars(pcompilerwidestring(tstringconstnode(left).value_str))) then
  836. begin
  837. tstringconstnode(left).changestringtype(resultdef);
  838. result:=left;
  839. left:=nil;
  840. end
  841. else
  842. begin
  843. { get the correct procedure name }
  844. procname := 'fpc_'+tstringdef(left.resultdef).stringtypname+
  845. '_to_'+tstringdef(resultdef).stringtypname;
  846. if tstringdef(resultdef).stringtype=st_shortstring then
  847. begin
  848. newblock:=internalstatements(newstat);
  849. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  850. addstatement(newstat,restemp);
  851. addstatement(newstat,ccallnode.createintern(procname,ccallparanode.create(left,ccallparanode.create(
  852. ctemprefnode.create(restemp),nil))));
  853. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  854. addstatement(newstat,ctemprefnode.create(restemp));
  855. result:=newblock;
  856. end
  857. else
  858. result := ccallnode.createinternres(procname,ccallparanode.create(left,nil),resultdef);
  859. left:=nil;
  860. end;
  861. end;
  862. function ttypeconvnode.typecheck_char_to_string : tnode;
  863. var
  864. procname: string[31];
  865. para : tcallparanode;
  866. hp : tstringconstnode;
  867. ws : pcompilerwidestring;
  868. newblock : tblocknode;
  869. newstat : tstatementnode;
  870. restemp : ttempcreatenode;
  871. begin
  872. result:=nil;
  873. { we can't do widechar to ansichar conversions at compile time, since }
  874. { this maps all non-ascii chars to '?' -> loses information }
  875. if (left.nodetype=ordconstn) and
  876. ((tstringdef(resultdef).stringtype=st_widestring) or
  877. (torddef(left.resultdef).ordtype=uchar) or
  878. { >=128 is destroyed }
  879. (tordconstnode(left).value.uvalue<128)) then
  880. begin
  881. if tstringdef(resultdef).stringtype=st_widestring then
  882. begin
  883. initwidestring(ws);
  884. if torddef(left.resultdef).ordtype=uwidechar then
  885. concatwidestringchar(ws,tcompilerwidechar(tordconstnode(left).value.uvalue))
  886. else
  887. concatwidestringchar(ws,tcompilerwidechar(chr(tordconstnode(left).value.uvalue)));
  888. hp:=cstringconstnode.createwstr(ws);
  889. donewidestring(ws);
  890. end
  891. else
  892. begin
  893. if torddef(left.resultdef).ordtype=uwidechar then
  894. hp:=cstringconstnode.createstr(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value.uvalue)))
  895. else
  896. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value.uvalue));
  897. tstringconstnode(hp).changestringtype(resultdef);
  898. end;
  899. result:=hp;
  900. end
  901. else
  902. { shortstrings are handled 'inline' (except for widechars) }
  903. if (tstringdef(resultdef).stringtype <> st_shortstring) or
  904. (torddef(left.resultdef).ordtype = uwidechar) then
  905. begin
  906. if (tstringdef(resultdef).stringtype <> st_shortstring) then
  907. begin
  908. { create the procname }
  909. if torddef(left.resultdef).ordtype<>uwidechar then
  910. procname := 'fpc_char_to_'
  911. else
  912. procname := 'fpc_wchar_to_';
  913. procname:=procname+tstringdef(resultdef).stringtypname;
  914. { and the parameter }
  915. para := ccallparanode.create(left,nil);
  916. { and finally the call }
  917. result := ccallnode.createinternres(procname,para,resultdef);
  918. end
  919. else
  920. begin
  921. newblock:=internalstatements(newstat);
  922. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  923. addstatement(newstat,restemp);
  924. addstatement(newstat,ccallnode.createintern('fpc_wchar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  925. ctemprefnode.create(restemp),nil))));
  926. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  927. addstatement(newstat,ctemprefnode.create(restemp));
  928. result:=newblock;
  929. end;
  930. left := nil;
  931. end
  932. else
  933. begin
  934. { create word(byte(char) shl 8 or 1) for litte endian machines }
  935. { and word(byte(char) or 256) for big endian machines }
  936. left := ctypeconvnode.create_internal(left,u8inttype);
  937. if (target_info.endian = endian_little) then
  938. left := caddnode.create(orn,
  939. cshlshrnode.create(shln,left,cordconstnode.create(8,s32inttype,false)),
  940. cordconstnode.create(1,s32inttype,false))
  941. else
  942. left := caddnode.create(orn,left,
  943. cordconstnode.create(1 shl 8,s32inttype,false));
  944. left := ctypeconvnode.create_internal(left,u16inttype);
  945. typecheckpass(left);
  946. end;
  947. end;
  948. function ttypeconvnode.typecheck_char_to_chararray : tnode;
  949. begin
  950. if resultdef.size <> 1 then
  951. begin
  952. { convert first to string, then to chararray }
  953. inserttypeconv(left,cshortstringtype);
  954. inserttypeconv(left,resultdef);
  955. result:=left;
  956. left := nil;
  957. exit;
  958. end;
  959. result := nil;
  960. end;
  961. function ttypeconvnode.typecheck_char_to_char : tnode;
  962. var
  963. hp : tordconstnode;
  964. begin
  965. result:=nil;
  966. if (left.nodetype=ordconstn) and
  967. ((torddef(resultdef).ordtype<>uchar) or
  968. (torddef(left.resultdef).ordtype<>uwidechar) or
  969. { >= 128 is replaced by '?' currently -> loses information }
  970. (tordconstnode(left).value.uvalue<128)) then
  971. begin
  972. if (torddef(resultdef).ordtype=uchar) and
  973. (torddef(left.resultdef).ordtype=uwidechar) then
  974. begin
  975. hp:=cordconstnode.create(
  976. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value.uvalue))),
  977. cchartype,true);
  978. result:=hp;
  979. end
  980. else if (torddef(resultdef).ordtype=uwidechar) and
  981. (torddef(left.resultdef).ordtype=uchar) then
  982. begin
  983. hp:=cordconstnode.create(
  984. asciichar2unicode(chr(tordconstnode(left).value.uvalue)),
  985. cwidechartype,true);
  986. result:=hp;
  987. end
  988. else
  989. internalerror(200105131);
  990. exit;
  991. end;
  992. end;
  993. function ttypeconvnode.typecheck_int_to_int : tnode;
  994. var
  995. v : TConstExprInt;
  996. begin
  997. result:=nil;
  998. if left.nodetype=ordconstn then
  999. begin
  1000. v:=tordconstnode(left).value;
  1001. if is_currency(resultdef) then
  1002. v:=v*10000;
  1003. if (resultdef.typ=pointerdef) then
  1004. result:=cpointerconstnode.create(TConstPtrUInt(v.uvalue),resultdef)
  1005. else
  1006. begin
  1007. if is_currency(left.resultdef) then
  1008. v:=v div 10000;
  1009. result:=cordconstnode.create(v,resultdef,false);
  1010. end;
  1011. end
  1012. else if left.nodetype=pointerconstn then
  1013. begin
  1014. v:=tpointerconstnode(left).value;
  1015. if (resultdef.typ=pointerdef) then
  1016. result:=cpointerconstnode.create(v.uvalue,resultdef)
  1017. else
  1018. begin
  1019. if is_currency(resultdef) then
  1020. v:=v*10000;
  1021. result:=cordconstnode.create(v,resultdef,false);
  1022. end;
  1023. end
  1024. else
  1025. begin
  1026. { multiply by 10000 for currency. We need to use getcopy to pass
  1027. the argument because the current node is always disposed. Only
  1028. inserting the multiply in the left node is not possible because
  1029. it'll get in an infinite loop to convert int->currency }
  1030. if is_currency(resultdef) then
  1031. begin
  1032. result:=caddnode.create(muln,getcopy,cordconstnode.create(10000,resultdef,false));
  1033. include(result.flags,nf_is_currency);
  1034. end
  1035. else if is_currency(left.resultdef) then
  1036. begin
  1037. result:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,resultdef,false));
  1038. include(result.flags,nf_is_currency);
  1039. end;
  1040. end;
  1041. end;
  1042. function ttypeconvnode.typecheck_int_to_real : tnode;
  1043. var
  1044. rv : bestreal;
  1045. begin
  1046. result:=nil;
  1047. if left.nodetype=ordconstn then
  1048. begin
  1049. rv:=tordconstnode(left).value;
  1050. if is_currency(resultdef) then
  1051. rv:=rv*10000.0
  1052. else if is_currency(left.resultdef) then
  1053. rv:=rv/10000.0;
  1054. result:=crealconstnode.create(rv,resultdef);
  1055. end
  1056. else
  1057. begin
  1058. { multiply by 10000 for currency. We need to use getcopy to pass
  1059. the argument because the current node is always disposed. Only
  1060. inserting the multiply in the left node is not possible because
  1061. it'll get in an infinite loop to convert int->currency }
  1062. if is_currency(resultdef) then
  1063. begin
  1064. result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resultdef));
  1065. include(result.flags,nf_is_currency);
  1066. end
  1067. else if is_currency(left.resultdef) then
  1068. begin
  1069. result:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,resultdef));
  1070. include(result.flags,nf_is_currency);
  1071. end;
  1072. end;
  1073. end;
  1074. function ttypeconvnode.typecheck_real_to_currency : tnode;
  1075. begin
  1076. if not is_currency(resultdef) then
  1077. internalerror(200304221);
  1078. result:=nil;
  1079. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
  1080. include(left.flags,nf_is_currency);
  1081. typecheckpass(left);
  1082. { Convert constants directly, else call Round() }
  1083. if left.nodetype=realconstn then
  1084. result:=cordconstnode.create(round(trealconstnode(left).value_real),resultdef,false)
  1085. else
  1086. begin
  1087. result:=ccallnode.createinternres('fpc_round_real',
  1088. ccallparanode.create(left,nil),resultdef);
  1089. left:=nil;
  1090. end;
  1091. end;
  1092. function ttypeconvnode.typecheck_real_to_real : tnode;
  1093. begin
  1094. result:=nil;
  1095. if is_currency(left.resultdef) and not(is_currency(resultdef)) then
  1096. begin
  1097. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resultdef));
  1098. include(left.flags,nf_is_currency);
  1099. typecheckpass(left);
  1100. end
  1101. else
  1102. if is_currency(resultdef) and not(is_currency(left.resultdef)) then
  1103. begin
  1104. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
  1105. include(left.flags,nf_is_currency);
  1106. typecheckpass(left);
  1107. end;
  1108. end;
  1109. function ttypeconvnode.typecheck_cchar_to_pchar : tnode;
  1110. begin
  1111. result:=nil;
  1112. if is_pwidechar(resultdef) then
  1113. inserttypeconv(left,cwidestringtype)
  1114. else
  1115. inserttypeconv(left,cshortstringtype);
  1116. { evaluate again, reset resultdef so the convert_typ
  1117. will be calculated again and cstring_to_pchar will
  1118. be used for futher conversion }
  1119. convtype:=tc_none;
  1120. result:=pass_typecheck;
  1121. end;
  1122. function ttypeconvnode.typecheck_cstring_to_pchar : tnode;
  1123. begin
  1124. result:=nil;
  1125. if is_pwidechar(resultdef) then
  1126. inserttypeconv(left,cwidestringtype)
  1127. else
  1128. if is_pchar(resultdef) and
  1129. is_widestring(left.resultdef) then
  1130. begin
  1131. inserttypeconv(left,cansistringtype);
  1132. { the second pass of second_cstring_to_pchar expects a }
  1133. { strinconstn, but this may become a call to the }
  1134. { widestring manager in case left contains "high ascii" }
  1135. if (left.nodetype<>stringconstn) then
  1136. begin
  1137. result:=left;
  1138. left:=nil;
  1139. end;
  1140. end;
  1141. end;
  1142. function ttypeconvnode.typecheck_cstring_to_int : tnode;
  1143. var
  1144. fcc : cardinal;
  1145. pb : pbyte;
  1146. begin
  1147. result:=nil;
  1148. if left.nodetype<>stringconstn then
  1149. internalerror(200510012);
  1150. if tstringconstnode(left).len=4 then
  1151. begin
  1152. pb:=pbyte(tstringconstnode(left).value_str);
  1153. fcc:=(pb[0] shl 24) or (pb[1] shl 16) or (pb[2] shl 8) or pb[3];
  1154. result:=cordconstnode.create(fcc,u32inttype,false);
  1155. end
  1156. else
  1157. CGMessage2(type_e_illegal_type_conversion,left.resultdef.GetTypeName,resultdef.GetTypeName);
  1158. end;
  1159. function ttypeconvnode.typecheck_arrayconstructor_to_set : tnode;
  1160. var
  1161. hp : tnode;
  1162. begin
  1163. result:=nil;
  1164. if left.nodetype<>arrayconstructorn then
  1165. internalerror(5546);
  1166. { remove typeconv node }
  1167. hp:=left;
  1168. left:=nil;
  1169. { create a set constructor tree }
  1170. arrayconstructor_to_set(hp);
  1171. result:=hp;
  1172. end;
  1173. function ttypeconvnode.typecheck_set_to_set : tnode;
  1174. begin
  1175. result:=nil;
  1176. { constant sets can be converted by changing the type only }
  1177. if (left.nodetype=setconstn) then
  1178. begin
  1179. left.resultdef:=resultdef;
  1180. result:=left;
  1181. left:=nil;
  1182. exit;
  1183. end;
  1184. end;
  1185. function ttypeconvnode.typecheck_pchar_to_string : tnode;
  1186. var
  1187. newblock : tblocknode;
  1188. newstat : tstatementnode;
  1189. restemp : ttempcreatenode;
  1190. begin
  1191. if tstringdef(resultdef).stringtype=st_shortstring then
  1192. begin
  1193. newblock:=internalstatements(newstat);
  1194. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1195. addstatement(newstat,restemp);
  1196. addstatement(newstat,ccallnode.createintern('fpc_pchar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1197. ctemprefnode.create(restemp),nil))));
  1198. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1199. addstatement(newstat,ctemprefnode.create(restemp));
  1200. result:=newblock;
  1201. end
  1202. else
  1203. result := ccallnode.createinternres(
  1204. 'fpc_pchar_to_'+tstringdef(resultdef).stringtypname,
  1205. ccallparanode.create(left,nil),resultdef);
  1206. left:=nil;
  1207. end;
  1208. function ttypeconvnode.typecheck_interface_to_string : tnode;
  1209. begin
  1210. if assigned(tobjectdef(left.resultdef).iidstr) then
  1211. begin
  1212. if not(oo_has_valid_guid in tobjectdef(left.resultdef).objectoptions) then
  1213. CGMessage1(type_interface_has_no_guid,tobjectdef(left.resultdef).typename);
  1214. result:=cstringconstnode.createstr(tobjectdef(left.resultdef).iidstr^);
  1215. tstringconstnode(result).changestringtype(cshortstringtype);
  1216. end;
  1217. end;
  1218. function ttypeconvnode.typecheck_interface_to_guid : tnode;
  1219. begin
  1220. if assigned(tobjectdef(left.resultdef).iidguid) then
  1221. begin
  1222. if not(oo_has_valid_guid in tobjectdef(left.resultdef).objectoptions) then
  1223. CGMessage1(type_interface_has_no_guid,tobjectdef(left.resultdef).typename);
  1224. result:=cguidconstnode.create(tobjectdef(left.resultdef).iidguid^);
  1225. end;
  1226. end;
  1227. function ttypeconvnode.typecheck_dynarray_to_openarray : tnode;
  1228. begin
  1229. { a dynamic array is a pointer to an array, so to convert it to }
  1230. { an open array, we have to dereference it (JM) }
  1231. result := ctypeconvnode.create_internal(left,voidpointertype);
  1232. typecheckpass(result);
  1233. { left is reused }
  1234. left := nil;
  1235. result := cderefnode.create(result);
  1236. include(result.flags,nf_no_checkpointer);
  1237. result.resultdef := resultdef;
  1238. end;
  1239. function ttypeconvnode.typecheck_pwchar_to_string : tnode;
  1240. var
  1241. newblock : tblocknode;
  1242. newstat : tstatementnode;
  1243. restemp : ttempcreatenode;
  1244. begin
  1245. if tstringdef(resultdef).stringtype=st_shortstring then
  1246. begin
  1247. newblock:=internalstatements(newstat);
  1248. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1249. addstatement(newstat,restemp);
  1250. addstatement(newstat,ccallnode.createintern('fpc_pwidechar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1251. ctemprefnode.create(restemp),nil))));
  1252. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1253. addstatement(newstat,ctemprefnode.create(restemp));
  1254. result:=newblock;
  1255. end
  1256. else
  1257. result := ccallnode.createinternres(
  1258. 'fpc_pwidechar_to_'+tstringdef(resultdef).stringtypname,
  1259. ccallparanode.create(left,nil),resultdef);
  1260. left:=nil;
  1261. end;
  1262. function ttypeconvnode.typecheck_variant_to_dynarray : tnode;
  1263. begin
  1264. result := ccallnode.createinternres(
  1265. 'fpc_variant_to_dynarray',
  1266. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  1267. ccallparanode.create(left,nil)
  1268. ),resultdef);
  1269. typecheckpass(result);
  1270. left:=nil;
  1271. end;
  1272. function ttypeconvnode.typecheck_dynarray_to_variant : tnode;
  1273. begin
  1274. result := ccallnode.createinternres(
  1275. 'fpc_dynarray_to_variant',
  1276. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  1277. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil)
  1278. ),resultdef);
  1279. typecheckpass(result);
  1280. left:=nil;
  1281. end;
  1282. function ttypeconvnode.typecheck_variant_to_interface : tnode;
  1283. begin
  1284. if tobjectdef(resultdef).is_related(tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1285. result := ccallnode.createinternres(
  1286. 'fpc_variant_to_idispatch',
  1287. ccallparanode.create(left,nil)
  1288. ,resultdef)
  1289. else
  1290. result := ccallnode.createinternres(
  1291. 'fpc_variant_to_interface',
  1292. ccallparanode.create(left,nil)
  1293. ,resultdef);
  1294. typecheckpass(result);
  1295. left:=nil;
  1296. end;
  1297. function ttypeconvnode.typecheck_interface_to_variant : tnode;
  1298. begin
  1299. if tobjectdef(left.resultdef).is_related(tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1300. result := ccallnode.createinternres(
  1301. 'fpc_idispatch_to_variant',
  1302. ccallparanode.create(left,nil)
  1303. ,resultdef)
  1304. else
  1305. result := ccallnode.createinternres(
  1306. 'fpc_interface_to_variant',
  1307. ccallparanode.create(left,nil)
  1308. ,resultdef);
  1309. typecheckpass(result);
  1310. left:=nil;
  1311. end;
  1312. function ttypeconvnode.typecheck_variant_to_enum : tnode;
  1313. begin
  1314. result := ctypeconvnode.create_internal(left,sinttype);
  1315. result := ctypeconvnode.create_internal(result,resultdef);
  1316. typecheckpass(result);
  1317. { left is reused }
  1318. left := nil;
  1319. end;
  1320. function ttypeconvnode.typecheck_enum_to_variant : tnode;
  1321. begin
  1322. result := ctypeconvnode.create_internal(left,sinttype);
  1323. result := ctypeconvnode.create_internal(result,cvarianttype);
  1324. typecheckpass(result);
  1325. { left is reused }
  1326. left := nil;
  1327. end;
  1328. function ttypeconvnode.typecheck_array_2_dynarray : tnode;
  1329. var
  1330. newstatement : tstatementnode;
  1331. temp : ttempcreatenode;
  1332. temp2 : ttempcreatenode;
  1333. begin
  1334. { create statements with call to getmem+initialize }
  1335. result:=internalstatements(newstatement);
  1336. { create temp for result }
  1337. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1338. addstatement(newstatement,temp);
  1339. { get temp for array of lengths }
  1340. temp2:=ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1341. addstatement(newstatement,temp2);
  1342. { one dimensional }
  1343. addstatement(newstatement,cassignmentnode.create(
  1344. ctemprefnode.create_offset(temp2,0),
  1345. cordconstnode.create
  1346. (tarraydef(left.resultdef).highrange+1,s32inttype,true)));
  1347. { create call to fpc_dynarr_setlength }
  1348. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1349. ccallparanode.create(caddrnode.create_internal
  1350. (ctemprefnode.create(temp2)),
  1351. ccallparanode.create(cordconstnode.create
  1352. (1,s32inttype,true),
  1353. ccallparanode.create(caddrnode.create_internal
  1354. (crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  1355. ccallparanode.create(
  1356. ctypeconvnode.create_internal(
  1357. ctemprefnode.create(temp),voidpointertype),
  1358. nil))))
  1359. ));
  1360. addstatement(newstatement,ctempdeletenode.create(temp2));
  1361. { copy ... }
  1362. addstatement(newstatement,cassignmentnode.create(
  1363. ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(ctemprefnode.create(temp),voidpointertype)),left.resultdef),
  1364. left
  1365. ));
  1366. { left is reused }
  1367. left:=nil;
  1368. { the last statement should return the value as
  1369. location and type, this is done be referencing the
  1370. temp and converting it first from a persistent temp to
  1371. normal temp }
  1372. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1373. addstatement(newstatement,ctemprefnode.create(temp));
  1374. end;
  1375. procedure copyparasym(p:TObject;arg:pointer);
  1376. var
  1377. newparast : TSymtable absolute arg;
  1378. vs : tparavarsym;
  1379. begin
  1380. if tsym(p).typ<>paravarsym then
  1381. exit;
  1382. with tparavarsym(p) do
  1383. begin
  1384. vs:=tparavarsym.create(realname,paranr,varspez,vardef,varoptions);
  1385. vs.defaultconstsym:=defaultconstsym;
  1386. newparast.insert(vs);
  1387. end;
  1388. end;
  1389. function ttypeconvnode.typecheck_proc_to_procvar : tnode;
  1390. var
  1391. pd : tabstractprocdef;
  1392. begin
  1393. result:=nil;
  1394. pd:=tabstractprocdef(left.resultdef);
  1395. { create procvardef }
  1396. resultdef:=tprocvardef.create(pd.parast.symtablelevel);
  1397. tprocvardef(resultdef).proctypeoption:=pd.proctypeoption;
  1398. tprocvardef(resultdef).proccalloption:=pd.proccalloption;
  1399. tprocvardef(resultdef).procoptions:=pd.procoptions;
  1400. tprocvardef(resultdef).returndef:=pd.returndef;
  1401. { method ? then set the methodpointer flag }
  1402. if (pd.owner.symtabletype=ObjectSymtable) then
  1403. include(tprocvardef(resultdef).procoptions,po_methodpointer);
  1404. { was it a local procedure? }
  1405. if (pd.owner.symtabletype=localsymtable) then
  1406. include(tprocvardef(resultdef).procoptions,po_local);
  1407. { only need the address of the method? this is needed
  1408. for @tobject.create. In this case there will be a loadn without
  1409. a methodpointer. }
  1410. if (left.nodetype=loadn) and
  1411. not assigned(tloadnode(left).left) then
  1412. include(tprocvardef(resultdef).procoptions,po_addressonly);
  1413. { Add parameters use only references, we don't need to keep the
  1414. parast. We use the parast from the original function to calculate
  1415. our parameter data and reset it afterwards }
  1416. pd.parast.SymList.ForEachCall(@copyparasym,tprocvardef(resultdef).parast);
  1417. tprocvardef(resultdef).calcparas;
  1418. end;
  1419. function ttypeconvnode.typecheck_call_helper(c : tconverttype) : tnode;
  1420. const
  1421. resultdefconvert : array[tconverttype] of pointer = (
  1422. {none} nil,
  1423. {equal} nil,
  1424. {not_possible} nil,
  1425. { string_2_string } @ttypeconvnode.typecheck_string_to_string,
  1426. { char_2_string } @ttypeconvnode.typecheck_char_to_string,
  1427. { char_2_chararray } @ttypeconvnode.typecheck_char_to_chararray,
  1428. { pchar_2_string } @ttypeconvnode.typecheck_pchar_to_string,
  1429. { cchar_2_pchar } @ttypeconvnode.typecheck_cchar_to_pchar,
  1430. { cstring_2_pchar } @ttypeconvnode.typecheck_cstring_to_pchar,
  1431. { cstring_2_int } @ttypeconvnode.typecheck_cstring_to_int,
  1432. { ansistring_2_pchar } nil,
  1433. { string_2_chararray } @ttypeconvnode.typecheck_string_to_chararray,
  1434. { chararray_2_string } @ttypeconvnode.typecheck_chararray_to_string,
  1435. { array_2_pointer } nil,
  1436. { pointer_2_array } nil,
  1437. { int_2_int } @ttypeconvnode.typecheck_int_to_int,
  1438. { int_2_bool } nil,
  1439. { bool_2_bool } nil,
  1440. { bool_2_int } nil,
  1441. { real_2_real } @ttypeconvnode.typecheck_real_to_real,
  1442. { int_2_real } @ttypeconvnode.typecheck_int_to_real,
  1443. { real_2_currency } @ttypeconvnode.typecheck_real_to_currency,
  1444. { proc_2_procvar } @ttypeconvnode.typecheck_proc_to_procvar,
  1445. { nil_2_methodprocvar } nil,
  1446. { arrayconstructor_2_set } @ttypeconvnode.typecheck_arrayconstructor_to_set,
  1447. { set_to_set } @ttypeconvnode.typecheck_set_to_set,
  1448. { cord_2_pointer } @ttypeconvnode.typecheck_cord_to_pointer,
  1449. { intf_2_string } @ttypeconvnode.typecheck_interface_to_string,
  1450. { intf_2_guid } @ttypeconvnode.typecheck_interface_to_guid,
  1451. { class_2_intf } nil,
  1452. { char_2_char } @ttypeconvnode.typecheck_char_to_char,
  1453. { dynarray_2_openarray} @ttypeconvnode.typecheck_dynarray_to_openarray,
  1454. { pwchar_2_string} @ttypeconvnode.typecheck_pwchar_to_string,
  1455. { variant_2_dynarray} @ttypeconvnode.typecheck_variant_to_dynarray,
  1456. { dynarray_2_variant} @ttypeconvnode.typecheck_dynarray_to_variant,
  1457. { variant_2_enum} @ttypeconvnode.typecheck_variant_to_enum,
  1458. { enum_2_variant} @ttypeconvnode.typecheck_enum_to_variant,
  1459. { variant_2_interface} @ttypeconvnode.typecheck_interface_to_variant,
  1460. { interface_2_variant} @ttypeconvnode.typecheck_variant_to_interface,
  1461. { array_2_dynarray} @ttypeconvnode.typecheck_array_2_dynarray
  1462. );
  1463. type
  1464. tprocedureofobject = function : tnode of object;
  1465. var
  1466. r : packed record
  1467. proc : pointer;
  1468. obj : pointer;
  1469. end;
  1470. begin
  1471. result:=nil;
  1472. { this is a little bit dirty but it works }
  1473. { and should be quite portable too }
  1474. r.proc:=resultdefconvert[c];
  1475. r.obj:=self;
  1476. if assigned(r.proc) then
  1477. result:=tprocedureofobject(r)();
  1478. end;
  1479. function ttypeconvnode.actualtargetnode: tnode;
  1480. begin
  1481. result:=self;
  1482. while (result.nodetype=typeconvn) and
  1483. (nf_absolute in result.flags) and
  1484. (resultdef.size=left.resultdef.size) do
  1485. result:=ttypeconvnode(result).left;
  1486. end;
  1487. function ttypeconvnode.pass_typecheck:tnode;
  1488. var
  1489. hdef : tdef;
  1490. hp : tnode;
  1491. currprocdef : tabstractprocdef;
  1492. aprocdef : tprocdef;
  1493. eq : tequaltype;
  1494. cdoptions : tcompare_defs_options;
  1495. newblock: tblocknode;
  1496. newstatement: tstatementnode;
  1497. tempnode: ttempcreatenode;
  1498. begin
  1499. result:=nil;
  1500. resultdef:=totypedef;
  1501. typecheckpass(left);
  1502. if codegenerror then
  1503. exit;
  1504. { When absolute force tc_equal }
  1505. if (nf_absolute in flags) then
  1506. begin
  1507. convtype:=tc_equal;
  1508. if not(tstoreddef(resultdef).is_intregable) and
  1509. not(tstoreddef(resultdef).is_fpuregable) then
  1510. make_not_regable(left,[ra_addr_regable]);
  1511. exit;
  1512. end;
  1513. { tp procvar support. Skip typecasts to procvar, record or set. Those
  1514. convert on the procvar value. This is used to access the
  1515. fields of a methodpointer }
  1516. if not(nf_load_procvar in flags) and
  1517. not(resultdef.typ in [procvardef,recorddef,setdef]) then
  1518. maybe_call_procvar(left,true);
  1519. { convert array constructors to sets, because there is no conversion
  1520. possible for array constructors }
  1521. if (resultdef.typ<>arraydef) and
  1522. is_array_constructor(left.resultdef) then
  1523. begin
  1524. arrayconstructor_to_set(left);
  1525. typecheckpass(left);
  1526. end;
  1527. if convtype=tc_none then
  1528. begin
  1529. cdoptions:=[cdo_check_operator,cdo_allow_variant];
  1530. if nf_explicit in flags then
  1531. include(cdoptions,cdo_explicit);
  1532. if nf_internal in flags then
  1533. include(cdoptions,cdo_internal);
  1534. eq:=compare_defs_ext(left.resultdef,resultdef,left.nodetype,convtype,aprocdef,cdoptions);
  1535. case eq of
  1536. te_exact,
  1537. te_equal :
  1538. begin
  1539. result := simplify;
  1540. if assigned(result) then
  1541. exit;
  1542. { Only leave when there is no conversion to do.
  1543. We can still need to call a conversion routine,
  1544. like the routine to convert a stringconstnode }
  1545. if convtype in [tc_equal,tc_not_possible] then
  1546. begin
  1547. left.resultdef:=resultdef;
  1548. if (nf_explicit in flags) and (left.nodetype = addrn) then
  1549. include(left.flags, nf_typedaddr);
  1550. result:=left;
  1551. left:=nil;
  1552. exit;
  1553. end;
  1554. end;
  1555. te_convert_l1,
  1556. te_convert_l2,
  1557. te_convert_l3,
  1558. te_convert_l4,
  1559. te_convert_l5:
  1560. begin
  1561. result := simplify;
  1562. if assigned(result) then
  1563. exit;
  1564. { nothing to do }
  1565. end;
  1566. te_convert_operator :
  1567. begin
  1568. include(current_procinfo.flags,pi_do_call);
  1569. aprocdef.procsym.IncRefCountBy(1);
  1570. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[]);
  1571. { tell explicitly which def we must use !! (PM) }
  1572. tcallnode(hp).procdefinition:=aprocdef;
  1573. left:=nil;
  1574. result:=hp;
  1575. exit;
  1576. end;
  1577. te_incompatible :
  1578. begin
  1579. { Procedures have a resultdef of voiddef and functions of their
  1580. own resultdef. They will therefore always be incompatible with
  1581. a procvar. Because isconvertable cannot check for procedures we
  1582. use an extra check for them.}
  1583. if (left.nodetype=calln) and
  1584. (tcallnode(left).para_count=0) and
  1585. (resultdef.typ=procvardef) and
  1586. (
  1587. (m_tp_procvar in current_settings.modeswitches) or
  1588. (m_mac_procvar in current_settings.modeswitches)
  1589. ) then
  1590. begin
  1591. if assigned(tcallnode(left).right) then
  1592. begin
  1593. { this is already a procvar, if it is really equal
  1594. is checked below }
  1595. convtype:=tc_equal;
  1596. hp:=tcallnode(left).right.getcopy;
  1597. currprocdef:=tabstractprocdef(hp.resultdef);
  1598. end
  1599. else
  1600. begin
  1601. convtype:=tc_proc_2_procvar;
  1602. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).Find_procdef_byprocvardef(Tprocvardef(resultdef));
  1603. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  1604. tprocdef(currprocdef),tcallnode(left).symtableproc);
  1605. if (tcallnode(left).symtableprocentry.owner.symtabletype=ObjectSymtable) then
  1606. begin
  1607. if assigned(tcallnode(left).methodpointer) then
  1608. tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy)
  1609. else
  1610. tloadnode(hp).set_mp(load_self_node);
  1611. end;
  1612. typecheckpass(hp);
  1613. end;
  1614. left.free;
  1615. left:=hp;
  1616. { Now check if the procedure we are going to assign to
  1617. the procvar, is compatible with the procvar's type }
  1618. if not(nf_explicit in flags) and
  1619. (proc_to_procvar_equal(currprocdef,tprocvardef(resultdef))=te_incompatible) then
  1620. IncompatibleTypes(left.resultdef,resultdef);
  1621. exit;
  1622. end;
  1623. { Handle explicit type conversions }
  1624. if nf_explicit in flags then
  1625. begin
  1626. { do common tc_equal cast }
  1627. convtype:=tc_equal;
  1628. { ordinal constants can be resized to 1,2,4,8 bytes }
  1629. if (left.nodetype=ordconstn) then
  1630. begin
  1631. { Insert typeconv for ordinal to the correct size first on left, after
  1632. that the other conversion can be done }
  1633. hdef:=nil;
  1634. case longint(resultdef.size) of
  1635. 1 :
  1636. hdef:=s8inttype;
  1637. 2 :
  1638. hdef:=s16inttype;
  1639. 4 :
  1640. hdef:=s32inttype;
  1641. 8 :
  1642. hdef:=s64inttype;
  1643. end;
  1644. { we need explicit, because it can also be an enum }
  1645. if assigned(hdef) then
  1646. inserttypeconv_internal(left,hdef)
  1647. else
  1648. CGMessage2(type_e_illegal_type_conversion,left.resultdef.GetTypeName,resultdef.GetTypeName);
  1649. end;
  1650. { check if the result could be in a register }
  1651. if (not(tstoreddef(resultdef).is_intregable) and
  1652. not(tstoreddef(resultdef).is_fpuregable)) or
  1653. ((left.resultdef.typ = floatdef) and
  1654. (resultdef.typ <> floatdef)) then
  1655. make_not_regable(left,[ra_addr_regable]);
  1656. { class/interface to class/interface, with checkobject support }
  1657. if is_class_or_interface(resultdef) and
  1658. is_class_or_interface(left.resultdef) then
  1659. begin
  1660. { check if the types are related }
  1661. if not(nf_internal in flags) and
  1662. (not(tobjectdef(left.resultdef).is_related(tobjectdef(resultdef)))) and
  1663. (not(tobjectdef(resultdef).is_related(tobjectdef(left.resultdef)))) then
  1664. begin
  1665. { Give an error when typecasting class to interface, this is compatible
  1666. with delphi }
  1667. if is_interface(resultdef) and
  1668. not is_interface(left.resultdef) then
  1669. CGMessage2(type_e_classes_not_related,
  1670. FullTypeName(left.resultdef,resultdef),
  1671. FullTypeName(resultdef,left.resultdef))
  1672. else
  1673. CGMessage2(type_w_classes_not_related,
  1674. FullTypeName(left.resultdef,resultdef),
  1675. FullTypeName(resultdef,left.resultdef))
  1676. end;
  1677. { Add runtime check? }
  1678. if (cs_check_object in current_settings.localswitches) and
  1679. not(nf_internal in flags) then
  1680. begin
  1681. { we can translate the typeconvnode to 'as' when
  1682. typecasting to a class or interface }
  1683. { we need to make sure the result can still be
  1684. passed as a var parameter }
  1685. newblock:=internalstatements(newstatement);
  1686. if (valid_for_var(left,false)) then
  1687. begin
  1688. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1689. addstatement(newstatement,tempnode);
  1690. addstatement(newstatement,cassignmentnode.create(
  1691. ctemprefnode.create(tempnode),
  1692. caddrnode.create_internal(left)));
  1693. left:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),left.resultdef);
  1694. end
  1695. else
  1696. begin
  1697. tempnode:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  1698. addstatement(newstatement,tempnode);
  1699. addstatement(newstatement,cassignmentnode.create(
  1700. ctemprefnode.create(tempnode),
  1701. left));
  1702. left:=ctemprefnode.create(tempnode);
  1703. end;
  1704. addstatement(newstatement,casnode.create(left.getcopy,cloadvmtaddrnode.create(ctypenode.create(resultdef))));
  1705. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  1706. addstatement(newstatement,ctypeconvnode.create_internal(left,resultdef));
  1707. left:=nil;
  1708. result:=newblock;
  1709. exit;
  1710. end;
  1711. end
  1712. else
  1713. begin
  1714. { only if the same size or formal def, and }
  1715. { don't allow type casting of constants to }
  1716. { structured types }
  1717. if not(
  1718. (left.resultdef.typ=formaldef) or
  1719. (
  1720. not(is_open_array(left.resultdef)) and
  1721. not(is_array_constructor(left.resultdef)) and
  1722. (left.resultdef.size=resultdef.size) and
  1723. { disallow casts of const nodes }
  1724. (not is_constnode(left) or
  1725. { however, there are some exceptions }
  1726. (not(resultdef.typ in [arraydef,recorddef,setdef,stringdef,
  1727. filedef,variantdef,objectdef]) or
  1728. is_class_or_interface(resultdef) or
  1729. { the softfloat code generates casts <const. float> to record }
  1730. (nf_internal in flags)
  1731. ))
  1732. ) or
  1733. (
  1734. is_void(left.resultdef) and
  1735. (left.nodetype=derefn)
  1736. )
  1737. ) then
  1738. CGMessage2(type_e_illegal_type_conversion,left.resultdef.GetTypeName,resultdef.GetTypeName);
  1739. end;
  1740. end
  1741. else
  1742. IncompatibleTypes(left.resultdef,resultdef);
  1743. end;
  1744. else
  1745. internalerror(200211231);
  1746. end;
  1747. end;
  1748. { Give hint or warning for unportable code, exceptions are
  1749. - typecasts from constants
  1750. - void }
  1751. if not(nf_internal in flags) and
  1752. (left.nodetype<>ordconstn) and
  1753. not(is_void(left.resultdef)) and
  1754. (((left.resultdef.typ=orddef) and
  1755. (resultdef.typ in [pointerdef,procvardef,classrefdef])) or
  1756. ((resultdef.typ=orddef) and
  1757. (left.resultdef.typ in [pointerdef,procvardef,classrefdef]))) then
  1758. begin
  1759. {Converting pointers to signed integers is a bad idea. Warn.}
  1760. warn_pointer_to_signed:=(resultdef.typ=orddef) and (Torddef(resultdef).ordtype in [s8bit,s16bit,s32bit,s64bit]);
  1761. { Give a warning when sizes don't match, because then info will be lost }
  1762. if left.resultdef.size=resultdef.size then
  1763. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  1764. else
  1765. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  1766. end;
  1767. result := simplify;
  1768. if assigned(result) then
  1769. exit;
  1770. { now call the resultdef helper to do constant folding }
  1771. result:=typecheck_call_helper(convtype);
  1772. end;
  1773. {$ifndef cpu64bitaddr}
  1774. { checks whether we can safely remove 64 bit typeconversions }
  1775. { in case range and overflow checking are off, and in case }
  1776. { the result of this node tree is downcasted again to a }
  1777. { 8/16/32 bit value afterwards }
  1778. function checkremove64bittypeconvs(n: tnode): boolean;
  1779. var
  1780. gotmuldivmod, gotsint: boolean;
  1781. { checks whether a node is either an u32bit, or originally }
  1782. { was one but was implicitly converted to s64bit }
  1783. function wasoriginallyint32(n: tnode): boolean;
  1784. begin
  1785. if (n.resultdef.typ<>orddef) then
  1786. exit(false);
  1787. if (torddef(n.resultdef).ordtype in [s32bit,u32bit]) then
  1788. begin
  1789. if (torddef(n.resultdef).ordtype=s32bit) then
  1790. gotsint:=true;
  1791. exit(true);
  1792. end;
  1793. if (torddef(n.resultdef).ordtype=s64bit) and
  1794. { nf_explicit is also set for explicitly typecasted }
  1795. { ordconstn's }
  1796. ([nf_internal,nf_explicit]*n.flags=[]) and
  1797. { either a typeconversion node coming from u32bit }
  1798. (((n.nodetype=typeconvn) and
  1799. (ttypeconvnode(n).left.resultdef.typ=orddef) and
  1800. (torddef(ttypeconvnode(n).left.resultdef).ordtype in [s32bit,u32bit])) or
  1801. { or an ordconstnode which was/is a valid cardinal }
  1802. ((n.nodetype=ordconstn) and
  1803. (tordconstnode(n).value>=int64(low(longint))) and
  1804. (tordconstnode(n).value<=high(cardinal)))) then
  1805. begin
  1806. if ((n.nodetype=typeconvn) and
  1807. (torddef(ttypeconvnode(n).left.resultdef).ordtype=s32bit)) or
  1808. ((n.nodetype=ordconstn) and
  1809. (tordconstnode(n).value<0)) then
  1810. gotsint:=true;
  1811. exit(true);
  1812. end;
  1813. result:=false;
  1814. end;
  1815. function docheckremove64bittypeconvs(n: tnode): boolean;
  1816. begin
  1817. result:=false;
  1818. if wasoriginallyint32(n) then
  1819. exit(true);
  1820. case n.nodetype of
  1821. subn,orn,xorn:
  1822. begin
  1823. { nf_internal is set by taddnode.typecheckpass in }
  1824. { case the arguments of this subn were u32bit, but }
  1825. { upcasted to s64bit for calculation correctness }
  1826. { (normally only needed when range checking, but }
  1827. { also done otherwise so there is no difference }
  1828. { in overload choosing etc between $r+ and $r-) }
  1829. if (nf_internal in n.flags) then
  1830. result:=true
  1831. else
  1832. result:=
  1833. docheckremove64bittypeconvs(tbinarynode(n).left) and
  1834. docheckremove64bittypeconvs(tbinarynode(n).right);
  1835. end;
  1836. addn,muln,divn,modn,andn:
  1837. begin
  1838. if n.nodetype in [muln,divn,modn] then
  1839. gotmuldivmod:=true;
  1840. result:=
  1841. docheckremove64bittypeconvs(tbinarynode(n).left) and
  1842. docheckremove64bittypeconvs(tbinarynode(n).right);
  1843. end;
  1844. end;
  1845. end;
  1846. begin { checkremove64bittypeconvs }
  1847. gotmuldivmod:=false;
  1848. gotsint:=false;
  1849. result:=
  1850. docheckremove64bittypeconvs(n) and
  1851. not(gotmuldivmod and gotsint);
  1852. end;
  1853. procedure doremove64bittypeconvs(var n: tnode; todef: tdef);
  1854. begin
  1855. case n.nodetype of
  1856. subn,addn,muln,divn,modn,xorn,andn,orn:
  1857. begin
  1858. exclude(n.flags,nf_internal);
  1859. if is_signed(n.resultdef) then
  1860. begin
  1861. doremove64bittypeconvs(tbinarynode(n).left,s32inttype);
  1862. doremove64bittypeconvs(tbinarynode(n).right,s32inttype);
  1863. n.resultdef:=s32inttype
  1864. end
  1865. else
  1866. begin
  1867. doremove64bittypeconvs(tbinarynode(n).left,u32inttype);
  1868. doremove64bittypeconvs(tbinarynode(n).right,u32inttype);
  1869. n.resultdef:=u32inttype
  1870. end;
  1871. end;
  1872. ordconstn:
  1873. inserttypeconv_internal(n,todef);
  1874. typeconvn:
  1875. n.resultdef:=todef;
  1876. end;
  1877. end;
  1878. {$endif not cpu64bitaddr}
  1879. function ttypeconvnode.simplify: tnode;
  1880. var
  1881. hp: tnode;
  1882. begin
  1883. result := nil;
  1884. { Constant folding and other node transitions to
  1885. remove the typeconv node }
  1886. case left.nodetype of
  1887. realconstn :
  1888. begin
  1889. if (convtype = tc_real_2_currency) then
  1890. result := typecheck_real_to_currency
  1891. else if (convtype = tc_real_2_real) then
  1892. result := typecheck_real_to_real
  1893. else
  1894. exit;
  1895. if not(assigned(result)) then
  1896. begin
  1897. result := left;
  1898. left := nil;
  1899. end;
  1900. if (result.nodetype = realconstn) then
  1901. begin
  1902. hp:=result;
  1903. result:=crealconstnode.create(trealconstnode(hp).value_real,resultdef);
  1904. if ([nf_explicit,nf_internal] * flags <> []) then
  1905. include(result.flags, nf_explicit);
  1906. hp.free;
  1907. end;
  1908. end;
  1909. niln :
  1910. begin
  1911. { nil to ordinal node }
  1912. if (resultdef.typ=orddef) then
  1913. begin
  1914. hp:=cordconstnode.create(0,resultdef,true);
  1915. if ([nf_explicit,nf_internal] * flags <> []) then
  1916. include(hp.flags, nf_explicit);
  1917. result:=hp;
  1918. exit;
  1919. end
  1920. else
  1921. { fold nil to any pointer type }
  1922. if (resultdef.typ=pointerdef) then
  1923. begin
  1924. hp:=cnilnode.create;
  1925. hp.resultdef:=resultdef;
  1926. if ([nf_explicit,nf_internal] * flags <> []) then
  1927. include(hp.flags, nf_explicit);
  1928. result:=hp;
  1929. exit;
  1930. end
  1931. else
  1932. { remove typeconv after niln, but not when the result is a
  1933. methodpointer. The typeconv of the methodpointer will then
  1934. take care of updateing size of niln to OS_64 }
  1935. if not((resultdef.typ=procvardef) and
  1936. (po_methodpointer in tprocvardef(resultdef).procoptions)) then
  1937. begin
  1938. left.resultdef:=resultdef;
  1939. if ([nf_explicit,nf_internal] * flags <> []) then
  1940. include(left.flags, nf_explicit);
  1941. result:=left;
  1942. left:=nil;
  1943. exit;
  1944. end;
  1945. end;
  1946. ordconstn :
  1947. begin
  1948. { ordinal contants can be directly converted }
  1949. { but not char to char because it is a widechar to char or via versa }
  1950. { which needs extra code to do the code page transistion }
  1951. { constant ordinal to pointer }
  1952. if (resultdef.typ=pointerdef) and
  1953. (convtype<>tc_cchar_2_pchar) then
  1954. begin
  1955. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value.uvalue),resultdef);
  1956. if ([nf_explicit,nf_internal] * flags <> []) then
  1957. include(hp.flags, nf_explicit);
  1958. result:=hp;
  1959. exit;
  1960. end
  1961. else if is_ordinal(resultdef) and
  1962. not(convtype=tc_char_2_char) then
  1963. begin
  1964. { replace the resultdef and recheck the range }
  1965. if ([nf_explicit,nf_internal] * flags <> []) then
  1966. include(left.flags, nf_explicit)
  1967. else
  1968. { no longer an ordconst with an explicit typecast }
  1969. exclude(left.flags, nf_explicit);
  1970. { when converting from one boolean type to another, force }
  1971. { booleans to 0/1, and byte/word/long/qwordbool to 0/-1 }
  1972. { (Delphi-compatibile) }
  1973. if is_boolean(left.resultdef) and
  1974. is_boolean(resultdef) and
  1975. (is_cbool(left.resultdef) or
  1976. is_cbool(resultdef)) then
  1977. begin
  1978. if is_pasbool(resultdef) then
  1979. tordconstnode(left).value:=ord(tordconstnode(left).value<>0)
  1980. else
  1981. {$ifdef VER2_2}
  1982. tordconstnode(left).value:=ord(tordconstnode(left).value<>0);
  1983. tordconstnode(left).value:=-tordconstnode(left).value;
  1984. {$else}
  1985. tordconstnode(left).value:=-ord(tordconstnode(left).value<>0);
  1986. {$endif VER2_2}
  1987. end
  1988. else
  1989. testrange(resultdef,tordconstnode(left).value,(nf_explicit in flags));
  1990. left.resultdef:=resultdef;
  1991. result:=left;
  1992. left:=nil;
  1993. exit;
  1994. end;
  1995. end;
  1996. pointerconstn :
  1997. begin
  1998. { pointerconstn to any pointer is folded too }
  1999. if (resultdef.typ=pointerdef) then
  2000. begin
  2001. left.resultdef:=resultdef;
  2002. if ([nf_explicit,nf_internal] * flags <> []) then
  2003. include(left.flags, nf_explicit)
  2004. else
  2005. { no longer an ordconst with an explicit typecast }
  2006. exclude(left.flags, nf_explicit);
  2007. result:=left;
  2008. left:=nil;
  2009. exit;
  2010. end
  2011. { constant pointer to ordinal }
  2012. else if is_ordinal(resultdef) then
  2013. begin
  2014. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  2015. resultdef,not(nf_explicit in flags));
  2016. if ([nf_explicit,nf_internal] * flags <> []) then
  2017. include(hp.flags, nf_explicit);
  2018. result:=hp;
  2019. exit;
  2020. end;
  2021. end;
  2022. end;
  2023. {$ifndef cpu64bitaddr}
  2024. { must be done before code below, because we need the
  2025. typeconversions for ordconstn's as well }
  2026. case convtype of
  2027. tc_int_2_int:
  2028. begin
  2029. if (localswitches * [cs_check_range,cs_check_overflow] = []) and
  2030. (resultdef.typ in [pointerdef,orddef,enumdef]) and
  2031. (resultdef.size <= 4) and
  2032. is_64bitint(left.resultdef) and
  2033. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn]) and
  2034. checkremove64bittypeconvs(left) then
  2035. begin
  2036. { avoid unnecessary widening of intermediary calculations }
  2037. { to 64 bit }
  2038. doremove64bittypeconvs(left,generrordef);
  2039. end;
  2040. end;
  2041. end;
  2042. {$endif not cpu64bitaddr}
  2043. end;
  2044. procedure Ttypeconvnode.mark_write;
  2045. begin
  2046. left.mark_write;
  2047. end;
  2048. function ttypeconvnode.first_cord_to_pointer : tnode;
  2049. begin
  2050. result:=nil;
  2051. internalerror(200104043);
  2052. end;
  2053. function ttypeconvnode.first_int_to_int : tnode;
  2054. begin
  2055. first_int_to_int:=nil;
  2056. expectloc:=left.expectloc;
  2057. if not is_void(left.resultdef) then
  2058. begin
  2059. if (left.expectloc<>LOC_REGISTER) and
  2060. (resultdef.size>left.resultdef.size) then
  2061. expectloc:=LOC_REGISTER
  2062. else
  2063. if (left.expectloc=LOC_CREGISTER) and
  2064. (resultdef.size<left.resultdef.size) then
  2065. expectloc:=LOC_REGISTER;
  2066. end;
  2067. end;
  2068. function ttypeconvnode.first_cstring_to_pchar : tnode;
  2069. begin
  2070. result:=nil;
  2071. expectloc:=LOC_REGISTER;
  2072. end;
  2073. function ttypeconvnode.first_cstring_to_int : tnode;
  2074. begin
  2075. result:=nil;
  2076. internalerror(200510014);
  2077. end;
  2078. function ttypeconvnode.first_string_to_chararray : tnode;
  2079. begin
  2080. first_string_to_chararray:=nil;
  2081. expectloc:=left.expectloc;
  2082. end;
  2083. function ttypeconvnode.first_char_to_string : tnode;
  2084. begin
  2085. first_char_to_string:=nil;
  2086. expectloc:=LOC_REFERENCE;
  2087. end;
  2088. function ttypeconvnode.first_nothing : tnode;
  2089. begin
  2090. first_nothing:=nil;
  2091. end;
  2092. function ttypeconvnode.first_array_to_pointer : tnode;
  2093. begin
  2094. first_array_to_pointer:=nil;
  2095. expectloc:=LOC_REGISTER;
  2096. end;
  2097. function ttypeconvnode.first_int_to_real: tnode;
  2098. var
  2099. fname: string[32];
  2100. begin
  2101. if target_info.system in system_wince then
  2102. begin
  2103. { converting a 64bit integer to a float requires a helper }
  2104. if is_64bitint(left.resultdef) or
  2105. is_currency(left.resultdef) then
  2106. begin
  2107. { hack to avoid double division by 10000, as it's
  2108. already done by typecheckpass.resultdef_int_to_real }
  2109. if is_currency(left.resultdef) then
  2110. left.resultdef := s64inttype;
  2111. if is_signed(left.resultdef) then
  2112. fname:='I64TO'
  2113. else
  2114. fname:='UI64TO';
  2115. end
  2116. else
  2117. { other integers are supposed to be 32 bit }
  2118. begin
  2119. if is_signed(left.resultdef) then
  2120. fname:='ITO'
  2121. else
  2122. fname:='UTO';
  2123. firstpass(left);
  2124. end;
  2125. if tfloatdef(resultdef).floattype=s64real then
  2126. fname:=fname+'D'
  2127. else
  2128. fname:=fname+'S';
  2129. result:=ccallnode.createintern(fname,ccallparanode.create(
  2130. left,nil));
  2131. left:=nil;
  2132. firstpass(result);
  2133. exit;
  2134. end
  2135. else
  2136. begin
  2137. { converting a 64bit integer to a float requires a helper }
  2138. if is_64bitint(left.resultdef) or
  2139. is_currency(left.resultdef) then
  2140. begin
  2141. { hack to avoid double division by 10000, as it's
  2142. already done by typecheckpass.resultdef_int_to_real }
  2143. if is_currency(left.resultdef) then
  2144. left.resultdef := s64inttype;
  2145. if is_signed(left.resultdef) then
  2146. fname:='int64_to_'
  2147. else
  2148. { we can't do better currently }
  2149. fname:='int64_to_';
  2150. end
  2151. else
  2152. { other integers are supposed to be 32 bit }
  2153. begin
  2154. if is_signed(left.resultdef) then
  2155. fname:='int32_to_'
  2156. else
  2157. { we can't do better currently }
  2158. fname:='int32_to_';
  2159. firstpass(left);
  2160. end;
  2161. if tfloatdef(resultdef).floattype=s64real then
  2162. fname:=fname+'float64'
  2163. else
  2164. fname:=fname+'float32';
  2165. result:=ctypeconvnode.create_internal(ccallnode.createintern(fname,ccallparanode.create(
  2166. left,nil)),resultdef);
  2167. left:=nil;
  2168. firstpass(result);
  2169. exit;
  2170. end;
  2171. end;
  2172. function ttypeconvnode.first_real_to_real : tnode;
  2173. begin
  2174. {$ifdef cpufpemu}
  2175. if cs_fp_emulation in current_settings.moduleswitches then
  2176. begin
  2177. if target_info.system in system_wince then
  2178. begin
  2179. case tfloatdef(left.resultdef).floattype of
  2180. s32real:
  2181. case tfloatdef(resultdef).floattype of
  2182. s64real:
  2183. result:=ccallnode.createintern('STOD',ccallparanode.create(left,nil));
  2184. s32real:
  2185. begin
  2186. result:=left;
  2187. left:=nil;
  2188. end;
  2189. else
  2190. internalerror(2005082704);
  2191. end;
  2192. s64real:
  2193. case tfloatdef(resultdef).floattype of
  2194. s32real:
  2195. result:=ccallnode.createintern('DTOS',ccallparanode.create(left,nil));
  2196. s64real:
  2197. begin
  2198. result:=left;
  2199. left:=nil;
  2200. end;
  2201. else
  2202. internalerror(2005082703);
  2203. end;
  2204. else
  2205. internalerror(2005082702);
  2206. end;
  2207. left:=nil;
  2208. firstpass(result);
  2209. exit;
  2210. end
  2211. else
  2212. begin
  2213. case tfloatdef(left.resultdef).floattype of
  2214. s32real:
  2215. case tfloatdef(resultdef).floattype of
  2216. s64real:
  2217. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
  2218. ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').typedef),nil)),resultdef);
  2219. s32real:
  2220. begin
  2221. result:=left;
  2222. left:=nil;
  2223. end;
  2224. else
  2225. internalerror(200610151);
  2226. end;
  2227. s64real:
  2228. case tfloatdef(resultdef).floattype of
  2229. s32real:
  2230. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
  2231. ctypeconvnode.create_internal(left,search_system_type('FLOAT64').typedef),nil)),resultdef);
  2232. s64real:
  2233. begin
  2234. result:=left;
  2235. left:=nil;
  2236. end;
  2237. else
  2238. internalerror(200610152);
  2239. end;
  2240. else
  2241. internalerror(200610153);
  2242. end;
  2243. left:=nil;
  2244. firstpass(result);
  2245. exit;
  2246. end;
  2247. end
  2248. else
  2249. {$endif cpufpemu}
  2250. begin
  2251. first_real_to_real:=nil;
  2252. expectloc:=LOC_FPUREGISTER;
  2253. end;
  2254. end;
  2255. function ttypeconvnode.first_pointer_to_array : tnode;
  2256. begin
  2257. first_pointer_to_array:=nil;
  2258. expectloc:=LOC_REFERENCE;
  2259. end;
  2260. function ttypeconvnode.first_cchar_to_pchar : tnode;
  2261. begin
  2262. first_cchar_to_pchar:=nil;
  2263. internalerror(200104021);
  2264. end;
  2265. function ttypeconvnode.first_bool_to_int : tnode;
  2266. begin
  2267. first_bool_to_int:=nil;
  2268. { byte(boolean) or word(wordbool) or longint(longbool) must
  2269. be accepted for var parameters }
  2270. if (nf_explicit in flags) and
  2271. (left.resultdef.size=resultdef.size) and
  2272. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2273. exit;
  2274. { when converting to 64bit, first convert to a 32bit int and then }
  2275. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  2276. if resultdef.size > sizeof(aint) then
  2277. begin
  2278. result := ctypeconvnode.create_internal(left,s32inttype);
  2279. result := ctypeconvnode.create(result,resultdef);
  2280. left := nil;
  2281. firstpass(result);
  2282. exit;
  2283. end;
  2284. expectloc:=LOC_REGISTER;
  2285. end;
  2286. function ttypeconvnode.first_int_to_bool : tnode;
  2287. begin
  2288. first_int_to_bool:=nil;
  2289. { byte(boolean) or word(wordbool) or longint(longbool) must
  2290. be accepted for var parameters }
  2291. if (nf_explicit in flags) and
  2292. (left.resultdef.size=resultdef.size) and
  2293. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2294. exit;
  2295. { when converting 64bit int to C-ctyle boolean, first convert to a 32bit int and then }
  2296. { convert to a boolean (only necessary for 32bit processors) }
  2297. if (left.resultdef.size > sizeof(aint)) and (left.resultdef.size<>resultdef.size)
  2298. and is_cbool(resultdef) then
  2299. begin
  2300. result := ctypeconvnode.create_internal(left,s32inttype);
  2301. left := nil;
  2302. firstpass(result);
  2303. exit;
  2304. end;
  2305. expectloc:=LOC_REGISTER;
  2306. end;
  2307. function ttypeconvnode.first_bool_to_bool : tnode;
  2308. begin
  2309. first_bool_to_bool:=nil;
  2310. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) then
  2311. expectloc := left.expectloc
  2312. else
  2313. expectloc:=LOC_REGISTER;
  2314. end;
  2315. function ttypeconvnode.first_char_to_char : tnode;
  2316. var
  2317. fname: string[18];
  2318. begin
  2319. if (torddef(resultdef).ordtype=uchar) and
  2320. (torddef(left.resultdef).ordtype=uwidechar) then
  2321. fname := 'fpc_wchar_to_char'
  2322. else if (torddef(resultdef).ordtype=uwidechar) and
  2323. (torddef(left.resultdef).ordtype=uchar) then
  2324. fname := 'fpc_char_to_wchar'
  2325. else
  2326. internalerror(2007081201);
  2327. result := ccallnode.createintern(fname,ccallparanode.create(left,nil));
  2328. left:=nil;
  2329. firstpass(result);
  2330. end;
  2331. function ttypeconvnode.first_proc_to_procvar : tnode;
  2332. begin
  2333. first_proc_to_procvar:=nil;
  2334. { if we take the address of a nested function, it'll }
  2335. { probably be used in a foreach() construct and then }
  2336. { the parent needs a stackframe }
  2337. if (tprocdef(left.resultdef).parast.symtablelevel>=normal_function_level) then
  2338. include(current_procinfo.flags,pi_needs_stackframe);
  2339. if tabstractprocdef(resultdef).is_addressonly then
  2340. expectloc:=LOC_REGISTER
  2341. else
  2342. begin
  2343. if not(left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  2344. CGMessage(parser_e_illegal_expression);
  2345. expectloc:=left.expectloc;
  2346. end;
  2347. end;
  2348. function ttypeconvnode.first_nil_to_methodprocvar : tnode;
  2349. begin
  2350. first_nil_to_methodprocvar:=nil;
  2351. expectloc:=LOC_REFERENCE;
  2352. end;
  2353. function ttypeconvnode.first_set_to_set : tnode;
  2354. var
  2355. newstatement : tstatementnode;
  2356. temp : ttempcreatenode;
  2357. begin
  2358. { in theory, we should do range checking here,
  2359. but Delphi doesn't do it either (FK) }
  2360. if left.nodetype=setconstn then
  2361. begin
  2362. left.resultdef:=resultdef;
  2363. result:=left;
  2364. left:=nil;
  2365. end
  2366. { equal sets for the code generator? }
  2367. else if (left.resultdef.size=resultdef.size) and
  2368. (tsetdef(left.resultdef).setbase=tsetdef(resultdef).setbase) then
  2369. { TODO: This causes wrong (but Delphi-compatible) results for disjoint subsets}
  2370. { e.g., this prints true because of this:
  2371. var
  2372. sa: set of 1..2;
  2373. sb: set of 5..6;
  2374. b: byte;
  2375. begin
  2376. b:=1;
  2377. sa:=[1..2];
  2378. sb:=sa;
  2379. writeln(b in sb);
  2380. end.
  2381. }
  2382. begin
  2383. result:=left;
  2384. left:=nil;
  2385. end
  2386. else
  2387. begin
  2388. result:=internalstatements(newstatement);
  2389. { in case left is a smallset expression, it can be an addn or so. }
  2390. { fpc_varset_load expects a formal const parameter, which doesn't }
  2391. { accept set addn's -> assign to a temp first and pass the temp }
  2392. if not(left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  2393. begin
  2394. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,false);
  2395. addstatement(newstatement,temp);
  2396. { temp := left }
  2397. addstatement(newstatement,cassignmentnode.create(
  2398. ctemprefnode.create(temp),left));
  2399. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2400. addstatement(newstatement,ctemprefnode.create(temp));
  2401. left:=result;
  2402. firstpass(left);
  2403. { recreate the result's internalstatements list }
  2404. result:=internalstatements(newstatement);
  2405. end;
  2406. { create temp for result }
  2407. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2408. addstatement(newstatement,temp);
  2409. addstatement(newstatement,ccallnode.createintern('fpc_varset_load',
  2410. ccallparanode.create(cordconstnode.create(tsetdef(left.resultdef).setbase div 8 - tsetdef(resultdef).setbase div 8,sinttype,false),
  2411. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2412. ccallparanode.create(ctemprefnode.create(temp),
  2413. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  2414. ccallparanode.create(left,nil))))))
  2415. );
  2416. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2417. addstatement(newstatement,ctemprefnode.create(temp));
  2418. left:=nil;
  2419. end;
  2420. end;
  2421. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  2422. begin
  2423. first_ansistring_to_pchar:=nil;
  2424. expectloc:=LOC_REGISTER;
  2425. end;
  2426. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  2427. begin
  2428. first_arrayconstructor_to_set:=nil;
  2429. internalerror(200104022);
  2430. end;
  2431. function ttypeconvnode.first_class_to_intf : tnode;
  2432. var
  2433. hd : tobjectdef;
  2434. ImplIntf : TImplementedInterface;
  2435. begin
  2436. result:=nil;
  2437. expectloc:=LOC_REGISTER;
  2438. hd:=tobjectdef(left.resultdef);
  2439. while assigned(hd) do
  2440. begin
  2441. ImplIntf:=hd.find_implemented_interface(tobjectdef(resultdef));
  2442. if assigned(ImplIntf) then
  2443. begin
  2444. case ImplIntf.IType of
  2445. etStandard:
  2446. { handle in pass 2 }
  2447. ;
  2448. etFieldValue:
  2449. if is_interface(tobjectdef(resultdef)) then
  2450. begin
  2451. result:=left;
  2452. propaccesslist_to_node(result,tpropertysym(implintf.implementsgetter).owner,tpropertysym(implintf.implementsgetter).propaccesslist[palt_read]);
  2453. left:=nil;
  2454. end
  2455. else
  2456. begin
  2457. internalerror(200802213);
  2458. end;
  2459. etStaticMethodResult,
  2460. etVirtualMethodResult:
  2461. if is_interface(tobjectdef(resultdef)) then
  2462. begin
  2463. { constructor create(l:tnode; v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags); }
  2464. result:=ccallnode.create(nil,tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym),
  2465. tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym).owner,
  2466. left,[]);
  2467. addsymref(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym);
  2468. left:=nil;
  2469. end
  2470. else if is_class(tobjectdef(resultdef)) then
  2471. begin
  2472. internalerror(200802211);
  2473. end
  2474. else
  2475. internalerror(200802231);
  2476. else
  2477. internalerror(200802165);
  2478. end;
  2479. break;
  2480. end;
  2481. hd:=hd.childof;
  2482. end;
  2483. if hd=nil then
  2484. internalerror(200802164);
  2485. end;
  2486. function ttypeconvnode._first_int_to_int : tnode;
  2487. begin
  2488. result:=first_int_to_int;
  2489. end;
  2490. function ttypeconvnode._first_cstring_to_pchar : tnode;
  2491. begin
  2492. result:=first_cstring_to_pchar;
  2493. end;
  2494. function ttypeconvnode._first_cstring_to_int : tnode;
  2495. begin
  2496. result:=first_cstring_to_int;
  2497. end;
  2498. function ttypeconvnode._first_string_to_chararray : tnode;
  2499. begin
  2500. result:=first_string_to_chararray;
  2501. end;
  2502. function ttypeconvnode._first_char_to_string : tnode;
  2503. begin
  2504. result:=first_char_to_string;
  2505. end;
  2506. function ttypeconvnode._first_nothing : tnode;
  2507. begin
  2508. result:=first_nothing;
  2509. end;
  2510. function ttypeconvnode._first_array_to_pointer : tnode;
  2511. begin
  2512. result:=first_array_to_pointer;
  2513. end;
  2514. function ttypeconvnode._first_int_to_real : tnode;
  2515. begin
  2516. result:=first_int_to_real;
  2517. end;
  2518. function ttypeconvnode._first_real_to_real : tnode;
  2519. begin
  2520. result:=first_real_to_real;
  2521. end;
  2522. function ttypeconvnode._first_pointer_to_array : tnode;
  2523. begin
  2524. result:=first_pointer_to_array;
  2525. end;
  2526. function ttypeconvnode._first_cchar_to_pchar : tnode;
  2527. begin
  2528. result:=first_cchar_to_pchar;
  2529. end;
  2530. function ttypeconvnode._first_bool_to_int : tnode;
  2531. begin
  2532. result:=first_bool_to_int;
  2533. end;
  2534. function ttypeconvnode._first_int_to_bool : tnode;
  2535. begin
  2536. result:=first_int_to_bool;
  2537. end;
  2538. function ttypeconvnode._first_bool_to_bool : tnode;
  2539. begin
  2540. result:=first_bool_to_bool;
  2541. end;
  2542. function ttypeconvnode._first_proc_to_procvar : tnode;
  2543. begin
  2544. result:=first_proc_to_procvar;
  2545. end;
  2546. function ttypeconvnode._first_nil_to_methodprocvar : tnode;
  2547. begin
  2548. result:=first_nil_to_methodprocvar;
  2549. end;
  2550. function ttypeconvnode._first_set_to_set : tnode;
  2551. begin
  2552. result:=first_set_to_set;
  2553. end;
  2554. function ttypeconvnode._first_cord_to_pointer : tnode;
  2555. begin
  2556. result:=first_cord_to_pointer;
  2557. end;
  2558. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  2559. begin
  2560. result:=first_ansistring_to_pchar;
  2561. end;
  2562. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  2563. begin
  2564. result:=first_arrayconstructor_to_set;
  2565. end;
  2566. function ttypeconvnode._first_class_to_intf : tnode;
  2567. begin
  2568. result:=first_class_to_intf;
  2569. end;
  2570. function ttypeconvnode._first_char_to_char : tnode;
  2571. begin
  2572. result:=first_char_to_char;
  2573. end;
  2574. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  2575. const
  2576. firstconvert : array[tconverttype] of pointer = (
  2577. nil, { none }
  2578. @ttypeconvnode._first_nothing, {equal}
  2579. @ttypeconvnode._first_nothing, {not_possible}
  2580. nil, { removed in typecheck_string_to_string }
  2581. @ttypeconvnode._first_char_to_string,
  2582. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  2583. nil, { removed in typecheck_chararray_to_string }
  2584. @ttypeconvnode._first_cchar_to_pchar,
  2585. @ttypeconvnode._first_cstring_to_pchar,
  2586. @ttypeconvnode._first_cstring_to_int,
  2587. @ttypeconvnode._first_ansistring_to_pchar,
  2588. @ttypeconvnode._first_string_to_chararray,
  2589. nil, { removed in typecheck_chararray_to_string }
  2590. @ttypeconvnode._first_array_to_pointer,
  2591. @ttypeconvnode._first_pointer_to_array,
  2592. @ttypeconvnode._first_int_to_int,
  2593. @ttypeconvnode._first_int_to_bool,
  2594. @ttypeconvnode._first_bool_to_bool,
  2595. @ttypeconvnode._first_bool_to_int,
  2596. @ttypeconvnode._first_real_to_real,
  2597. @ttypeconvnode._first_int_to_real,
  2598. nil, { removed in typecheck_real_to_currency }
  2599. @ttypeconvnode._first_proc_to_procvar,
  2600. @ttypeconvnode._first_nil_to_methodprocvar,
  2601. @ttypeconvnode._first_arrayconstructor_to_set,
  2602. @ttypeconvnode._first_set_to_set,
  2603. @ttypeconvnode._first_cord_to_pointer,
  2604. @ttypeconvnode._first_nothing,
  2605. @ttypeconvnode._first_nothing,
  2606. @ttypeconvnode._first_class_to_intf,
  2607. @ttypeconvnode._first_char_to_char,
  2608. @ttypeconvnode._first_nothing,
  2609. @ttypeconvnode._first_nothing,
  2610. nil,
  2611. nil,
  2612. nil,
  2613. nil,
  2614. nil,
  2615. nil,
  2616. nil
  2617. );
  2618. type
  2619. tprocedureofobject = function : tnode of object;
  2620. var
  2621. r : packed record
  2622. proc : pointer;
  2623. obj : pointer;
  2624. end;
  2625. begin
  2626. { this is a little bit dirty but it works }
  2627. { and should be quite portable too }
  2628. r.proc:=firstconvert[c];
  2629. r.obj:=self;
  2630. if not assigned(r.proc) then
  2631. internalerror(200312081);
  2632. first_call_helper:=tprocedureofobject(r)()
  2633. end;
  2634. function ttypeconvnode.pass_1 : tnode;
  2635. begin
  2636. if warn_pointer_to_signed then
  2637. cgmessage(type_w_pointer_to_signed);
  2638. result:=nil;
  2639. firstpass(left);
  2640. if codegenerror then
  2641. exit;
  2642. expectloc:=left.expectloc;
  2643. result:=first_call_helper(convtype);
  2644. end;
  2645. function ttypeconvnode.assign_allowed:boolean;
  2646. begin
  2647. result:=(convtype=tc_equal) or
  2648. { typecasting from void is always allowed }
  2649. is_void(left.resultdef) or
  2650. (left.resultdef.typ=formaldef) or
  2651. { int 2 int with same size reuses same location, or for
  2652. tp7 mode also allow size < orignal size }
  2653. (
  2654. (convtype=tc_int_2_int) and
  2655. (
  2656. (resultdef.size=left.resultdef.size) or
  2657. ((m_tp7 in current_settings.modeswitches) and
  2658. (resultdef.size<left.resultdef.size))
  2659. )
  2660. ) or
  2661. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  2662. ((convtype in [tc_int_2_bool,tc_bool_2_int,tc_bool_2_bool]) and
  2663. (nf_explicit in flags) and
  2664. (resultdef.size=left.resultdef.size));
  2665. { When using only a part of the value it can't be in a register since
  2666. that will load the value in a new register first }
  2667. { the same goes for changing the sign of equal-sized values which
  2668. are smaller than an entire register }
  2669. if (resultdef.size<left.resultdef.size) or
  2670. ((resultdef.size=left.resultdef.size) and
  2671. (left.resultdef.size<sizeof(aint)) and
  2672. (is_signed(resultdef) xor is_signed(left.resultdef))) then
  2673. make_not_regable(left,[ra_addr_regable]);
  2674. end;
  2675. function ttypeconvnode.docompare(p: tnode) : boolean;
  2676. begin
  2677. docompare :=
  2678. inherited docompare(p) and
  2679. (convtype = ttypeconvnode(p).convtype);
  2680. end;
  2681. procedure ttypeconvnode._second_int_to_int;
  2682. begin
  2683. second_int_to_int;
  2684. end;
  2685. procedure ttypeconvnode._second_string_to_string;
  2686. begin
  2687. second_string_to_string;
  2688. end;
  2689. procedure ttypeconvnode._second_cstring_to_pchar;
  2690. begin
  2691. second_cstring_to_pchar;
  2692. end;
  2693. procedure ttypeconvnode._second_cstring_to_int;
  2694. begin
  2695. second_cstring_to_int;
  2696. end;
  2697. procedure ttypeconvnode._second_string_to_chararray;
  2698. begin
  2699. second_string_to_chararray;
  2700. end;
  2701. procedure ttypeconvnode._second_array_to_pointer;
  2702. begin
  2703. second_array_to_pointer;
  2704. end;
  2705. procedure ttypeconvnode._second_pointer_to_array;
  2706. begin
  2707. second_pointer_to_array;
  2708. end;
  2709. procedure ttypeconvnode._second_chararray_to_string;
  2710. begin
  2711. second_chararray_to_string;
  2712. end;
  2713. procedure ttypeconvnode._second_char_to_string;
  2714. begin
  2715. second_char_to_string;
  2716. end;
  2717. procedure ttypeconvnode._second_int_to_real;
  2718. begin
  2719. second_int_to_real;
  2720. end;
  2721. procedure ttypeconvnode._second_real_to_real;
  2722. begin
  2723. second_real_to_real;
  2724. end;
  2725. procedure ttypeconvnode._second_cord_to_pointer;
  2726. begin
  2727. second_cord_to_pointer;
  2728. end;
  2729. procedure ttypeconvnode._second_proc_to_procvar;
  2730. begin
  2731. second_proc_to_procvar;
  2732. end;
  2733. procedure ttypeconvnode._second_nil_to_methodprocvar;
  2734. begin
  2735. second_nil_to_methodprocvar;
  2736. end;
  2737. procedure ttypeconvnode._second_bool_to_int;
  2738. begin
  2739. second_bool_to_int;
  2740. end;
  2741. procedure ttypeconvnode._second_int_to_bool;
  2742. begin
  2743. second_int_to_bool;
  2744. end;
  2745. procedure ttypeconvnode._second_bool_to_bool;
  2746. begin
  2747. second_bool_to_bool;
  2748. end;
  2749. procedure ttypeconvnode._second_set_to_set;
  2750. begin
  2751. second_set_to_set;
  2752. end;
  2753. procedure ttypeconvnode._second_ansistring_to_pchar;
  2754. begin
  2755. second_ansistring_to_pchar;
  2756. end;
  2757. procedure ttypeconvnode._second_class_to_intf;
  2758. begin
  2759. second_class_to_intf;
  2760. end;
  2761. procedure ttypeconvnode._second_char_to_char;
  2762. begin
  2763. second_char_to_char;
  2764. end;
  2765. procedure ttypeconvnode._second_nothing;
  2766. begin
  2767. second_nothing;
  2768. end;
  2769. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  2770. const
  2771. secondconvert : array[tconverttype] of pointer = (
  2772. @ttypeconvnode._second_nothing, {none}
  2773. @ttypeconvnode._second_nothing, {equal}
  2774. @ttypeconvnode._second_nothing, {not_possible}
  2775. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resultdef pass }
  2776. @ttypeconvnode._second_char_to_string,
  2777. @ttypeconvnode._second_nothing, {char_to_charray}
  2778. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resultdef pass }
  2779. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  2780. @ttypeconvnode._second_cstring_to_pchar,
  2781. @ttypeconvnode._second_cstring_to_int,
  2782. @ttypeconvnode._second_ansistring_to_pchar,
  2783. @ttypeconvnode._second_string_to_chararray,
  2784. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resultdef pass }
  2785. @ttypeconvnode._second_array_to_pointer,
  2786. @ttypeconvnode._second_pointer_to_array,
  2787. @ttypeconvnode._second_int_to_int,
  2788. @ttypeconvnode._second_int_to_bool,
  2789. @ttypeconvnode._second_bool_to_bool,
  2790. @ttypeconvnode._second_bool_to_int,
  2791. @ttypeconvnode._second_real_to_real,
  2792. @ttypeconvnode._second_int_to_real,
  2793. @ttypeconvnode._second_nothing, { real_to_currency, handled in resultdef pass }
  2794. @ttypeconvnode._second_proc_to_procvar,
  2795. @ttypeconvnode._second_nil_to_methodprocvar,
  2796. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  2797. @ttypeconvnode._second_nothing, { second_set_to_set, handled in first pass }
  2798. @ttypeconvnode._second_cord_to_pointer,
  2799. @ttypeconvnode._second_nothing, { interface 2 string }
  2800. @ttypeconvnode._second_nothing, { interface 2 guid }
  2801. @ttypeconvnode._second_class_to_intf,
  2802. @ttypeconvnode._second_char_to_char,
  2803. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  2804. @ttypeconvnode._second_nothing, { pwchar_2_string }
  2805. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  2806. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  2807. @ttypeconvnode._second_nothing, { variant_2_enum }
  2808. @ttypeconvnode._second_nothing, { enum_2_variant }
  2809. @ttypeconvnode._second_nothing, { variant_2_interface }
  2810. @ttypeconvnode._second_nothing, { interface_2_variant }
  2811. @ttypeconvnode._second_nothing { array_2_dynarray }
  2812. );
  2813. type
  2814. tprocedureofobject = procedure of object;
  2815. var
  2816. r : packed record
  2817. proc : pointer;
  2818. obj : pointer;
  2819. end;
  2820. begin
  2821. { this is a little bit dirty but it works }
  2822. { and should be quite portable too }
  2823. r.proc:=secondconvert[c];
  2824. r.obj:=self;
  2825. tprocedureofobject(r)();
  2826. end;
  2827. {*****************************************************************************
  2828. TISNODE
  2829. *****************************************************************************}
  2830. constructor tisnode.create(l,r : tnode);
  2831. begin
  2832. inherited create(isn,l,r);
  2833. end;
  2834. function tisnode.pass_typecheck:tnode;
  2835. var
  2836. paras: tcallparanode;
  2837. begin
  2838. result:=nil;
  2839. typecheckpass(left);
  2840. typecheckpass(right);
  2841. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2842. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2843. if codegenerror then
  2844. exit;
  2845. if (right.resultdef.typ=classrefdef) then
  2846. begin
  2847. { left must be a class }
  2848. if is_class(left.resultdef) then
  2849. begin
  2850. { the operands must be related }
  2851. if (not(tobjectdef(left.resultdef).is_related(
  2852. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  2853. (not(tobjectdef(tclassrefdef(right.resultdef).pointeddef).is_related(
  2854. tobjectdef(left.resultdef)))) then
  2855. CGMessage2(type_e_classes_not_related,left.resultdef.typename,
  2856. tclassrefdef(right.resultdef).pointeddef.typename);
  2857. end
  2858. else
  2859. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2860. { call fpc_do_is helper }
  2861. paras := ccallparanode.create(
  2862. left,
  2863. ccallparanode.create(
  2864. right,nil));
  2865. result := ccallnode.createintern('fpc_do_is',paras);
  2866. left := nil;
  2867. right := nil;
  2868. end
  2869. else if is_interface(right.resultdef) then
  2870. begin
  2871. { left is a class }
  2872. if is_class(left.resultdef) then
  2873. begin
  2874. { the class must implement the interface }
  2875. if tobjectdef(left.resultdef).find_implemented_interface(tobjectdef(right.resultdef))=nil then
  2876. CGMessage2(type_e_classes_not_related,
  2877. FullTypeName(left.resultdef,right.resultdef),
  2878. FullTypeName(right.resultdef,left.resultdef))
  2879. end
  2880. { left is an interface }
  2881. else if is_interface(left.resultdef) then
  2882. begin
  2883. { the operands must be related }
  2884. if (not(tobjectdef(left.resultdef).is_related(tobjectdef(right.resultdef)))) and
  2885. (not(tobjectdef(right.resultdef).is_related(tobjectdef(left.resultdef)))) then
  2886. CGMessage2(type_e_classes_not_related,
  2887. FullTypeName(left.resultdef,right.resultdef),
  2888. FullTypeName(right.resultdef,left.resultdef));
  2889. end
  2890. else
  2891. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2892. { call fpc_do_is helper }
  2893. paras := ccallparanode.create(
  2894. left,
  2895. ccallparanode.create(
  2896. right,nil));
  2897. result := ccallnode.createintern('fpc_do_is',paras);
  2898. left := nil;
  2899. right := nil;
  2900. end
  2901. else
  2902. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  2903. resultdef:=booltype;
  2904. end;
  2905. function tisnode.pass_1 : tnode;
  2906. begin
  2907. internalerror(200204254);
  2908. result:=nil;
  2909. end;
  2910. { dummy pass_2, it will never be called, but we need one since }
  2911. { you can't instantiate an abstract class }
  2912. procedure tisnode.pass_generate_code;
  2913. begin
  2914. end;
  2915. {*****************************************************************************
  2916. TASNODE
  2917. *****************************************************************************}
  2918. constructor tasnode.create(l,r : tnode);
  2919. begin
  2920. inherited create(asn,l,r);
  2921. call := nil;
  2922. end;
  2923. destructor tasnode.destroy;
  2924. begin
  2925. call.free;
  2926. inherited destroy;
  2927. end;
  2928. function tasnode.pass_typecheck:tnode;
  2929. var
  2930. hp : tnode;
  2931. begin
  2932. result:=nil;
  2933. typecheckpass(right);
  2934. typecheckpass(left);
  2935. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2936. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2937. if codegenerror then
  2938. exit;
  2939. if (right.resultdef.typ=classrefdef) then
  2940. begin
  2941. { left must be a class }
  2942. if is_class(left.resultdef) then
  2943. begin
  2944. { the operands must be related }
  2945. if (not(tobjectdef(left.resultdef).is_related(
  2946. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  2947. (not(tobjectdef(tclassrefdef(right.resultdef).pointeddef).is_related(
  2948. tobjectdef(left.resultdef)))) then
  2949. CGMessage2(type_e_classes_not_related,
  2950. FullTypeName(left.resultdef,tclassrefdef(right.resultdef).pointeddef),
  2951. FullTypeName(tclassrefdef(right.resultdef).pointeddef,left.resultdef));
  2952. end
  2953. else
  2954. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2955. resultdef:=tclassrefdef(right.resultdef).pointeddef;
  2956. end
  2957. else if is_interface(right.resultdef) then
  2958. begin
  2959. { left is a class }
  2960. if not(is_class(left.resultdef) or
  2961. is_interfacecom(left.resultdef)) then
  2962. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  2963. resultdef:=right.resultdef;
  2964. { load the GUID of the interface }
  2965. if (right.nodetype=typen) then
  2966. begin
  2967. if assigned(tobjectdef(right.resultdef).iidguid) then
  2968. begin
  2969. if not(oo_has_valid_guid in tobjectdef(right.resultdef).objectoptions) then
  2970. CGMessage1(type_interface_has_no_guid,tobjectdef(right.resultdef).typename);
  2971. hp:=cguidconstnode.create(tobjectdef(right.resultdef).iidguid^);
  2972. right.free;
  2973. right:=hp;
  2974. end
  2975. else
  2976. internalerror(200206282);
  2977. typecheckpass(right);
  2978. end;
  2979. end
  2980. else
  2981. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  2982. end;
  2983. function tasnode.dogetcopy: tnode;
  2984. begin
  2985. result := inherited dogetcopy;
  2986. if assigned(call) then
  2987. tasnode(result).call := call.getcopy
  2988. else
  2989. tasnode(result).call := nil;
  2990. end;
  2991. function tasnode.pass_1 : tnode;
  2992. var
  2993. procname: string;
  2994. begin
  2995. result:=nil;
  2996. if not assigned(call) then
  2997. begin
  2998. if is_class(left.resultdef) and
  2999. (right.resultdef.typ=classrefdef) then
  3000. call := ccallnode.createinternres('fpc_do_as',
  3001. ccallparanode.create(left,ccallparanode.create(right,nil)),
  3002. resultdef)
  3003. else
  3004. begin
  3005. if is_class(left.resultdef) then
  3006. procname := 'fpc_class_as_intf'
  3007. else
  3008. procname := 'fpc_intf_as';
  3009. call := ccallnode.createintern(procname,
  3010. ccallparanode.create(right,ccallparanode.create(left,nil)));
  3011. call := ctypeconvnode.create_internal(call,resultdef);
  3012. end;
  3013. left := nil;
  3014. right := nil;
  3015. firstpass(call);
  3016. if codegenerror then
  3017. exit;
  3018. expectloc:=call.expectloc;
  3019. end;
  3020. end;
  3021. begin
  3022. ctypeconvnode:=ttypeconvnode;
  3023. casnode:=tasnode;
  3024. cisnode:=tisnode;
  3025. end.