ncnv.pas 125 KB

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