pexpr.pas 115 KB

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