ncnv.pas 117 KB

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