pexpr.pas 115 KB

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