pexpr.pas 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Does parsing of expression for Free Pascal
  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 pexpr;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. symtype,symdef,symbase,
  22. node,ncal,
  23. tokens,globtype,globals,constexp;
  24. { reads a whole expression }
  25. function expr(dotypecheck:boolean) : tnode;
  26. { reads an expression without assignements and .. }
  27. function comp_expr(accept_equal,typeonly:boolean):tnode;
  28. { reads a single factor }
  29. function factor(getaddr,typeonly:boolean) : tnode;
  30. procedure string_dec(var def: tdef; allowtypedef: boolean);
  31. function parse_paras(__colon,__namedpara : boolean;end_of_paras : ttoken) : tnode;
  32. { the ID token has to be consumed before calling this function }
  33. procedure do_member_read(structh:tabstractrecorddef;getaddr:boolean;sym:tsym;var p1:tnode;var again:boolean;callflags:tcallnodeflags);
  34. function get_intconst:TConstExprInt;
  35. function get_stringconst:string;
  36. implementation
  37. uses
  38. { common }
  39. cutils,
  40. { global }
  41. verbose,
  42. systems,widestr,
  43. { symtable }
  44. symconst,symtable,symsym,defutil,defcmp,
  45. { module }
  46. fmodule,ppu,
  47. { pass 1 }
  48. pass_1,htypechk,
  49. nmat,nadd,nmem,nset,ncnv,ninl,ncon,nld,nflw,nbas,nutils,
  50. { parser }
  51. scanner,
  52. pbase,pinline,ptype,
  53. { codegen }
  54. cgbase,procinfo,cpuinfo
  55. ;
  56. { sub_expr(opmultiply) is need to get -1 ** 4 to be
  57. read as - (1**4) and not (-1)**4 PM }
  58. type
  59. Toperator_precedence=(opcompare,opaddition,opmultiply,oppower);
  60. const
  61. highest_precedence = oppower;
  62. function sub_expr(pred_level:Toperator_precedence;accept_equal,typeonly:boolean):tnode;forward;
  63. const
  64. { true, if the inherited call is anonymous }
  65. anon_inherited : boolean = false;
  66. { last def found, only used by anon. inherited calls to insert proper type casts }
  67. srdef : tdef = nil;
  68. procedure string_dec(var def:tdef; allowtypedef: boolean);
  69. { reads a string type with optional length }
  70. { and returns a pointer to the string }
  71. { definition }
  72. var
  73. p : tnode;
  74. begin
  75. def:=cshortstringtype;
  76. consume(_STRING);
  77. if (token=_LECKKLAMMER) then
  78. begin
  79. if not(allowtypedef) then
  80. Message(parser_e_no_local_para_def);
  81. consume(_LECKKLAMMER);
  82. p:=comp_expr(true,false);
  83. if not is_constintnode(p) then
  84. begin
  85. Message(parser_e_illegal_expression);
  86. { error recovery }
  87. consume(_RECKKLAMMER);
  88. end
  89. else
  90. begin
  91. if (tordconstnode(p).value<=0) then
  92. begin
  93. Message(parser_e_invalid_string_size);
  94. tordconstnode(p).value:=255;
  95. end;
  96. consume(_RECKKLAMMER);
  97. if tordconstnode(p).value>255 then
  98. begin
  99. { longstring is currently unsupported (CEC)! }
  100. { t:=tstringdef.createlong(tordconstnode(p).value))}
  101. Message(parser_e_invalid_string_size);
  102. tordconstnode(p).value:=255;
  103. def:=tstringdef.createshort(int64(tordconstnode(p).value));
  104. end
  105. else
  106. if tordconstnode(p).value<>255 then
  107. def:=tstringdef.createshort(int64(tordconstnode(p).value));
  108. end;
  109. p.free;
  110. end
  111. else
  112. begin
  113. if cs_ansistrings in current_settings.localswitches then
  114. def:=cansistringtype
  115. else
  116. def:=cshortstringtype;
  117. end;
  118. end;
  119. function parse_paras(__colon,__namedpara : boolean;end_of_paras : ttoken) : tnode;
  120. var
  121. p1,p2,argname : tnode;
  122. prev_in_args,
  123. old_named_args_allowed,
  124. old_allow_array_constructor : boolean;
  125. begin
  126. if token=end_of_paras then
  127. begin
  128. parse_paras:=nil;
  129. exit;
  130. end;
  131. { save old values }
  132. prev_in_args:=in_args;
  133. old_allow_array_constructor:=allow_array_constructor;
  134. old_named_args_allowed:=named_args_allowed;
  135. { set para parsing values }
  136. in_args:=true;
  137. named_args_allowed:=false;
  138. allow_array_constructor:=true;
  139. p2:=nil;
  140. repeat
  141. if __namedpara then
  142. begin
  143. if token=_COMMA then
  144. begin
  145. { empty parameter }
  146. p2:=ccallparanode.create(cnothingnode.create,p2);
  147. end
  148. else
  149. begin
  150. named_args_allowed:=true;
  151. p1:=comp_expr(true,false);
  152. named_args_allowed:=false;
  153. if found_arg_name then
  154. begin
  155. argname:=p1;
  156. p1:=comp_expr(true,false);
  157. p2:=ccallparanode.create(p1,p2);
  158. tcallparanode(p2).parametername:=argname;
  159. end
  160. else
  161. p2:=ccallparanode.create(p1,p2);
  162. found_arg_name:=false;
  163. end;
  164. end
  165. else
  166. begin
  167. p1:=comp_expr(true,false);
  168. p2:=ccallparanode.create(p1,p2);
  169. end;
  170. { it's for the str(l:5,s); }
  171. if __colon and (token=_COLON) then
  172. begin
  173. consume(_COLON);
  174. p1:=comp_expr(true,false);
  175. p2:=ccallparanode.create(p1,p2);
  176. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  177. if try_to_consume(_COLON) then
  178. begin
  179. p1:=comp_expr(true,false);
  180. p2:=ccallparanode.create(p1,p2);
  181. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  182. end
  183. end;
  184. until not try_to_consume(_COMMA);
  185. allow_array_constructor:=old_allow_array_constructor;
  186. in_args:=prev_in_args;
  187. named_args_allowed:=old_named_args_allowed;
  188. parse_paras:=p2;
  189. end;
  190. function gen_c_style_operator(ntyp:tnodetype;p1,p2:tnode) : tnode;
  191. var
  192. hp : tnode;
  193. hdef : tdef;
  194. temp : ttempcreatenode;
  195. newstatement : tstatementnode;
  196. begin
  197. { Properties are not allowed, because the write can
  198. be different from the read }
  199. if (nf_isproperty in p1.flags) then
  200. begin
  201. Message(type_e_variable_id_expected);
  202. { We can continue with the loading,
  203. it'll not create errors. Only the expected
  204. result can be wrong }
  205. end;
  206. hp:=p1;
  207. while assigned(hp) and
  208. (hp.nodetype in [derefn,subscriptn,vecn,typeconvn]) do
  209. hp:=tunarynode(hp).left;
  210. if not assigned(hp) then
  211. internalerror(200410121);
  212. if (hp.nodetype=calln) then
  213. begin
  214. typecheckpass(p1);
  215. result:=internalstatements(newstatement);
  216. hdef:=tpointerdef.create(p1.resultdef);
  217. temp:=ctempcreatenode.create(hdef,sizeof(pint),tt_persistent,false);
  218. addstatement(newstatement,temp);
  219. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(temp),caddrnode.create_internal(p1)));
  220. addstatement(newstatement,cassignmentnode.create(
  221. cderefnode.create(ctemprefnode.create(temp)),
  222. caddnode.create(ntyp,
  223. cderefnode.create(ctemprefnode.create(temp)),
  224. p2)));
  225. addstatement(newstatement,ctempdeletenode.create(temp));
  226. end
  227. else
  228. result:=cassignmentnode.create(p1,caddnode.create(ntyp,p1.getcopy,p2));
  229. end;
  230. function statement_syssym(l : byte) : tnode;
  231. var
  232. p1,p2,paras : tnode;
  233. err,
  234. prev_in_args : boolean;
  235. begin
  236. prev_in_args:=in_args;
  237. case l of
  238. in_new_x :
  239. begin
  240. if afterassignment or in_args then
  241. statement_syssym:=new_function
  242. else
  243. statement_syssym:=new_dispose_statement(true);
  244. end;
  245. in_dispose_x :
  246. begin
  247. statement_syssym:=new_dispose_statement(false);
  248. end;
  249. in_ord_x :
  250. begin
  251. consume(_LKLAMMER);
  252. in_args:=true;
  253. p1:=comp_expr(true,false);
  254. consume(_RKLAMMER);
  255. p1:=geninlinenode(in_ord_x,false,p1);
  256. statement_syssym := p1;
  257. end;
  258. in_exit :
  259. begin
  260. if try_to_consume(_LKLAMMER) then
  261. begin
  262. if not (m_mac in current_settings.modeswitches) then
  263. begin
  264. if not(try_to_consume(_RKLAMMER)) then
  265. begin
  266. p1:=comp_expr(true,false);
  267. consume(_RKLAMMER);
  268. if (not assigned(current_procinfo) or
  269. is_void(current_procinfo.procdef.returndef)) then
  270. begin
  271. Message(parser_e_void_function);
  272. { recovery }
  273. p1.free;
  274. p1:=nil;
  275. end;
  276. end
  277. else
  278. p1:=nil;
  279. end
  280. else
  281. begin
  282. if not (current_procinfo.procdef.procsym.name = pattern) then
  283. Message(parser_e_macpas_exit_wrong_param);
  284. consume(_ID);
  285. consume(_RKLAMMER);
  286. p1:=nil;
  287. end
  288. end
  289. else
  290. p1:=nil;
  291. statement_syssym:=cexitnode.create(p1);
  292. end;
  293. in_break :
  294. begin
  295. statement_syssym:=cbreaknode.create
  296. end;
  297. in_continue :
  298. begin
  299. statement_syssym:=ccontinuenode.create
  300. end;
  301. in_leave :
  302. begin
  303. if m_mac in current_settings.modeswitches then
  304. statement_syssym:=cbreaknode.create
  305. else
  306. begin
  307. Message1(sym_e_id_not_found, orgpattern);
  308. statement_syssym:=cerrornode.create;
  309. end;
  310. end;
  311. in_cycle :
  312. begin
  313. if m_mac in current_settings.modeswitches then
  314. statement_syssym:=ccontinuenode.create
  315. else
  316. begin
  317. Message1(sym_e_id_not_found, orgpattern);
  318. statement_syssym:=cerrornode.create;
  319. end;
  320. end;
  321. in_typeof_x :
  322. begin
  323. consume(_LKLAMMER);
  324. in_args:=true;
  325. p1:=comp_expr(true,false);
  326. consume(_RKLAMMER);
  327. if p1.nodetype=typen then
  328. ttypenode(p1).allowed:=true;
  329. { Allow classrefdef, which is required for
  330. Typeof(self) in static class methods }
  331. if not(is_objc_class_or_protocol(p1.resultdef)) and
  332. ((p1.resultdef.typ = objectdef) or
  333. (assigned(current_procinfo) and
  334. ((po_classmethod in current_procinfo.procdef.procoptions) or
  335. (po_staticmethod in current_procinfo.procdef.procoptions)) and
  336. (p1.resultdef.typ=classrefdef))) then
  337. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  338. else
  339. begin
  340. Message(parser_e_class_id_expected);
  341. p1.destroy;
  342. statement_syssym:=cerrornode.create;
  343. end;
  344. end;
  345. in_sizeof_x,
  346. in_bitsizeof_x :
  347. begin
  348. consume(_LKLAMMER);
  349. in_args:=true;
  350. p1:=comp_expr(true,false);
  351. consume(_RKLAMMER);
  352. if (p1.nodetype<>typen) and
  353. (
  354. (is_object(p1.resultdef) and
  355. (oo_has_constructor in tobjectdef(p1.resultdef).objectoptions)) or
  356. is_open_array(p1.resultdef) or
  357. is_array_of_const(p1.resultdef) or
  358. is_open_string(p1.resultdef)
  359. ) then
  360. begin
  361. statement_syssym:=geninlinenode(in_sizeof_x,false,p1);
  362. { no packed bit support for these things }
  363. if (l = in_bitsizeof_x) then
  364. statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sinttype,true));
  365. end
  366. else
  367. begin
  368. if (p1.resultdef.typ=forwarddef) then
  369. Message1(type_e_type_is_not_completly_defined,tforwarddef(p1.resultdef).tosymname^);
  370. if (l = in_sizeof_x) or
  371. (not((p1.nodetype = vecn) and
  372. is_packed_array(tvecnode(p1).left.resultdef)) and
  373. not((p1.nodetype = subscriptn) and
  374. is_packed_record_or_object(tsubscriptnode(p1).left.resultdef))) then
  375. begin
  376. statement_syssym:=cordconstnode.create(p1.resultdef.size,sinttype,true);
  377. if (l = in_bitsizeof_x) then
  378. statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sinttype,true));
  379. end
  380. else
  381. statement_syssym:=cordconstnode.create(p1.resultdef.packedbitsize,sinttype,true);
  382. { p1 not needed !}
  383. p1.destroy;
  384. end;
  385. end;
  386. in_typeinfo_x,
  387. in_objc_encode_x :
  388. begin
  389. if (l=in_typeinfo_x) or
  390. (m_objectivec1 in current_settings.modeswitches) then
  391. begin
  392. consume(_LKLAMMER);
  393. in_args:=true;
  394. p1:=comp_expr(true,false);
  395. { When reading a class type it is parsed as loadvmtaddrn,
  396. typeinfo only needs the type so we remove the loadvmtaddrn }
  397. if p1.nodetype=loadvmtaddrn then
  398. begin
  399. p2:=tloadvmtaddrnode(p1).left;
  400. tloadvmtaddrnode(p1).left:=nil;
  401. p1.free;
  402. p1:=p2;
  403. end;
  404. if p1.nodetype=typen then
  405. ttypenode(p1).allowed:=true;
  406. { else
  407. begin
  408. p1.destroy;
  409. p1:=cerrornode.create;
  410. Message(parser_e_illegal_parameter_list);
  411. end;}
  412. consume(_RKLAMMER);
  413. p2:=geninlinenode(l,false,p1);
  414. statement_syssym:=p2;
  415. end
  416. else
  417. begin
  418. Message1(sym_e_id_not_found, orgpattern);
  419. statement_syssym:=cerrornode.create;
  420. end;
  421. end;
  422. in_unaligned_x :
  423. begin
  424. err:=false;
  425. consume(_LKLAMMER);
  426. in_args:=true;
  427. p1:=comp_expr(true,false);
  428. p2:=ccallparanode.create(p1,nil);
  429. p2:=geninlinenode(in_unaligned_x,false,p2);
  430. consume(_RKLAMMER);
  431. statement_syssym:=p2;
  432. end;
  433. in_assigned_x :
  434. begin
  435. err:=false;
  436. consume(_LKLAMMER);
  437. in_args:=true;
  438. p1:=comp_expr(true,false);
  439. { When reading a class type it is parsed as loadvmtaddrn,
  440. typeinfo only needs the type so we remove the loadvmtaddrn }
  441. if p1.nodetype=loadvmtaddrn then
  442. begin
  443. p2:=tloadvmtaddrnode(p1).left;
  444. tloadvmtaddrnode(p1).left:=nil;
  445. p1.free;
  446. p1:=p2;
  447. end;
  448. if not codegenerror then
  449. begin
  450. case p1.resultdef.typ of
  451. procdef, { procvar }
  452. pointerdef,
  453. procvardef,
  454. classrefdef : ;
  455. objectdef :
  456. if not is_implicit_pointer_object_type(p1.resultdef) then
  457. begin
  458. Message(parser_e_illegal_parameter_list);
  459. err:=true;
  460. end;
  461. arraydef :
  462. if not is_dynamic_array(p1.resultdef) then
  463. begin
  464. Message(parser_e_illegal_parameter_list);
  465. err:=true;
  466. end;
  467. else
  468. begin
  469. Message(parser_e_illegal_parameter_list);
  470. err:=true;
  471. end;
  472. end;
  473. end
  474. else
  475. err:=true;
  476. if not err then
  477. begin
  478. p2:=ccallparanode.create(p1,nil);
  479. p2:=geninlinenode(in_assigned_x,false,p2);
  480. end
  481. else
  482. begin
  483. p1.free;
  484. p2:=cerrornode.create;
  485. end;
  486. consume(_RKLAMMER);
  487. statement_syssym:=p2;
  488. end;
  489. in_addr_x :
  490. begin
  491. consume(_LKLAMMER);
  492. in_args:=true;
  493. p1:=comp_expr(true,false);
  494. p1:=caddrnode.create(p1);
  495. consume(_RKLAMMER);
  496. statement_syssym:=p1;
  497. end;
  498. in_ofs_x :
  499. begin
  500. consume(_LKLAMMER);
  501. in_args:=true;
  502. p1:=comp_expr(true,false);
  503. p1:=caddrnode.create(p1);
  504. do_typecheckpass(p1);
  505. { Ofs() returns a cardinal/qword, not a pointer }
  506. p1.resultdef:=uinttype;
  507. consume(_RKLAMMER);
  508. statement_syssym:=p1;
  509. end;
  510. in_seg_x :
  511. begin
  512. consume(_LKLAMMER);
  513. in_args:=true;
  514. p1:=comp_expr(true,false);
  515. p1:=geninlinenode(in_seg_x,false,p1);
  516. consume(_RKLAMMER);
  517. statement_syssym:=p1;
  518. end;
  519. in_high_x,
  520. in_low_x :
  521. begin
  522. consume(_LKLAMMER);
  523. in_args:=true;
  524. p1:=comp_expr(true,false);
  525. p2:=geninlinenode(l,false,p1);
  526. consume(_RKLAMMER);
  527. statement_syssym:=p2;
  528. end;
  529. in_succ_x,
  530. in_pred_x :
  531. begin
  532. consume(_LKLAMMER);
  533. in_args:=true;
  534. p1:=comp_expr(true,false);
  535. p2:=geninlinenode(l,false,p1);
  536. consume(_RKLAMMER);
  537. statement_syssym:=p2;
  538. end;
  539. in_inc_x,
  540. in_dec_x :
  541. begin
  542. consume(_LKLAMMER);
  543. in_args:=true;
  544. p1:=comp_expr(true,false);
  545. if try_to_consume(_COMMA) then
  546. p2:=ccallparanode.create(comp_expr(true,false),nil)
  547. else
  548. p2:=nil;
  549. p2:=ccallparanode.create(p1,p2);
  550. statement_syssym:=geninlinenode(l,false,p2);
  551. consume(_RKLAMMER);
  552. end;
  553. in_slice_x:
  554. begin
  555. if not(in_args) then
  556. begin
  557. message(parser_e_illegal_slice);
  558. consume(_LKLAMMER);
  559. in_args:=true;
  560. comp_expr(true,false).free;
  561. if try_to_consume(_COMMA) then
  562. comp_expr(true,false).free;
  563. statement_syssym:=cerrornode.create;
  564. consume(_RKLAMMER);
  565. end
  566. else
  567. begin
  568. consume(_LKLAMMER);
  569. in_args:=true;
  570. p1:=comp_expr(true,false);
  571. Consume(_COMMA);
  572. if not(codegenerror) then
  573. p2:=ccallparanode.create(comp_expr(true,false),nil)
  574. else
  575. p2:=cerrornode.create;
  576. p2:=ccallparanode.create(p1,p2);
  577. statement_syssym:=geninlinenode(l,false,p2);
  578. consume(_RKLAMMER);
  579. end;
  580. end;
  581. in_initialize_x:
  582. begin
  583. statement_syssym:=inline_initialize;
  584. end;
  585. in_finalize_x:
  586. begin
  587. statement_syssym:=inline_finalize;
  588. end;
  589. in_copy_x:
  590. begin
  591. statement_syssym:=inline_copy;
  592. end;
  593. in_concat_x :
  594. begin
  595. consume(_LKLAMMER);
  596. in_args:=true;
  597. { Translate to x:=x+y[+z]. The addnode will do the
  598. type checking }
  599. p2:=nil;
  600. repeat
  601. p1:=comp_expr(true,false);
  602. if p2<>nil then
  603. p2:=caddnode.create(addn,p2,p1)
  604. else
  605. begin
  606. { Force string type if it isn't yet }
  607. if not(
  608. (p1.resultdef.typ=stringdef) or
  609. is_chararray(p1.resultdef) or
  610. is_char(p1.resultdef)
  611. ) then
  612. inserttypeconv(p1,cshortstringtype);
  613. p2:=p1;
  614. end;
  615. until not try_to_consume(_COMMA);
  616. consume(_RKLAMMER);
  617. statement_syssym:=p2;
  618. end;
  619. in_read_x,
  620. in_readln_x,
  621. in_readstr_x:
  622. begin
  623. if try_to_consume(_LKLAMMER) then
  624. begin
  625. paras:=parse_paras(false,false,_RKLAMMER);
  626. consume(_RKLAMMER);
  627. end
  628. else
  629. paras:=nil;
  630. p1:=geninlinenode(l,false,paras);
  631. statement_syssym := p1;
  632. end;
  633. in_setlength_x:
  634. begin
  635. statement_syssym := inline_setlength;
  636. end;
  637. in_objc_selector_x:
  638. begin
  639. if (m_objectivec1 in current_settings.modeswitches) then
  640. begin
  641. consume(_LKLAMMER);
  642. in_args:=true;
  643. { don't turn procsyms into calls (getaddr = true) }
  644. p1:=factor(true,false);
  645. p2:=geninlinenode(l,false,p1);
  646. consume(_RKLAMMER);
  647. statement_syssym:=p2;
  648. end
  649. else
  650. begin
  651. Message1(sym_e_id_not_found, orgpattern);
  652. statement_syssym:=cerrornode.create;
  653. end;
  654. end;
  655. in_length_x:
  656. begin
  657. consume(_LKLAMMER);
  658. in_args:=true;
  659. p1:=comp_expr(true,false);
  660. p2:=geninlinenode(l,false,p1);
  661. consume(_RKLAMMER);
  662. statement_syssym:=p2;
  663. end;
  664. in_write_x,
  665. in_writeln_x,
  666. in_writestr_x :
  667. begin
  668. if try_to_consume(_LKLAMMER) then
  669. begin
  670. paras:=parse_paras(true,false,_RKLAMMER);
  671. consume(_RKLAMMER);
  672. end
  673. else
  674. paras:=nil;
  675. p1 := geninlinenode(l,false,paras);
  676. statement_syssym := p1;
  677. end;
  678. in_str_x_string :
  679. begin
  680. consume(_LKLAMMER);
  681. paras:=parse_paras(true,false,_RKLAMMER);
  682. consume(_RKLAMMER);
  683. p1 := geninlinenode(l,false,paras);
  684. statement_syssym := p1;
  685. end;
  686. in_val_x:
  687. Begin
  688. consume(_LKLAMMER);
  689. in_args := true;
  690. p1:= ccallparanode.create(comp_expr(true,false), nil);
  691. consume(_COMMA);
  692. p2 := ccallparanode.create(comp_expr(true,false),p1);
  693. if try_to_consume(_COMMA) then
  694. p2 := ccallparanode.create(comp_expr(true,false),p2);
  695. consume(_RKLAMMER);
  696. p2 := geninlinenode(l,false,p2);
  697. statement_syssym := p2;
  698. End;
  699. in_include_x_y,
  700. in_exclude_x_y :
  701. begin
  702. consume(_LKLAMMER);
  703. in_args:=true;
  704. p1:=comp_expr(true,false);
  705. consume(_COMMA);
  706. p2:=comp_expr(true,false);
  707. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  708. consume(_RKLAMMER);
  709. end;
  710. in_pack_x_y_z,
  711. in_unpack_x_y_z :
  712. begin
  713. consume(_LKLAMMER);
  714. in_args:=true;
  715. p1:=comp_expr(true,false);
  716. consume(_COMMA);
  717. p2:=comp_expr(true,false);
  718. consume(_COMMA);
  719. paras:=comp_expr(true,false);
  720. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,ccallparanode.create(paras,nil))));
  721. consume(_RKLAMMER);
  722. end;
  723. in_assert_x_y :
  724. begin
  725. consume(_LKLAMMER);
  726. in_args:=true;
  727. p1:=comp_expr(true,false);
  728. if try_to_consume(_COMMA) then
  729. p2:=comp_expr(true,false)
  730. else
  731. begin
  732. { then insert an empty string }
  733. p2:=cstringconstnode.createstr('');
  734. end;
  735. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  736. consume(_RKLAMMER);
  737. end;
  738. in_get_frame:
  739. begin
  740. statement_syssym:=geninlinenode(l,false,nil);
  741. end;
  742. (*
  743. in_get_caller_frame:
  744. begin
  745. if try_to_consume(_LKLAMMER) then
  746. begin
  747. {You used to call get_caller_frame as get_caller_frame(get_frame),
  748. however, as a stack frame may not exist, it does more harm than
  749. good, so ignore it.}
  750. in_args:=true;
  751. p1:=comp_expr(true,false);
  752. p1.destroy;
  753. consume(_RKLAMMER);
  754. end;
  755. statement_syssym:=geninlinenode(l,false,nil);
  756. end;
  757. *)
  758. else
  759. internalerror(15);
  760. end;
  761. in_args:=prev_in_args;
  762. end;
  763. function maybe_load_methodpointer(st:TSymtable;var p1:tnode):boolean;
  764. begin
  765. maybe_load_methodpointer:=false;
  766. if not assigned(p1) then
  767. begin
  768. case st.symtabletype of
  769. withsymtable :
  770. begin
  771. if (st.defowner.typ=objectdef) then
  772. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  773. end;
  774. ObjectSymtable,
  775. recordsymtable:
  776. begin
  777. { We are calling from the static class method which has no self node }
  778. if assigned(current_procinfo) and current_procinfo.procdef.no_self_node then
  779. p1:=cloadvmtaddrnode.create(ctypenode.create(current_procinfo.procdef.struct))
  780. else
  781. p1:=load_self_node;
  782. { We are calling a member }
  783. maybe_load_methodpointer:=true;
  784. end;
  785. end;
  786. end;
  787. end;
  788. { reads the parameter for a subroutine call }
  789. procedure do_proc_call(sym:tsym;st:TSymtable;obj:tabstractrecorddef;getaddr:boolean;var again : boolean;var p1:tnode;callflags:tcallnodeflags);
  790. var
  791. membercall,
  792. prevafterassn : boolean;
  793. i : integer;
  794. para,p2 : tnode;
  795. currpara : tparavarsym;
  796. aprocdef : tprocdef;
  797. begin
  798. prevafterassn:=afterassignment;
  799. afterassignment:=false;
  800. membercall:=false;
  801. aprocdef:=nil;
  802. { when it is a call to a member we need to load the
  803. methodpointer first
  804. }
  805. membercall:=maybe_load_methodpointer(st,p1);
  806. { When we are expecting a procvar we also need
  807. to get the address in some cases }
  808. if assigned(getprocvardef) then
  809. begin
  810. if (block_type=bt_const) or
  811. getaddr then
  812. begin
  813. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  814. getaddr:=true;
  815. end
  816. else
  817. if (m_tp_procvar in current_settings.modeswitches) or
  818. (m_mac_procvar in current_settings.modeswitches) then
  819. begin
  820. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  821. if assigned(aprocdef) then
  822. getaddr:=true;
  823. end;
  824. end;
  825. { only need to get the address of the procedure? }
  826. if getaddr then
  827. begin
  828. { Retrieve info which procvar to call. For tp_procvar the
  829. aprocdef is already loaded above so we can reuse it }
  830. if not assigned(aprocdef) and
  831. assigned(getprocvardef) then
  832. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  833. { generate a methodcallnode or proccallnode }
  834. { we shouldn't convert things like @tcollection.load }
  835. p2:=cloadnode.create_procvar(sym,aprocdef,st);
  836. if assigned(p1) then
  837. begin
  838. { for loading methodpointer of an inherited function
  839. we use self as instance and load the address of
  840. the function directly and not through the vmt (PFV) }
  841. if (cnf_inherited in callflags) then
  842. begin
  843. include(p2.flags,nf_inherited);
  844. p1.free;
  845. p1:=load_self_node;
  846. end;
  847. if (p1.nodetype<>typen) then
  848. tloadnode(p2).set_mp(p1)
  849. else
  850. p1.free;
  851. end;
  852. p1:=p2;
  853. { no postfix operators }
  854. again:=false;
  855. end
  856. else
  857. begin
  858. para:=nil;
  859. if anon_inherited then
  860. begin
  861. if not assigned(current_procinfo) then
  862. internalerror(200305054);
  863. for i:=0 to current_procinfo.procdef.paras.count-1 do
  864. begin
  865. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  866. if not(vo_is_hidden_para in currpara.varoptions) then
  867. begin
  868. { inheritance by msgint? }
  869. if assigned(srdef) then
  870. { anonymous inherited via msgid calls only require a var parameter for
  871. both methods, so we need some type casting here }
  872. para:=ccallparanode.create(ctypeconvnode.create_internal(ctypeconvnode.create_internal(
  873. cloadnode.create(currpara,currpara.owner),cformaltype),tparavarsym(tprocdef(srdef).paras[i]).vardef),
  874. para)
  875. else
  876. para:=ccallparanode.create(cloadnode.create(currpara,currpara.owner),para);
  877. end;
  878. end;
  879. end
  880. else
  881. begin
  882. if try_to_consume(_LKLAMMER) then
  883. begin
  884. para:=parse_paras(false,false,_RKLAMMER);
  885. consume(_RKLAMMER);
  886. end;
  887. end;
  888. { indicate if this call was generated by a member and
  889. no explicit self is used, this is needed to determine
  890. how to handle a destructor call (PFV) }
  891. if membercall then
  892. include(callflags,cnf_member_call);
  893. if assigned(obj) then
  894. begin
  895. if not (st.symtabletype in [ObjectSymtable,recordsymtable]) then
  896. internalerror(200310031);
  897. p1:=ccallnode.create(para,tprocsym(sym),obj.symtable,p1,callflags);
  898. end
  899. else
  900. p1:=ccallnode.create(para,tprocsym(sym),st,p1,callflags);
  901. end;
  902. afterassignment:=prevafterassn;
  903. end;
  904. procedure handle_procvar(pv : tprocvardef;var p2 : tnode);
  905. var
  906. hp,hp2 : tnode;
  907. hpp : ^tnode;
  908. currprocdef : tprocdef;
  909. begin
  910. if not assigned(pv) then
  911. internalerror(200301121);
  912. if (m_tp_procvar in current_settings.modeswitches) or
  913. (m_mac_procvar in current_settings.modeswitches) then
  914. begin
  915. hp:=p2;
  916. hpp:=@p2;
  917. while assigned(hp) and
  918. (hp.nodetype=typeconvn) do
  919. begin
  920. hp:=ttypeconvnode(hp).left;
  921. { save orignal address of the old tree so we can replace the node }
  922. hpp:=@hp;
  923. end;
  924. if (hp.nodetype=calln) and
  925. { a procvar can't have parameters! }
  926. not assigned(tcallnode(hp).left) then
  927. begin
  928. currprocdef:=tcallnode(hp).symtableprocentry.Find_procdef_byprocvardef(pv);
  929. if assigned(currprocdef) then
  930. begin
  931. hp2:=cloadnode.create_procvar(tprocsym(tcallnode(hp).symtableprocentry),currprocdef,tcallnode(hp).symtableproc);
  932. if (po_methodpointer in pv.procoptions) then
  933. tloadnode(hp2).set_mp(tcallnode(hp).methodpointer.getcopy);
  934. hp.destroy;
  935. { replace the old callnode with the new loadnode }
  936. hpp^:=hp2;
  937. end;
  938. end;
  939. end;
  940. end;
  941. { checks whether sym is a static field and if so, translates the access
  942. to the appropriate node tree }
  943. function handle_staticfield_access(sym: tsym; nested: boolean; var p1: tnode): boolean;
  944. var
  945. static_name: shortstring;
  946. srsymtable: tsymtable;
  947. begin
  948. result:=false;
  949. { generate access code }
  950. if (sp_static in sym.symoptions) then
  951. begin
  952. result:=true;
  953. if not nested then
  954. static_name:=lower(sym.owner.name^)+'_'+sym.name
  955. else
  956. static_name:=lower(generate_nested_name(sym.owner,'_'))+'_'+sym.name;
  957. if sym.owner.defowner.typ=objectdef then
  958. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable)
  959. else
  960. searchsym_in_record(trecorddef(sym.owner.defowner),static_name,sym,srsymtable);
  961. if assigned(sym) then
  962. check_hints(sym,sym.symoptions,sym.deprecatedmsg);
  963. p1.free;
  964. p1:=nil;
  965. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  966. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  967. end;
  968. end;
  969. { the following procedure handles the access to a property symbol }
  970. procedure handle_propertysym(propsym : tpropertysym;st : TSymtable;var p1 : tnode);
  971. var
  972. paras : tnode;
  973. p2 : tnode;
  974. membercall : boolean;
  975. callflags : tcallnodeflags;
  976. propaccesslist : tpropaccesslist;
  977. sym: tsym;
  978. statements : tstatementnode;
  979. converted_result_data : ttempcreatenode;
  980. begin
  981. { property parameters? read them only if the property really }
  982. { has parameters }
  983. paras:=nil;
  984. if (ppo_hasparameters in propsym.propoptions) then
  985. begin
  986. if try_to_consume(_LECKKLAMMER) then
  987. begin
  988. paras:=parse_paras(false,false,_RECKKLAMMER);
  989. consume(_RECKKLAMMER);
  990. end;
  991. end;
  992. { indexed property }
  993. if (ppo_indexed in propsym.propoptions) then
  994. begin
  995. p2:=cordconstnode.create(propsym.index,propsym.indexdef,true);
  996. paras:=ccallparanode.create(p2,paras);
  997. end;
  998. { we need only a write property if a := follows }
  999. { if not(afterassignment) and not(in_args) then }
  1000. if token=_ASSIGNMENT then
  1001. begin
  1002. if getpropaccesslist(propsym,palt_write,propaccesslist) then
  1003. begin
  1004. sym:=propaccesslist.firstsym^.sym;
  1005. case sym.typ of
  1006. procsym :
  1007. begin
  1008. callflags:=[];
  1009. { generate the method call }
  1010. membercall:=maybe_load_methodpointer(st,p1);
  1011. if membercall then
  1012. include(callflags,cnf_member_call);
  1013. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags);
  1014. addsymref(sym);
  1015. paras:=nil;
  1016. consume(_ASSIGNMENT);
  1017. { read the expression }
  1018. if propsym.propdef.typ=procvardef then
  1019. getprocvardef:=tprocvardef(propsym.propdef);
  1020. p2:=comp_expr(true,false);
  1021. if assigned(getprocvardef) then
  1022. handle_procvar(getprocvardef,p2);
  1023. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  1024. { mark as property, both the tcallnode and the real call block }
  1025. include(p1.flags,nf_isproperty);
  1026. getprocvardef:=nil;
  1027. end;
  1028. fieldvarsym :
  1029. begin
  1030. { generate access code }
  1031. if not handle_staticfield_access(sym,false,p1) then
  1032. propaccesslist_to_node(p1,st,propaccesslist);
  1033. include(p1.flags,nf_isproperty);
  1034. consume(_ASSIGNMENT);
  1035. { read the expression }
  1036. p2:=comp_expr(true,false);
  1037. p1:=cassignmentnode.create(p1,p2);
  1038. end
  1039. else
  1040. begin
  1041. p1:=cerrornode.create;
  1042. Message(parser_e_no_procedure_to_access_property);
  1043. end;
  1044. end;
  1045. end
  1046. else
  1047. if (ppo_dispid_write in propsym.propoptions) then
  1048. begin
  1049. consume(_ASSIGNMENT);
  1050. p2:=comp_expr(true,false);
  1051. { concat value parameter too }
  1052. p2:=ccallparanode.create(p2,paras);
  1053. paras:=nil;
  1054. p1:=translate_disp_call(p1,p2,dct_propput,'',propsym.dispid,voidtype);
  1055. end
  1056. else
  1057. begin
  1058. p1:=cerrornode.create;
  1059. Message(parser_e_no_procedure_to_access_property);
  1060. end;
  1061. end
  1062. else
  1063. begin
  1064. if getpropaccesslist(propsym,palt_read,propaccesslist) then
  1065. begin
  1066. sym := propaccesslist.firstsym^.sym;
  1067. case sym.typ of
  1068. fieldvarsym :
  1069. begin
  1070. { generate access code }
  1071. if not handle_staticfield_access(sym,false,p1) then
  1072. propaccesslist_to_node(p1,st,propaccesslist);
  1073. include(p1.flags,nf_isproperty);
  1074. end;
  1075. procsym :
  1076. begin
  1077. callflags:=[];
  1078. { generate the method call }
  1079. membercall:=maybe_load_methodpointer(st,p1);
  1080. if membercall then
  1081. include(callflags,cnf_member_call);
  1082. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags);
  1083. paras:=nil;
  1084. include(p1.flags,nf_isproperty);
  1085. end
  1086. else
  1087. begin
  1088. p1:=cerrornode.create;
  1089. Message(type_e_mismatch);
  1090. end;
  1091. end;
  1092. end
  1093. else
  1094. if (ppo_dispid_read in propsym.propoptions) then
  1095. begin
  1096. p2:=internalstatements(statements);
  1097. converted_result_data:=ctempcreatenode.create(propsym.propdef,sizeof(propsym.propdef),tt_persistent,true);
  1098. addstatement(statements,converted_result_data);
  1099. addstatement(statements,cassignmentnode.create(ctemprefnode.create(converted_result_data),
  1100. ctypeconvnode.create_internal(translate_disp_call(p1,paras,dct_propget,'',propsym.dispid,propsym.propdef),
  1101. propsym.propdef)));
  1102. addstatement(statements,ctempdeletenode.create_normal_temp(converted_result_data));
  1103. addstatement(statements,ctemprefnode.create(converted_result_data));
  1104. p1:=p2;
  1105. paras:=nil;
  1106. end
  1107. else
  1108. begin
  1109. { error, no function to read property }
  1110. p1:=cerrornode.create;
  1111. Message(parser_e_no_procedure_to_access_property);
  1112. end;
  1113. end;
  1114. { release paras if not used }
  1115. if assigned(paras) then
  1116. paras.free;
  1117. end;
  1118. { the ID token has to be consumed before calling this function }
  1119. procedure do_member_read(structh:tabstractrecorddef;getaddr:boolean;sym:tsym;var p1:tnode;var again:boolean;callflags:tcallnodeflags);
  1120. var
  1121. isclassref : boolean;
  1122. begin
  1123. if sym=nil then
  1124. begin
  1125. { pattern is still valid unless
  1126. there is another ID just after the ID of sym }
  1127. Message1(sym_e_id_no_member,orgpattern);
  1128. p1.free;
  1129. p1:=cerrornode.create;
  1130. { try to clean up }
  1131. again:=false;
  1132. end
  1133. else
  1134. begin
  1135. if assigned(p1) then
  1136. begin
  1137. if not assigned(p1.resultdef) then
  1138. do_typecheckpass(p1);
  1139. isclassref:=(p1.resultdef.typ=classrefdef);
  1140. end
  1141. else
  1142. isclassref:=false;
  1143. { we assume, that only procsyms and varsyms are in an object }
  1144. { symbol table, for classes, properties are allowed }
  1145. case sym.typ of
  1146. procsym:
  1147. begin
  1148. do_proc_call(sym,sym.owner,structh,
  1149. (getaddr and not(token in [_CARET,_POINT])),
  1150. again,p1,callflags);
  1151. { we need to know which procedure is called }
  1152. do_typecheckpass(p1);
  1153. { calling using classref? }
  1154. if isclassref and
  1155. (p1.nodetype=calln) and
  1156. assigned(tcallnode(p1).procdefinition) and
  1157. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  1158. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  1159. Message(parser_e_only_class_members_via_class_ref);
  1160. end;
  1161. fieldvarsym:
  1162. begin
  1163. if not handle_staticfield_access(sym,true,p1) then
  1164. begin
  1165. if isclassref then
  1166. if assigned(p1) and
  1167. (
  1168. is_self_node(p1) or
  1169. (assigned(current_procinfo) and (current_procinfo.procdef.no_self_node) and
  1170. (current_procinfo.procdef.struct=structh))) then
  1171. Message(parser_e_only_class_members)
  1172. else
  1173. Message(parser_e_only_class_members_via_class_ref);
  1174. p1:=csubscriptnode.create(sym,p1);
  1175. end;
  1176. end;
  1177. propertysym:
  1178. begin
  1179. if isclassref and not (sp_static in sym.symoptions) then
  1180. Message(parser_e_only_class_members_via_class_ref);
  1181. handle_propertysym(tpropertysym(sym),sym.owner,p1);
  1182. end;
  1183. typesym:
  1184. begin
  1185. p1.free;
  1186. if try_to_consume(_LKLAMMER) then
  1187. begin
  1188. p1:=comp_expr(true,false);
  1189. consume(_RKLAMMER);
  1190. p1:=ctypeconvnode.create_explicit(p1,ttypesym(sym).typedef);
  1191. end
  1192. else
  1193. begin
  1194. p1:=ctypenode.create(ttypesym(sym).typedef);
  1195. if (is_class(ttypesym(sym).typedef) or is_objcclass(ttypesym(sym).typedef)) and
  1196. not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1197. p1:=cloadvmtaddrnode.create(p1);
  1198. end;
  1199. end;
  1200. constsym:
  1201. begin
  1202. p1.free;
  1203. p1:=genconstsymtree(tconstsym(sym));
  1204. end;
  1205. staticvarsym:
  1206. begin
  1207. { typed constant is a staticvarsym
  1208. now they are absolutevarsym }
  1209. p1.free;
  1210. p1:=cloadnode.create(sym,sym.Owner);
  1211. end;
  1212. absolutevarsym:
  1213. begin
  1214. p1.free;
  1215. p1:=nil;
  1216. { typed constants are absolutebarsyms now to handle storage properly }
  1217. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1218. end
  1219. else
  1220. internalerror(16);
  1221. end;
  1222. end;
  1223. end;
  1224. {****************************************************************************
  1225. Factor
  1226. ****************************************************************************}
  1227. function is_member_read(sym: tsym; st: tsymtable; var p1: tnode;
  1228. out memberparentdef: tdef): boolean;
  1229. var
  1230. hdef : tdef;
  1231. begin
  1232. result:=true;
  1233. memberparentdef:=nil;
  1234. case st.symtabletype of
  1235. ObjectSymtable,
  1236. recordsymtable:
  1237. begin
  1238. memberparentdef:=tdef(st.defowner);
  1239. exit;
  1240. end;
  1241. WithSymtable:
  1242. begin
  1243. if assigned(p1) then
  1244. internalerror(2007012002);
  1245. hdef:=tnode(twithsymtable(st).withrefnode).resultdef;
  1246. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  1247. if not(hdef.typ in [objectdef,classrefdef]) then
  1248. exit;
  1249. if (hdef.typ=classrefdef) then
  1250. hdef:=tclassrefdef(hdef).pointeddef;
  1251. memberparentdef:=hdef;
  1252. end;
  1253. else
  1254. result:=false;
  1255. end;
  1256. end;
  1257. {$maxfpuregisters 0}
  1258. function factor(getaddr,typeonly:boolean) : tnode;
  1259. {---------------------------------------------
  1260. Factor_read_id
  1261. ---------------------------------------------}
  1262. procedure factor_read_id(out p1:tnode;var again:boolean);
  1263. var
  1264. srsym : tsym;
  1265. srsymtable : TSymtable;
  1266. hdef : tdef;
  1267. orgstoredpattern,
  1268. storedpattern : string;
  1269. callflags: tcallnodeflags;
  1270. t : ttoken;
  1271. unit_found : boolean;
  1272. begin
  1273. { allow post fix operators }
  1274. again:=true;
  1275. { first check for identifier }
  1276. if token<>_ID then
  1277. begin
  1278. srsym:=generrorsym;
  1279. srsymtable:=nil;
  1280. consume(_ID);
  1281. end
  1282. else
  1283. begin
  1284. if typeonly then
  1285. searchsym_type(pattern,srsym,srsymtable)
  1286. else
  1287. searchsym(pattern,srsym,srsymtable);
  1288. { handle unit specification like System.Writeln }
  1289. unit_found:=try_consume_unitsym(srsym,srsymtable,t);
  1290. storedpattern:=pattern;
  1291. orgstoredpattern:=orgpattern;
  1292. consume(t);
  1293. { named parameter support }
  1294. found_arg_name:=false;
  1295. if not(unit_found) and
  1296. named_args_allowed and
  1297. (token=_ASSIGNMENT) then
  1298. begin
  1299. found_arg_name:=true;
  1300. p1:=cstringconstnode.createstr(storedpattern);
  1301. consume(_ASSIGNMENT);
  1302. exit;
  1303. end;
  1304. { if nothing found give error and return errorsym }
  1305. if assigned(srsym) then
  1306. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg)
  1307. else
  1308. begin
  1309. identifier_not_found(orgstoredpattern);
  1310. srsym:=generrorsym;
  1311. srsymtable:=nil;
  1312. end;
  1313. end;
  1314. { Access to funcret or need to call the function? }
  1315. if (srsym.typ in [absolutevarsym,localvarsym,paravarsym]) and
  1316. (vo_is_funcret in tabstractvarsym(srsym).varoptions) and
  1317. { result(x) is not allowed }
  1318. not(vo_is_result in tabstractvarsym(srsym).varoptions) and
  1319. (
  1320. (token=_LKLAMMER) or
  1321. (
  1322. (
  1323. (m_tp7 in current_settings.modeswitches) or
  1324. (m_delphi in current_settings.modeswitches)
  1325. ) and
  1326. (afterassignment or in_args)
  1327. )
  1328. ) then
  1329. begin
  1330. hdef:=tdef(srsym.owner.defowner);
  1331. if assigned(hdef) and
  1332. (hdef.typ=procdef) then
  1333. srsym:=tprocdef(hdef).procsym
  1334. else
  1335. begin
  1336. Message(parser_e_illegal_expression);
  1337. srsym:=generrorsym;
  1338. end;
  1339. srsymtable:=srsym.owner;
  1340. end;
  1341. begin
  1342. case srsym.typ of
  1343. absolutevarsym :
  1344. begin
  1345. if (tabsolutevarsym(srsym).abstyp=tovar) then
  1346. begin
  1347. p1:=nil;
  1348. propaccesslist_to_node(p1,nil,tabsolutevarsym(srsym).ref);
  1349. p1:=ctypeconvnode.create(p1,tabsolutevarsym(srsym).vardef);
  1350. include(p1.flags,nf_absolute);
  1351. end
  1352. else
  1353. p1:=cloadnode.create(srsym,srsymtable);
  1354. end;
  1355. staticvarsym,
  1356. localvarsym,
  1357. paravarsym,
  1358. fieldvarsym :
  1359. begin
  1360. { check if we are reading a field of an object/class/ }
  1361. { record. is_member_read() will deal with withsymtables }
  1362. { if needed. }
  1363. p1:=nil;
  1364. if is_member_read(srsym,srsymtable,p1,hdef) then
  1365. begin
  1366. { if the field was originally found in an }
  1367. { objectsymtable, it means it's part of self
  1368. if only method from which it was called is
  1369. not class static }
  1370. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) then
  1371. if assigned(current_procinfo) and current_procinfo.procdef.no_self_node then
  1372. p1:=cloadvmtaddrnode.create(ctypenode.create(current_procinfo.procdef.struct))
  1373. else
  1374. p1:=load_self_node;
  1375. { now, if the field itself is part of an objectsymtab }
  1376. { (it can be even if it was found in a withsymtable, }
  1377. { e.g., "with classinstance do field := 5"), then }
  1378. { let do_member_read handle it }
  1379. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  1380. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[])
  1381. else
  1382. { otherwise it's a regular record subscript }
  1383. p1:=csubscriptnode.create(srsym,p1);
  1384. end
  1385. else
  1386. { regular non-field load }
  1387. p1:=cloadnode.create(srsym,srsymtable);
  1388. end;
  1389. syssym :
  1390. begin
  1391. p1:=statement_syssym(tsyssym(srsym).number);
  1392. end;
  1393. typesym :
  1394. begin
  1395. hdef:=ttypesym(srsym).typedef;
  1396. if not assigned(hdef) then
  1397. begin
  1398. again:=false;
  1399. end
  1400. else
  1401. begin
  1402. { We need to know if this unit uses Variants }
  1403. if (hdef=cvarianttype) and
  1404. not(cs_compilesystem in current_settings.moduleswitches) then
  1405. current_module.flags:=current_module.flags or uf_uses_variants;
  1406. { if we get a generic then check that it is not an inline specialization }
  1407. if (df_generic in hdef.defoptions) and
  1408. (token=_LT) and
  1409. (m_delphi in current_settings.modeswitches) then
  1410. generate_specialization(hdef,false);
  1411. if try_to_consume(_LKLAMMER) then
  1412. begin
  1413. p1:=comp_expr(true,false);
  1414. consume(_RKLAMMER);
  1415. p1:=ctypeconvnode.create_explicit(p1,hdef);
  1416. end
  1417. else { not LKLAMMER }
  1418. if (token=_POINT) and
  1419. (is_object(hdef) or is_record(hdef)) then
  1420. begin
  1421. consume(_POINT);
  1422. { handles calling methods declared in parent objects
  1423. using "parentobject.methodname()" }
  1424. if assigned(current_structdef) and
  1425. not(getaddr) and
  1426. current_structdef.is_related(hdef) then
  1427. begin
  1428. p1:=ctypenode.create(hdef);
  1429. { search also in inherited methods }
  1430. searchsym_in_class(tobjectdef(hdef),tobjectdef(current_structdef),pattern,srsym,srsymtable);
  1431. if assigned(srsym) then
  1432. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1433. consume(_ID);
  1434. do_member_read(tobjectdef(hdef),false,srsym,p1,again,[]);
  1435. end
  1436. else
  1437. begin
  1438. { handles:
  1439. * @TObject.Load
  1440. * static methods and variables }
  1441. p1:=ctypenode.create(hdef);
  1442. { TP allows also @TMenu.Load if Load is only }
  1443. { defined in an anchestor class }
  1444. srsym:=search_struct_member(tabstractrecorddef(hdef),pattern);
  1445. if assigned(srsym) then
  1446. begin
  1447. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1448. consume(_ID);
  1449. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[]);
  1450. end
  1451. else
  1452. Message1(sym_e_id_no_member,orgpattern);
  1453. end;
  1454. end
  1455. else
  1456. begin
  1457. { class reference ? }
  1458. if is_class(hdef) or
  1459. is_objcclass(hdef) then
  1460. begin
  1461. if getaddr and (token=_POINT) then
  1462. begin
  1463. consume(_POINT);
  1464. { allows @Object.Method }
  1465. { also allows static methods and variables }
  1466. p1:=ctypenode.create(hdef);
  1467. { TP allows also @TMenu.Load if Load is only }
  1468. { defined in an anchestor class }
  1469. srsym:=search_struct_member(tobjectdef(hdef),pattern);
  1470. if assigned(srsym) then
  1471. begin
  1472. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1473. consume(_ID);
  1474. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[]);
  1475. end
  1476. else
  1477. begin
  1478. Message1(sym_e_id_no_member,orgpattern);
  1479. consume(_ID);
  1480. end;
  1481. end
  1482. else
  1483. begin
  1484. p1:=ctypenode.create(hdef);
  1485. { For a type block we simply return only
  1486. the type. For all other blocks we return
  1487. a loadvmt node }
  1488. if not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1489. p1:=cloadvmtaddrnode.create(p1);
  1490. end;
  1491. end
  1492. else
  1493. p1:=ctypenode.create(hdef);
  1494. end;
  1495. end;
  1496. end;
  1497. enumsym :
  1498. begin
  1499. p1:=genenumnode(tenumsym(srsym));
  1500. end;
  1501. constsym :
  1502. begin
  1503. if tconstsym(srsym).consttyp=constresourcestring then
  1504. begin
  1505. p1:=cloadnode.create(srsym,srsymtable);
  1506. do_typecheckpass(p1);
  1507. p1.resultdef:=cansistringtype;
  1508. end
  1509. else
  1510. p1:=genconstsymtree(tconstsym(srsym));
  1511. end;
  1512. procsym :
  1513. begin
  1514. p1:=nil;
  1515. { check if it's a method/class method }
  1516. if is_member_read(srsym,srsymtable,p1,hdef) then
  1517. begin
  1518. { not srsymtable.symtabletype since that can be }
  1519. { withsymtable as well }
  1520. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  1521. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[])
  1522. else
  1523. { no procsyms in records (yet) }
  1524. internalerror(2007012006);
  1525. end
  1526. else
  1527. begin
  1528. { regular procedure/function call }
  1529. if not unit_found then
  1530. callflags:=[]
  1531. else
  1532. callflags:=[cnf_unit_specified];
  1533. do_proc_call(srsym,srsymtable,nil,
  1534. (getaddr and not(token in [_CARET,_POINT,_LECKKLAMMER])),
  1535. again,p1,callflags);
  1536. end;
  1537. end;
  1538. propertysym :
  1539. begin
  1540. p1:=nil;
  1541. { property of a class/object? }
  1542. if is_member_read(srsym,srsymtable,p1,hdef) then
  1543. begin
  1544. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) then
  1545. if assigned(current_procinfo) and current_procinfo.procdef.no_self_node then
  1546. { no self node in static class methods }
  1547. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef))
  1548. else
  1549. p1:=load_self_node;
  1550. { not srsymtable.symtabletype since that can be }
  1551. { withsymtable as well }
  1552. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  1553. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[])
  1554. else
  1555. { no propertysyms in records (yet) }
  1556. internalerror(2009111510);
  1557. end
  1558. else
  1559. { no method pointer }
  1560. begin
  1561. handle_propertysym(tpropertysym(srsym),srsymtable,p1);
  1562. end;
  1563. end;
  1564. labelsym :
  1565. begin
  1566. { Support @label }
  1567. if getaddr then
  1568. begin
  1569. if srsym.owner<>current_procinfo.procdef.localst then
  1570. CGMessage(parser_e_label_outside_proc);
  1571. p1:=cloadnode.create(srsym,srsym.owner)
  1572. end
  1573. else
  1574. begin
  1575. consume(_COLON);
  1576. if tlabelsym(srsym).defined then
  1577. Message(sym_e_label_already_defined);
  1578. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  1579. begin
  1580. tlabelsym(srsym).nonlocal:=true;
  1581. exclude(current_procinfo.procdef.procoptions,po_inline);
  1582. end;
  1583. if tlabelsym(srsym).nonlocal and
  1584. (current_procinfo.procdef.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  1585. Message(sym_e_interprocgoto_into_init_final_code_not_allowed);
  1586. tlabelsym(srsym).defined:=true;
  1587. p1:=clabelnode.create(nil,tlabelsym(srsym));
  1588. tlabelsym(srsym).code:=p1;
  1589. end;
  1590. end;
  1591. errorsym :
  1592. begin
  1593. p1:=cerrornode.create;
  1594. if try_to_consume(_LKLAMMER) then
  1595. begin
  1596. parse_paras(false,false,_RKLAMMER);
  1597. consume(_RKLAMMER);
  1598. end;
  1599. end;
  1600. else
  1601. begin
  1602. p1:=cerrornode.create;
  1603. Message(parser_e_illegal_expression);
  1604. end;
  1605. end; { end case }
  1606. end;
  1607. end;
  1608. {---------------------------------------------
  1609. Factor_Read_Set
  1610. ---------------------------------------------}
  1611. { Read a set between [] }
  1612. function factor_read_set:tnode;
  1613. var
  1614. p1,p2 : tnode;
  1615. lastp,
  1616. buildp : tarrayconstructornode;
  1617. old_allow_array_constructor : boolean;
  1618. begin
  1619. buildp:=nil;
  1620. { be sure that a least one arrayconstructn is used, also for an
  1621. empty [] }
  1622. if token=_RECKKLAMMER then
  1623. buildp:=carrayconstructornode.create(nil,buildp)
  1624. else
  1625. repeat
  1626. { nested array constructors are not allowed, see also tests/webtbs/tw17213.pp }
  1627. old_allow_array_constructor:=allow_array_constructor;
  1628. allow_array_constructor:=false;
  1629. p1:=comp_expr(true,false);
  1630. if try_to_consume(_POINTPOINT) then
  1631. begin
  1632. p2:=comp_expr(true,false);
  1633. p1:=carrayconstructorrangenode.create(p1,p2);
  1634. end;
  1635. { insert at the end of the tree, to get the correct order }
  1636. if not assigned(buildp) then
  1637. begin
  1638. buildp:=carrayconstructornode.create(p1,nil);
  1639. lastp:=buildp;
  1640. end
  1641. else
  1642. begin
  1643. lastp.right:=carrayconstructornode.create(p1,nil);
  1644. lastp:=tarrayconstructornode(lastp.right);
  1645. end;
  1646. allow_array_constructor:=old_allow_array_constructor;
  1647. { there could be more elements }
  1648. until not try_to_consume(_COMMA);
  1649. factor_read_set:=buildp;
  1650. end;
  1651. {---------------------------------------------
  1652. PostFixOperators
  1653. ---------------------------------------------}
  1654. { returns whether or not p1 has been changed }
  1655. function postfixoperators(var p1:tnode;var again:boolean): boolean;
  1656. { tries to avoid syntax errors after invalid qualifiers }
  1657. procedure recoverconsume_postfixops;
  1658. begin
  1659. repeat
  1660. if not try_to_consume(_CARET) then
  1661. if try_to_consume(_POINT) then
  1662. try_to_consume(_ID)
  1663. else if try_to_consume(_LECKKLAMMER) then
  1664. begin
  1665. repeat
  1666. comp_expr(true,false);
  1667. until not try_to_consume(_COMMA);
  1668. consume(_RECKKLAMMER);
  1669. end
  1670. else if try_to_consume(_LKLAMMER) then
  1671. begin
  1672. repeat
  1673. comp_expr(true,false);
  1674. until not try_to_consume(_COMMA);
  1675. consume(_RKLAMMER);
  1676. end
  1677. else
  1678. break;
  1679. until false;
  1680. end;
  1681. procedure handle_variantarray;
  1682. var
  1683. p4 : tnode;
  1684. newstatement : tstatementnode;
  1685. tempresultvariant,
  1686. temp : ttempcreatenode;
  1687. paras : tcallparanode;
  1688. newblock : tnode;
  1689. countindices : aint;
  1690. begin
  1691. { create statements with call initialize the arguments and
  1692. call fpc_dynarr_setlength }
  1693. newblock:=internalstatements(newstatement);
  1694. { get temp for array of indicies,
  1695. we set the real size later }
  1696. temp:=ctempcreatenode.create(s32inttype,4,tt_persistent,false);
  1697. addstatement(newstatement,temp);
  1698. countindices:=0;
  1699. repeat
  1700. p4:=comp_expr(true,false);
  1701. addstatement(newstatement,cassignmentnode.create(
  1702. ctemprefnode.create_offset(temp,countindices*s32inttype.size),p4));
  1703. inc(countindices);
  1704. until not try_to_consume(_COMMA);
  1705. { set real size }
  1706. temp.size:=countindices*s32inttype.size;
  1707. consume(_RECKKLAMMER);
  1708. { we need only a write access if a := follows }
  1709. if token=_ASSIGNMENT then
  1710. begin
  1711. consume(_ASSIGNMENT);
  1712. p4:=comp_expr(true,false);
  1713. { create call to fpc_vararray_put }
  1714. paras:=ccallparanode.create(cordconstnode.create
  1715. (countindices,s32inttype,true),
  1716. ccallparanode.create(caddrnode.create_internal
  1717. (ctemprefnode.create(temp)),
  1718. ccallparanode.create(ctypeconvnode.create_internal(p4,cvarianttype),
  1719. ccallparanode.create(ctypeconvnode.create_internal(p1,cvarianttype)
  1720. ,nil))));
  1721. addstatement(newstatement,ccallnode.createintern('fpc_vararray_put',paras));
  1722. addstatement(newstatement,ctempdeletenode.create(temp));
  1723. end
  1724. else
  1725. begin
  1726. { create temp for result }
  1727. tempresultvariant:=ctempcreatenode.create(cvarianttype,cvarianttype.size,tt_persistent,true);
  1728. addstatement(newstatement,tempresultvariant);
  1729. { create call to fpc_vararray_get }
  1730. paras:=ccallparanode.create(cordconstnode.create
  1731. (countindices,s32inttype,true),
  1732. ccallparanode.create(caddrnode.create_internal
  1733. (ctemprefnode.create(temp)),
  1734. ccallparanode.create(p1,
  1735. ccallparanode.create(
  1736. ctemprefnode.create(tempresultvariant)
  1737. ,nil))));
  1738. addstatement(newstatement,ccallnode.createintern('fpc_vararray_get',paras));
  1739. addstatement(newstatement,ctempdeletenode.create(temp));
  1740. { the last statement should return the value as
  1741. location and type, this is done be referencing the
  1742. temp and converting it first from a persistent temp to
  1743. normal temp }
  1744. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempresultvariant));
  1745. addstatement(newstatement,ctemprefnode.create(tempresultvariant));
  1746. end;
  1747. p1:=newblock;
  1748. end;
  1749. var
  1750. protsym : tpropertysym;
  1751. p2,p3 : tnode;
  1752. srsym : tsym;
  1753. srsymtable : TSymtable;
  1754. structh : tabstractrecorddef;
  1755. { shouldn't be used that often, so the extra overhead is ok to save
  1756. stack space }
  1757. dispatchstring : ansistring;
  1758. nodechanged : boolean;
  1759. label
  1760. skipreckklammercheck;
  1761. begin
  1762. result:=false;
  1763. again:=true;
  1764. while again do
  1765. begin
  1766. { we need the resultdef }
  1767. do_typecheckpass_changed(p1,nodechanged);
  1768. result:=result or nodechanged;
  1769. if codegenerror then
  1770. begin
  1771. recoverconsume_postfixops;
  1772. exit;
  1773. end;
  1774. { handle token }
  1775. case token of
  1776. _CARET:
  1777. begin
  1778. consume(_CARET);
  1779. { support tp/mac procvar^ if the procvar returns a
  1780. pointer type }
  1781. if ((m_tp_procvar in current_settings.modeswitches) or
  1782. (m_mac_procvar in current_settings.modeswitches)) and
  1783. (p1.resultdef.typ=procvardef) and
  1784. (tprocvardef(p1.resultdef).returndef.typ=pointerdef) then
  1785. begin
  1786. p1:=ccallnode.create_procvar(nil,p1);
  1787. typecheckpass(p1);
  1788. end;
  1789. if (p1.resultdef.typ<>pointerdef) then
  1790. begin
  1791. { ^ as binary operator is a problem!!!! (FK) }
  1792. again:=false;
  1793. Message(parser_e_invalid_qualifier);
  1794. recoverconsume_postfixops;
  1795. p1.destroy;
  1796. p1:=cerrornode.create;
  1797. end
  1798. else
  1799. p1:=cderefnode.create(p1);
  1800. end;
  1801. _LECKKLAMMER:
  1802. begin
  1803. if is_class_or_interface_or_object(p1.resultdef) then
  1804. begin
  1805. { default property }
  1806. protsym:=search_default_property(tobjectdef(p1.resultdef));
  1807. if not(assigned(protsym)) then
  1808. begin
  1809. p1.destroy;
  1810. p1:=cerrornode.create;
  1811. again:=false;
  1812. message(parser_e_no_default_property_available);
  1813. end
  1814. else
  1815. begin
  1816. { The property symbol is referenced indirect }
  1817. protsym.IncRefCount;
  1818. handle_propertysym(protsym,protsym.owner,p1);
  1819. end;
  1820. end
  1821. else
  1822. begin
  1823. consume(_LECKKLAMMER);
  1824. repeat
  1825. { in all of the cases below, p1 is changed }
  1826. case p1.resultdef.typ of
  1827. pointerdef:
  1828. begin
  1829. { support delphi autoderef }
  1830. if (tpointerdef(p1.resultdef).pointeddef.typ=arraydef) and
  1831. (m_autoderef in current_settings.modeswitches) then
  1832. p1:=cderefnode.create(p1);
  1833. p2:=comp_expr(true,false);
  1834. { Support Pbytevar[0..9] which returns array [0..9].}
  1835. if try_to_consume(_POINTPOINT) then
  1836. p2:=crangenode.create(p2,comp_expr(true,false));
  1837. p1:=cvecnode.create(p1,p2);
  1838. end;
  1839. variantdef:
  1840. begin
  1841. handle_variantarray;
  1842. { the RECKKLAMMER is already read }
  1843. goto skipreckklammercheck;
  1844. end;
  1845. stringdef :
  1846. begin
  1847. p2:=comp_expr(true,false);
  1848. { Support string[0..9] which returns array [0..9] of char.}
  1849. if try_to_consume(_POINTPOINT) then
  1850. p2:=crangenode.create(p2,comp_expr(true,false));
  1851. p1:=cvecnode.create(p1,p2);
  1852. end;
  1853. arraydef:
  1854. begin
  1855. p2:=comp_expr(true,false);
  1856. { support SEG:OFS for go32v2 Mem[] }
  1857. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  1858. (p1.nodetype=loadn) and
  1859. assigned(tloadnode(p1).symtableentry) and
  1860. assigned(tloadnode(p1).symtableentry.owner.name) and
  1861. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1862. ((tloadnode(p1).symtableentry.name='MEM') or
  1863. (tloadnode(p1).symtableentry.name='MEMW') or
  1864. (tloadnode(p1).symtableentry.name='MEML')) then
  1865. begin
  1866. if try_to_consume(_COLON) then
  1867. begin
  1868. p3:=caddnode.create(muln,cordconstnode.create($10,s32inttype,false),p2);
  1869. p2:=comp_expr(true,false);
  1870. p2:=caddnode.create(addn,p2,p3);
  1871. if try_to_consume(_POINTPOINT) then
  1872. { Support mem[$a000:$0000..$07ff] which returns array [0..$7ff] of memtype.}
  1873. p2:=crangenode.create(p2,caddnode.create(addn,comp_expr(true,false),p3.getcopy));
  1874. p1:=cvecnode.create(p1,p2);
  1875. include(tvecnode(p1).flags,nf_memseg);
  1876. include(tvecnode(p1).flags,nf_memindex);
  1877. end
  1878. else
  1879. begin
  1880. if try_to_consume(_POINTPOINT) then
  1881. { Support mem[$80000000..$80000002] which returns array [0..2] of memtype.}
  1882. p2:=crangenode.create(p2,comp_expr(true,false));
  1883. p1:=cvecnode.create(p1,p2);
  1884. include(tvecnode(p1).flags,nf_memindex);
  1885. end;
  1886. end
  1887. else
  1888. begin
  1889. if try_to_consume(_POINTPOINT) then
  1890. { Support arrayvar[0..9] which returns array [0..9] of arraytype.}
  1891. p2:=crangenode.create(p2,comp_expr(true,false));
  1892. p1:=cvecnode.create(p1,p2);
  1893. end;
  1894. end;
  1895. else
  1896. begin
  1897. if p1.resultdef.typ<>undefineddef then
  1898. Message(parser_e_invalid_qualifier);
  1899. p1.destroy;
  1900. p1:=cerrornode.create;
  1901. comp_expr(true,false);
  1902. again:=false;
  1903. end;
  1904. end;
  1905. do_typecheckpass(p1);
  1906. until not try_to_consume(_COMMA);
  1907. consume(_RECKKLAMMER);
  1908. { handle_variantarray eats the RECKKLAMMER and jumps here }
  1909. skipreckklammercheck:
  1910. end;
  1911. end;
  1912. _POINT :
  1913. begin
  1914. consume(_POINT);
  1915. if (p1.resultdef.typ=pointerdef) and
  1916. (m_autoderef in current_settings.modeswitches) and
  1917. { don't auto-deref objc.id, because then the code
  1918. below for supporting id.anyobjcmethod isn't triggered }
  1919. (p1.resultdef<>objc_idtype) then
  1920. begin
  1921. p1:=cderefnode.create(p1);
  1922. do_typecheckpass(p1);
  1923. end;
  1924. { procvar.<something> can never mean anything so always
  1925. try to call it in case it returns a record/object/... }
  1926. maybe_call_procvar(p1,false);
  1927. case p1.resultdef.typ of
  1928. recorddef:
  1929. begin
  1930. if token=_ID then
  1931. begin
  1932. structh:=tabstractrecorddef(p1.resultdef);
  1933. searchsym_in_record(structh,pattern,srsym,srsymtable);
  1934. if assigned(srsym) then
  1935. begin
  1936. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1937. consume(_ID);
  1938. do_member_read(structh,getaddr,srsym,p1,again,[]);
  1939. end
  1940. else
  1941. begin
  1942. Message1(sym_e_id_no_member,orgpattern);
  1943. p1.destroy;
  1944. p1:=cerrornode.create;
  1945. { try to clean up }
  1946. consume(_ID);
  1947. end;
  1948. end
  1949. else
  1950. consume(_ID);
  1951. end;
  1952. enumdef:
  1953. begin
  1954. if token=_ID then
  1955. begin
  1956. srsym:=tsym(tenumdef(p1.resultdef).symtable.Find(pattern));
  1957. p1.destroy;
  1958. if assigned(srsym) and (srsym.typ=enumsym) then
  1959. begin
  1960. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1961. p1:=genenumnode(tenumsym(srsym));
  1962. end
  1963. else
  1964. begin
  1965. Message1(sym_e_id_no_member,orgpattern);
  1966. p1:=cerrornode.create;
  1967. end;
  1968. end;
  1969. consume(_ID);
  1970. end;
  1971. variantdef:
  1972. begin
  1973. { dispatch call? }
  1974. if token=_ID then
  1975. begin
  1976. dispatchstring:=orgpattern;
  1977. consume(_ID);
  1978. if try_to_consume(_LKLAMMER) then
  1979. begin
  1980. p2:=parse_paras(false,true,_RKLAMMER);
  1981. consume(_RKLAMMER);
  1982. end
  1983. else
  1984. p2:=nil;
  1985. { property setter? }
  1986. if (token=_ASSIGNMENT) and not(afterassignment) then
  1987. begin
  1988. consume(_ASSIGNMENT);
  1989. { read the expression }
  1990. p3:=comp_expr(true,false);
  1991. { concat value parameter too }
  1992. p2:=ccallparanode.create(p3,p2);
  1993. p1:=translate_disp_call(p1,p2,dct_propput,dispatchstring,0,voidtype);
  1994. end
  1995. else
  1996. { this is only an approximation
  1997. setting useresult if not necessary is only a waste of time, no more, no less (FK) }
  1998. if afterassignment or in_args or (token<>_SEMICOLON) then
  1999. p1:=translate_disp_call(p1,p2,dct_method,dispatchstring,0,cvarianttype)
  2000. else
  2001. p1:=translate_disp_call(p1,p2,dct_method,dispatchstring,0,voidtype);
  2002. end
  2003. else { Error }
  2004. Consume(_ID);
  2005. end;
  2006. classrefdef:
  2007. begin
  2008. if token=_ID then
  2009. begin
  2010. structh:=tobjectdef(tclassrefdef(p1.resultdef).pointeddef);
  2011. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable);
  2012. if assigned(srsym) then
  2013. begin
  2014. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2015. consume(_ID);
  2016. do_member_read(structh,getaddr,srsym,p1,again,[]);
  2017. end
  2018. else
  2019. begin
  2020. Message1(sym_e_id_no_member,orgpattern);
  2021. p1.destroy;
  2022. p1:=cerrornode.create;
  2023. { try to clean up }
  2024. consume(_ID);
  2025. end;
  2026. end
  2027. else { Error }
  2028. Consume(_ID);
  2029. end;
  2030. objectdef:
  2031. begin
  2032. if token=_ID then
  2033. begin
  2034. structh:=tobjectdef(p1.resultdef);
  2035. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable);
  2036. if assigned(srsym) then
  2037. begin
  2038. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2039. consume(_ID);
  2040. do_member_read(structh,getaddr,srsym,p1,again,[]);
  2041. end
  2042. else
  2043. begin
  2044. Message1(sym_e_id_no_member,orgpattern);
  2045. p1.destroy;
  2046. p1:=cerrornode.create;
  2047. { try to clean up }
  2048. consume(_ID);
  2049. end;
  2050. end
  2051. else { Error }
  2052. Consume(_ID);
  2053. end;
  2054. pointerdef:
  2055. begin
  2056. if (p1.resultdef=objc_idtype) then
  2057. begin
  2058. { objc's id type can be used to call any
  2059. Objective-C method of any Objective-C class
  2060. type that's currently in scope }
  2061. if search_objc_method(pattern,srsym,srsymtable) then
  2062. begin
  2063. consume(_ID);
  2064. do_proc_call(srsym,srsymtable,nil,
  2065. (getaddr and not(token in [_CARET,_POINT])),
  2066. again,p1,[cnf_objc_id_call]);
  2067. { we need to know which procedure is called }
  2068. do_typecheckpass(p1);
  2069. end
  2070. else
  2071. begin
  2072. consume(_ID);
  2073. Message(parser_e_methode_id_expected);
  2074. end;
  2075. end
  2076. else
  2077. begin
  2078. Message(parser_e_invalid_qualifier);
  2079. if tpointerdef(p1.resultdef).pointeddef.typ in [recorddef,objectdef,classrefdef] then
  2080. Message(parser_h_maybe_deref_caret_missing);
  2081. end
  2082. end;
  2083. else
  2084. begin
  2085. if p1.resultdef.typ<>undefineddef then
  2086. Message(parser_e_invalid_qualifier);
  2087. p1.destroy;
  2088. p1:=cerrornode.create;
  2089. { Error }
  2090. consume(_ID);
  2091. end;
  2092. end;
  2093. end;
  2094. else
  2095. begin
  2096. { is this a procedure variable ? }
  2097. if assigned(p1.resultdef) and
  2098. (p1.resultdef.typ=procvardef) then
  2099. begin
  2100. { Typenode for typecasting or expecting a procvar }
  2101. if (p1.nodetype=typen) or
  2102. (
  2103. assigned(getprocvardef) and
  2104. equal_defs(p1.resultdef,getprocvardef)
  2105. ) then
  2106. begin
  2107. if try_to_consume(_LKLAMMER) then
  2108. begin
  2109. p1:=comp_expr(true,false);
  2110. consume(_RKLAMMER);
  2111. p1:=ctypeconvnode.create_explicit(p1,p1.resultdef);
  2112. end
  2113. else
  2114. again:=false
  2115. end
  2116. else
  2117. begin
  2118. if try_to_consume(_LKLAMMER) then
  2119. begin
  2120. p2:=parse_paras(false,false,_RKLAMMER);
  2121. consume(_RKLAMMER);
  2122. p1:=ccallnode.create_procvar(p2,p1);
  2123. { proc():= is never possible }
  2124. if token=_ASSIGNMENT then
  2125. begin
  2126. Message(parser_e_illegal_expression);
  2127. p1.free;
  2128. p1:=cerrornode.create;
  2129. again:=false;
  2130. end;
  2131. end
  2132. else
  2133. again:=false;
  2134. end;
  2135. end
  2136. else
  2137. again:=false;
  2138. end;
  2139. end;
  2140. { we only try again if p1 was changed }
  2141. if again or
  2142. (p1.nodetype=errorn) then
  2143. result:=true;
  2144. end; { while again }
  2145. end;
  2146. {---------------------------------------------
  2147. Factor (Main)
  2148. ---------------------------------------------}
  2149. var
  2150. l : longint;
  2151. ic : int64;
  2152. qc : qword;
  2153. p1 : tnode;
  2154. code : integer;
  2155. srsym : tsym;
  2156. srsymtable : TSymtable;
  2157. pd : tprocdef;
  2158. hclassdef : tobjectdef;
  2159. d : bestreal;
  2160. cur : currency;
  2161. hs,hsorg : string;
  2162. hdef : tdef;
  2163. filepos : tfileposinfo;
  2164. again,
  2165. updatefpos,
  2166. nodechanged : boolean;
  2167. begin
  2168. { can't keep a copy of p1 and compare pointers afterwards, because
  2169. p1 may be freed and reallocated in the same place! }
  2170. updatefpos:=false;
  2171. p1:=nil;
  2172. filepos:=current_tokenpos;
  2173. again:=false;
  2174. if token=_ID then
  2175. begin
  2176. again:=true;
  2177. { Handle references to self }
  2178. if (idtoken=_SELF) and
  2179. not(block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) and
  2180. assigned(current_structdef) then
  2181. begin
  2182. p1:=load_self_node;
  2183. consume(_ID);
  2184. again:=true;
  2185. end
  2186. else
  2187. factor_read_id(p1,again);
  2188. if assigned(p1) then
  2189. begin
  2190. { factor_read_id will set the filepos to after the id,
  2191. and in case of _SELF the filepos will already be the
  2192. same as filepos (so setting it again doesn't hurt). }
  2193. p1.fileinfo:=filepos;
  2194. filepos:=current_tokenpos;
  2195. end;
  2196. { handle post fix operators }
  2197. updatefpos:=postfixoperators(p1,again);
  2198. end
  2199. else
  2200. begin
  2201. updatefpos:=true;
  2202. case token of
  2203. _RETURN :
  2204. begin
  2205. consume(_RETURN);
  2206. if not(token in [_SEMICOLON,_ELSE,_END]) then
  2207. p1 := cexitnode.create(comp_expr(true,false))
  2208. else
  2209. p1 := cexitnode.create(nil);
  2210. end;
  2211. _INHERITED :
  2212. begin
  2213. again:=true;
  2214. consume(_INHERITED);
  2215. if assigned(current_procinfo) and
  2216. assigned(current_structdef) and
  2217. (current_structdef.typ=objectdef) then
  2218. begin
  2219. hclassdef:=tobjectdef(current_structdef).childof;
  2220. { Objective-C categories *replace* methods in the class
  2221. they extend, or add methods to it. So calling an
  2222. inherited method always calls the method inherited from
  2223. the parent of the extended class }
  2224. if is_objccategory(current_structdef) then
  2225. hclassdef:=hclassdef.childof;
  2226. { if inherited; only then we need the method with
  2227. the same name }
  2228. if token <> _ID then
  2229. begin
  2230. hs:=current_procinfo.procdef.procsym.name;
  2231. hsorg:=current_procinfo.procdef.procsym.realname;
  2232. anon_inherited:=true;
  2233. { For message methods we need to search using the message
  2234. number or string }
  2235. pd:=tprocdef(tprocsym(current_procinfo.procdef.procsym).ProcdefList[0]);
  2236. srdef:=nil;
  2237. if (po_msgint in pd.procoptions) then
  2238. searchsym_in_class_by_msgint(hclassdef,pd.messageinf.i,srdef,srsym,srsymtable)
  2239. else
  2240. if (po_msgstr in pd.procoptions) then
  2241. searchsym_in_class_by_msgstr(hclassdef,pd.messageinf.str^,srsym,srsymtable)
  2242. else
  2243. searchsym_in_class(hclassdef,tobjectdef(current_structdef),hs,srsym,srsymtable);
  2244. end
  2245. else
  2246. begin
  2247. hs:=pattern;
  2248. hsorg:=orgpattern;
  2249. consume(_ID);
  2250. anon_inherited:=false;
  2251. searchsym_in_class(hclassdef,tobjectdef(current_structdef),hs,srsym,srsymtable);
  2252. end;
  2253. if assigned(srsym) then
  2254. begin
  2255. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2256. { load the procdef from the inherited class and
  2257. not from self }
  2258. case srsym.typ of
  2259. procsym:
  2260. begin
  2261. hdef:=hclassdef;
  2262. if (po_classmethod in current_procinfo.procdef.procoptions) or
  2263. (po_staticmethod in current_procinfo.procdef.procoptions) then
  2264. hdef:=tclassrefdef.create(hdef);
  2265. p1:=ctypenode.create(hdef);
  2266. end;
  2267. propertysym:
  2268. ;
  2269. else
  2270. begin
  2271. Message(parser_e_methode_id_expected);
  2272. p1:=cerrornode.create;
  2273. end;
  2274. end;
  2275. do_member_read(hclassdef,getaddr,srsym,p1,again,[cnf_inherited,cnf_anon_inherited]);
  2276. end
  2277. else
  2278. begin
  2279. if anon_inherited then
  2280. begin
  2281. { For message methods we need to call DefaultHandler }
  2282. if (po_msgint in pd.procoptions) or
  2283. (po_msgstr in pd.procoptions) then
  2284. begin
  2285. searchsym_in_class(hclassdef,hclassdef,'DEFAULTHANDLER',srsym,srsymtable);
  2286. if not assigned(srsym) or
  2287. (srsym.typ<>procsym) then
  2288. internalerror(200303171);
  2289. p1:=nil;
  2290. do_proc_call(srsym,srsym.owner,hclassdef,false,again,p1,[]);
  2291. end
  2292. else
  2293. begin
  2294. { we need to ignore the inherited; }
  2295. p1:=cnothingnode.create;
  2296. end;
  2297. end
  2298. else
  2299. begin
  2300. Message1(sym_e_id_no_member,hsorg);
  2301. p1:=cerrornode.create;
  2302. end;
  2303. again:=false;
  2304. end;
  2305. { turn auto inheriting off }
  2306. anon_inherited:=false;
  2307. end
  2308. else
  2309. begin
  2310. Message(parser_e_generic_methods_only_in_methods);
  2311. again:=false;
  2312. p1:=cerrornode.create;
  2313. end;
  2314. postfixoperators(p1,again);
  2315. end;
  2316. _INTCONST :
  2317. begin
  2318. {Try first wether the value fits in an int64.}
  2319. val(pattern,ic,code);
  2320. if code=0 then
  2321. begin
  2322. consume(_INTCONST);
  2323. int_to_type(ic,hdef);
  2324. p1:=cordconstnode.create(ic,hdef,true);
  2325. end
  2326. else
  2327. begin
  2328. { try qword next }
  2329. val(pattern,qc,code);
  2330. if code=0 then
  2331. begin
  2332. consume(_INTCONST);
  2333. int_to_type(qc,hdef);
  2334. p1:=cordconstnode.create(qc,hdef,true);
  2335. end;
  2336. end;
  2337. if code<>0 then
  2338. begin
  2339. { finally float }
  2340. val(pattern,d,code);
  2341. if code<>0 then
  2342. begin
  2343. Message(parser_e_invalid_integer);
  2344. consume(_INTCONST);
  2345. l:=1;
  2346. p1:=cordconstnode.create(l,sinttype,true);
  2347. end
  2348. else
  2349. begin
  2350. consume(_INTCONST);
  2351. p1:=crealconstnode.create(d,pbestrealtype^);
  2352. end;
  2353. end
  2354. else
  2355. { the necessary range checking has already been done by val }
  2356. tordconstnode(p1).rangecheck:=false;
  2357. end;
  2358. _REALNUMBER :
  2359. begin
  2360. val(pattern,d,code);
  2361. if code<>0 then
  2362. begin
  2363. Message(parser_e_error_in_real);
  2364. d:=1.0;
  2365. end;
  2366. consume(_REALNUMBER);
  2367. {$ifdef FPC_REAL2REAL_FIXED}
  2368. if current_settings.fputype=fpu_none then
  2369. Message(parser_e_unsupported_real);
  2370. if (current_settings.minfpconstprec=s32real) and
  2371. (d = single(d)) then
  2372. p1:=crealconstnode.create(d,s32floattype)
  2373. else if (current_settings.minfpconstprec=s64real) and
  2374. (d = double(d)) then
  2375. p1:=crealconstnode.create(d,s64floattype)
  2376. else
  2377. {$endif FPC_REAL2REAL_FIXED}
  2378. p1:=crealconstnode.create(d,pbestrealtype^);
  2379. {$ifdef FPC_HAS_STR_CURRENCY}
  2380. val(pattern,cur,code);
  2381. if code=0 then
  2382. trealconstnode(p1).value_currency:=cur;
  2383. {$endif FPC_HAS_STR_CURRENCY}
  2384. end;
  2385. _STRING :
  2386. begin
  2387. string_dec(hdef,true);
  2388. { STRING can be also a type cast }
  2389. if try_to_consume(_LKLAMMER) then
  2390. begin
  2391. p1:=comp_expr(true,false);
  2392. consume(_RKLAMMER);
  2393. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2394. { handle postfix operators here e.g. string(a)[10] }
  2395. again:=true;
  2396. postfixoperators(p1,again);
  2397. end
  2398. else
  2399. p1:=ctypenode.create(hdef);
  2400. end;
  2401. _FILE :
  2402. begin
  2403. hdef:=cfiletype;
  2404. consume(_FILE);
  2405. { FILE can be also a type cast }
  2406. if try_to_consume(_LKLAMMER) then
  2407. begin
  2408. p1:=comp_expr(true,false);
  2409. consume(_RKLAMMER);
  2410. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2411. { handle postfix operators here e.g. string(a)[10] }
  2412. again:=true;
  2413. postfixoperators(p1,again);
  2414. end
  2415. else
  2416. begin
  2417. p1:=ctypenode.create(hdef);
  2418. end;
  2419. end;
  2420. _CSTRING :
  2421. begin
  2422. p1:=cstringconstnode.createpchar(ansistring2pchar(cstringpattern),length(cstringpattern));
  2423. consume(_CSTRING);
  2424. end;
  2425. _CCHAR :
  2426. begin
  2427. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  2428. consume(_CCHAR);
  2429. end;
  2430. _CWSTRING:
  2431. begin
  2432. p1:=cstringconstnode.createwstr(patternw);
  2433. consume(_CWSTRING);
  2434. end;
  2435. _CWCHAR:
  2436. begin
  2437. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  2438. consume(_CWCHAR);
  2439. end;
  2440. _KLAMMERAFFE :
  2441. begin
  2442. consume(_KLAMMERAFFE);
  2443. got_addrn:=true;
  2444. { support both @<x> and @(<x>) }
  2445. if try_to_consume(_LKLAMMER) then
  2446. begin
  2447. p1:=factor(true,false);
  2448. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2449. begin
  2450. again:=true;
  2451. postfixoperators(p1,again);
  2452. end
  2453. else
  2454. consume(_RKLAMMER);
  2455. end
  2456. else
  2457. p1:=factor(true,false);
  2458. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2459. begin
  2460. again:=true;
  2461. postfixoperators(p1,again);
  2462. end;
  2463. got_addrn:=false;
  2464. p1:=caddrnode.create(p1);
  2465. p1.fileinfo:=filepos;
  2466. if cs_typed_addresses in current_settings.localswitches then
  2467. include(p1.flags,nf_typedaddr);
  2468. { Store the procvar that we are expecting, the
  2469. addrn will use the information to find the correct
  2470. procdef or it will return an error }
  2471. if assigned(getprocvardef) and
  2472. (taddrnode(p1).left.nodetype = loadn) then
  2473. taddrnode(p1).getprocvardef:=getprocvardef;
  2474. end;
  2475. _LKLAMMER :
  2476. begin
  2477. consume(_LKLAMMER);
  2478. p1:=comp_expr(true,false);
  2479. consume(_RKLAMMER);
  2480. { it's not a good solution }
  2481. { but (a+b)^ makes some problems }
  2482. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2483. begin
  2484. again:=true;
  2485. postfixoperators(p1,again);
  2486. end;
  2487. end;
  2488. _LECKKLAMMER :
  2489. begin
  2490. consume(_LECKKLAMMER);
  2491. p1:=factor_read_set;
  2492. consume(_RECKKLAMMER);
  2493. end;
  2494. _PLUS :
  2495. begin
  2496. consume(_PLUS);
  2497. p1:=factor(false,false);
  2498. p1:=cunaryplusnode.create(p1);
  2499. end;
  2500. _MINUS :
  2501. begin
  2502. consume(_MINUS);
  2503. if (token = _INTCONST) then
  2504. begin
  2505. { ugly hack, but necessary to be able to parse }
  2506. { -9223372036854775808 as int64 (JM) }
  2507. pattern := '-'+pattern;
  2508. p1:=sub_expr(oppower,false,false);
  2509. { -1 ** 4 should be - (1 ** 4) and not
  2510. (-1) ** 4
  2511. This was the reason of tw0869.pp test failure PM }
  2512. if p1.nodetype=starstarn then
  2513. begin
  2514. if tbinarynode(p1).left.nodetype=ordconstn then
  2515. begin
  2516. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  2517. p1:=cunaryminusnode.create(p1);
  2518. end
  2519. else if tbinarynode(p1).left.nodetype=realconstn then
  2520. begin
  2521. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  2522. trealconstnode(tbinarynode(p1).left).value_currency:=-trealconstnode(tbinarynode(p1).left).value_currency;
  2523. p1:=cunaryminusnode.create(p1);
  2524. end
  2525. else
  2526. internalerror(20021029);
  2527. end;
  2528. end
  2529. else
  2530. begin
  2531. p1:=sub_expr(oppower,false,false);
  2532. p1:=cunaryminusnode.create(p1);
  2533. end;
  2534. end;
  2535. _OP_NOT :
  2536. begin
  2537. consume(_OP_NOT);
  2538. p1:=factor(false,false);
  2539. p1:=cnotnode.create(p1);
  2540. end;
  2541. _TRUE :
  2542. begin
  2543. consume(_TRUE);
  2544. p1:=cordconstnode.create(1,booltype,false);
  2545. end;
  2546. _FALSE :
  2547. begin
  2548. consume(_FALSE);
  2549. p1:=cordconstnode.create(0,booltype,false);
  2550. end;
  2551. _NIL :
  2552. begin
  2553. consume(_NIL);
  2554. p1:=cnilnode.create;
  2555. { It's really ugly code nil^, but delphi allows it }
  2556. if token in [_CARET] then
  2557. begin
  2558. again:=true;
  2559. postfixoperators(p1,again);
  2560. end;
  2561. end;
  2562. _OBJCPROTOCOL:
  2563. begin
  2564. { The @protocol keyword is used in two ways in Objective-C:
  2565. 1) to declare protocols (~ Object Pascal interfaces)
  2566. 2) to obtain the metaclass (~ Object Pascal) "class of")
  2567. of a declared protocol
  2568. This code is for handling the second case. Because of 1),
  2569. we cannot simply use a system unit symbol.
  2570. }
  2571. consume(_OBJCPROTOCOL);
  2572. consume(_LKLAMMER);
  2573. p1:=factor(false,false);
  2574. consume(_RKLAMMER);
  2575. p1:=cinlinenode.create(in_objc_protocol_x,false,p1);
  2576. end;
  2577. else
  2578. begin
  2579. Message(parser_e_illegal_expression);
  2580. p1:=cerrornode.create;
  2581. { recover }
  2582. consume(token);
  2583. end;
  2584. end;
  2585. end;
  2586. { generate error node if no node is created }
  2587. if not assigned(p1) then
  2588. begin
  2589. {$ifdef EXTDEBUG}
  2590. Comment(V_Warning,'factor: p1=nil');
  2591. {$endif}
  2592. p1:=cerrornode.create;
  2593. updatefpos:=true;
  2594. end;
  2595. { get the resultdef for the node }
  2596. if (not assigned(p1.resultdef)) then
  2597. begin
  2598. do_typecheckpass_changed(p1,nodechanged);
  2599. updatefpos:=updatefpos or nodechanged;
  2600. end;
  2601. if assigned(p1) and
  2602. updatefpos then
  2603. p1.fileinfo:=filepos;
  2604. factor:=p1;
  2605. end;
  2606. {$maxfpuregisters default}
  2607. {****************************************************************************
  2608. Sub_Expr
  2609. ****************************************************************************}
  2610. const
  2611. { Warning these stay be ordered !! }
  2612. operator_levels:array[Toperator_precedence] of set of NOTOKEN..last_operator=
  2613. ([_LT,_LTE,_GT,_GTE,_EQ,_NE,_OP_IN],
  2614. [_PLUS,_MINUS,_OP_OR,_PIPE,_OP_XOR],
  2615. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  2616. _OP_AS,_OP_IS,_OP_AND,_AMPERSAND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  2617. [_STARSTAR] );
  2618. function sub_expr(pred_level:Toperator_precedence;accept_equal,typeonly:boolean):tnode;
  2619. {Reads a subexpression while the operators are of the current precedence
  2620. level, or any higher level. Replaces the old term, simpl_expr and
  2621. simpl2_expr.}
  2622. var
  2623. p1,p2 : tnode;
  2624. oldt : Ttoken;
  2625. filepos : tfileposinfo;
  2626. begin
  2627. if pred_level=highest_precedence then
  2628. p1:=factor(false,typeonly)
  2629. else
  2630. p1:=sub_expr(succ(pred_level),true,typeonly);
  2631. repeat
  2632. if (token in operator_levels[pred_level]) and
  2633. ((token<>_EQ) or accept_equal) then
  2634. begin
  2635. oldt:=token;
  2636. filepos:=current_tokenpos;
  2637. consume(token);
  2638. if pred_level=highest_precedence then
  2639. p2:=factor(false,false)
  2640. else
  2641. p2:=sub_expr(succ(pred_level),true,typeonly);
  2642. case oldt of
  2643. _PLUS :
  2644. p1:=caddnode.create(addn,p1,p2);
  2645. _MINUS :
  2646. p1:=caddnode.create(subn,p1,p2);
  2647. _STAR :
  2648. p1:=caddnode.create(muln,p1,p2);
  2649. _SLASH :
  2650. p1:=caddnode.create(slashn,p1,p2);
  2651. _EQ:
  2652. p1:=caddnode.create(equaln,p1,p2);
  2653. _GT :
  2654. p1:=caddnode.create(gtn,p1,p2);
  2655. _LT :
  2656. p1:=caddnode.create(ltn,p1,p2);
  2657. _GTE :
  2658. p1:=caddnode.create(gten,p1,p2);
  2659. _LTE :
  2660. p1:=caddnode.create(lten,p1,p2);
  2661. _SYMDIF :
  2662. p1:=caddnode.create(symdifn,p1,p2);
  2663. _STARSTAR :
  2664. p1:=caddnode.create(starstarn,p1,p2);
  2665. _OP_AS :
  2666. p1:=casnode.create(p1,p2);
  2667. _OP_IN :
  2668. p1:=cinnode.create(p1,p2);
  2669. _OP_IS :
  2670. p1:=cisnode.create(p1,p2);
  2671. _OP_OR,
  2672. _PIPE {macpas only} :
  2673. begin
  2674. p1:=caddnode.create(orn,p1,p2);
  2675. if (oldt = _PIPE) then
  2676. include(p1.flags,nf_short_bool);
  2677. end;
  2678. _OP_AND,
  2679. _AMPERSAND {macpas only} :
  2680. begin
  2681. p1:=caddnode.create(andn,p1,p2);
  2682. if (oldt = _AMPERSAND) then
  2683. include(p1.flags,nf_short_bool);
  2684. end;
  2685. _OP_DIV :
  2686. p1:=cmoddivnode.create(divn,p1,p2);
  2687. _OP_NOT :
  2688. p1:=cnotnode.create(p1);
  2689. _OP_MOD :
  2690. p1:=cmoddivnode.create(modn,p1,p2);
  2691. _OP_SHL :
  2692. p1:=cshlshrnode.create(shln,p1,p2);
  2693. _OP_SHR :
  2694. p1:=cshlshrnode.create(shrn,p1,p2);
  2695. _OP_XOR :
  2696. p1:=caddnode.create(xorn,p1,p2);
  2697. _ASSIGNMENT :
  2698. p1:=cassignmentnode.create(p1,p2);
  2699. _NE :
  2700. p1:=caddnode.create(unequaln,p1,p2);
  2701. end;
  2702. p1.fileinfo:=filepos;
  2703. end
  2704. else
  2705. break;
  2706. until false;
  2707. sub_expr:=p1;
  2708. end;
  2709. function comp_expr(accept_equal,typeonly:boolean):tnode;
  2710. var
  2711. oldafterassignment : boolean;
  2712. p1 : tnode;
  2713. begin
  2714. oldafterassignment:=afterassignment;
  2715. afterassignment:=true;
  2716. p1:=sub_expr(opcompare,accept_equal,typeonly);
  2717. { get the resultdef for this expression }
  2718. if not assigned(p1.resultdef) then
  2719. do_typecheckpass(p1);
  2720. afterassignment:=oldafterassignment;
  2721. comp_expr:=p1;
  2722. end;
  2723. function expr(dotypecheck : boolean) : tnode;
  2724. var
  2725. p1,p2 : tnode;
  2726. filepos : tfileposinfo;
  2727. oldafterassignment,
  2728. updatefpos : boolean;
  2729. begin
  2730. oldafterassignment:=afterassignment;
  2731. p1:=sub_expr(opcompare,true,false);
  2732. { get the resultdef for this expression }
  2733. if not assigned(p1.resultdef) and
  2734. dotypecheck then
  2735. do_typecheckpass(p1);
  2736. filepos:=current_tokenpos;
  2737. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2738. afterassignment:=true;
  2739. updatefpos:=true;
  2740. case token of
  2741. _POINTPOINT :
  2742. begin
  2743. consume(_POINTPOINT);
  2744. p2:=sub_expr(opcompare,true,false);
  2745. p1:=crangenode.create(p1,p2);
  2746. end;
  2747. _ASSIGNMENT :
  2748. begin
  2749. consume(_ASSIGNMENT);
  2750. if (p1.resultdef.typ=procvardef) then
  2751. getprocvardef:=tprocvardef(p1.resultdef);
  2752. p2:=sub_expr(opcompare,true,false);
  2753. if assigned(getprocvardef) then
  2754. handle_procvar(getprocvardef,p2);
  2755. getprocvardef:=nil;
  2756. p1:=cassignmentnode.create(p1,p2);
  2757. end;
  2758. _PLUSASN :
  2759. begin
  2760. consume(_PLUSASN);
  2761. p2:=sub_expr(opcompare,true,false);
  2762. p1:=gen_c_style_operator(addn,p1,p2);
  2763. end;
  2764. _MINUSASN :
  2765. begin
  2766. consume(_MINUSASN);
  2767. p2:=sub_expr(opcompare,true,false);
  2768. p1:=gen_c_style_operator(subn,p1,p2);
  2769. end;
  2770. _STARASN :
  2771. begin
  2772. consume(_STARASN );
  2773. p2:=sub_expr(opcompare,true,false);
  2774. p1:=gen_c_style_operator(muln,p1,p2);
  2775. end;
  2776. _SLASHASN :
  2777. begin
  2778. consume(_SLASHASN );
  2779. p2:=sub_expr(opcompare,true,false);
  2780. p1:=gen_c_style_operator(slashn,p1,p2);
  2781. end;
  2782. else
  2783. updatefpos:=false;
  2784. end;
  2785. { get the resultdef for this expression }
  2786. if not assigned(p1.resultdef) and
  2787. dotypecheck then
  2788. do_typecheckpass(p1);
  2789. afterassignment:=oldafterassignment;
  2790. if updatefpos then
  2791. p1.fileinfo:=filepos;
  2792. expr:=p1;
  2793. end;
  2794. function get_intconst:TConstExprInt;
  2795. {Reads an expression, tries to evalute it and check if it is an integer
  2796. constant. Then the constant is returned.}
  2797. var
  2798. p:tnode;
  2799. begin
  2800. result:=0;
  2801. p:=comp_expr(true,false);
  2802. if not codegenerror then
  2803. begin
  2804. if (p.nodetype<>ordconstn) or
  2805. not(is_integer(p.resultdef)) then
  2806. Message(parser_e_illegal_expression)
  2807. else
  2808. result:=tordconstnode(p).value;
  2809. end;
  2810. p.free;
  2811. end;
  2812. function get_stringconst:string;
  2813. {Reads an expression, tries to evaluate it and checks if it is a string
  2814. constant. Then the constant is returned.}
  2815. var
  2816. p:tnode;
  2817. begin
  2818. get_stringconst:='';
  2819. p:=comp_expr(true,false);
  2820. if p.nodetype<>stringconstn then
  2821. begin
  2822. if (p.nodetype=ordconstn) and is_char(p.resultdef) then
  2823. get_stringconst:=char(int64(tordconstnode(p).value))
  2824. else
  2825. Message(parser_e_illegal_expression);
  2826. end
  2827. else
  2828. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2829. p.free;
  2830. end;
  2831. end.