pexpr.pas 120 KB

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