ncnv.pas 135 KB

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