pexpr.pas 120 KB

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