pexpr.pas 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992
  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(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[])
  1355. else
  1356. { otherwise it's a regular record subscript }
  1357. p1:=csubscriptnode.create(srsym,p1);
  1358. end
  1359. else
  1360. { regular non-field load }
  1361. p1:=cloadnode.create(srsym,srsymtable);
  1362. end;
  1363. syssym :
  1364. begin
  1365. p1:=statement_syssym(tsyssym(srsym).number);
  1366. end;
  1367. typesym :
  1368. begin
  1369. hdef:=ttypesym(srsym).typedef;
  1370. if not assigned(hdef) then
  1371. begin
  1372. again:=false;
  1373. end
  1374. else
  1375. begin
  1376. { We need to know if this unit uses Variants }
  1377. if (hdef=cvarianttype) and
  1378. not(cs_compilesystem in current_settings.moduleswitches) then
  1379. current_module.flags:=current_module.flags or uf_uses_variants;
  1380. { if we get a generic then check that it is not an inline specialization }
  1381. if (df_generic in hdef.defoptions) and
  1382. (token=_LT) and
  1383. (m_delphi in current_settings.modeswitches) then
  1384. generate_specialization(hdef,false);
  1385. if try_to_consume(_LKLAMMER) then
  1386. begin
  1387. p1:=comp_expr(true,false);
  1388. consume(_RKLAMMER);
  1389. 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(tabstractrecorddef(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(tabstractrecorddef(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(tabstractrecorddef(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(tabstractrecorddef(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(tabstractrecorddef(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. calltype: tdispcalltype;
  1734. label
  1735. skipreckklammercheck;
  1736. begin
  1737. result:=false;
  1738. again:=true;
  1739. while again do
  1740. begin
  1741. { we need the resultdef }
  1742. do_typecheckpass_changed(p1,nodechanged);
  1743. result:=result or nodechanged;
  1744. if codegenerror then
  1745. begin
  1746. recoverconsume_postfixops;
  1747. exit;
  1748. end;
  1749. { handle token }
  1750. case token of
  1751. _CARET:
  1752. begin
  1753. consume(_CARET);
  1754. { support tp/mac procvar^ if the procvar returns a
  1755. pointer type }
  1756. if ((m_tp_procvar in current_settings.modeswitches) or
  1757. (m_mac_procvar in current_settings.modeswitches)) and
  1758. (p1.resultdef.typ=procvardef) and
  1759. (tprocvardef(p1.resultdef).returndef.typ=pointerdef) then
  1760. begin
  1761. p1:=ccallnode.create_procvar(nil,p1);
  1762. typecheckpass(p1);
  1763. end;
  1764. if (p1.resultdef.typ<>pointerdef) then
  1765. begin
  1766. { ^ as binary operator is a problem!!!! (FK) }
  1767. again:=false;
  1768. Message(parser_e_invalid_qualifier);
  1769. recoverconsume_postfixops;
  1770. p1.destroy;
  1771. p1:=cerrornode.create;
  1772. end
  1773. else
  1774. p1:=cderefnode.create(p1);
  1775. end;
  1776. _LECKKLAMMER:
  1777. begin
  1778. if is_class_or_interface_or_object(p1.resultdef) or
  1779. is_dispinterface(p1.resultdef) then
  1780. begin
  1781. { default property }
  1782. protsym:=search_default_property(tobjectdef(p1.resultdef));
  1783. if not(assigned(protsym)) then
  1784. begin
  1785. p1.destroy;
  1786. p1:=cerrornode.create;
  1787. again:=false;
  1788. message(parser_e_no_default_property_available);
  1789. end
  1790. else
  1791. begin
  1792. { The property symbol is referenced indirect }
  1793. protsym.IncRefCount;
  1794. handle_propertysym(protsym,protsym.owner,p1);
  1795. end;
  1796. end
  1797. else
  1798. begin
  1799. consume(_LECKKLAMMER);
  1800. repeat
  1801. { in all of the cases below, p1 is changed }
  1802. case p1.resultdef.typ of
  1803. pointerdef:
  1804. begin
  1805. { support delphi autoderef }
  1806. if (tpointerdef(p1.resultdef).pointeddef.typ=arraydef) and
  1807. (m_autoderef in current_settings.modeswitches) then
  1808. p1:=cderefnode.create(p1);
  1809. p2:=comp_expr(true,false);
  1810. { Support Pbytevar[0..9] which returns array [0..9].}
  1811. if try_to_consume(_POINTPOINT) then
  1812. p2:=crangenode.create(p2,comp_expr(true,false));
  1813. p1:=cvecnode.create(p1,p2);
  1814. end;
  1815. variantdef:
  1816. begin
  1817. handle_variantarray;
  1818. { the RECKKLAMMER is already read }
  1819. goto skipreckklammercheck;
  1820. end;
  1821. stringdef :
  1822. begin
  1823. p2:=comp_expr(true,false);
  1824. { Support string[0..9] which returns array [0..9] of char.}
  1825. if try_to_consume(_POINTPOINT) then
  1826. p2:=crangenode.create(p2,comp_expr(true,false));
  1827. p1:=cvecnode.create(p1,p2);
  1828. end;
  1829. arraydef:
  1830. begin
  1831. p2:=comp_expr(true,false);
  1832. { support SEG:OFS for go32v2 Mem[] }
  1833. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  1834. (p1.nodetype=loadn) and
  1835. assigned(tloadnode(p1).symtableentry) and
  1836. assigned(tloadnode(p1).symtableentry.owner.name) and
  1837. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1838. ((tloadnode(p1).symtableentry.name='MEM') or
  1839. (tloadnode(p1).symtableentry.name='MEMW') or
  1840. (tloadnode(p1).symtableentry.name='MEML')) then
  1841. begin
  1842. if try_to_consume(_COLON) then
  1843. begin
  1844. p3:=caddnode.create(muln,cordconstnode.create($10,s32inttype,false),p2);
  1845. p2:=comp_expr(true,false);
  1846. p2:=caddnode.create(addn,p2,p3);
  1847. if try_to_consume(_POINTPOINT) then
  1848. { Support mem[$a000:$0000..$07ff] which returns array [0..$7ff] of memtype.}
  1849. p2:=crangenode.create(p2,caddnode.create(addn,comp_expr(true,false),p3.getcopy));
  1850. p1:=cvecnode.create(p1,p2);
  1851. include(tvecnode(p1).flags,nf_memseg);
  1852. include(tvecnode(p1).flags,nf_memindex);
  1853. end
  1854. else
  1855. begin
  1856. if try_to_consume(_POINTPOINT) then
  1857. { Support mem[$80000000..$80000002] which returns array [0..2] of memtype.}
  1858. p2:=crangenode.create(p2,comp_expr(true,false));
  1859. p1:=cvecnode.create(p1,p2);
  1860. include(tvecnode(p1).flags,nf_memindex);
  1861. end;
  1862. end
  1863. else
  1864. begin
  1865. if try_to_consume(_POINTPOINT) then
  1866. { Support arrayvar[0..9] which returns array [0..9] of arraytype.}
  1867. p2:=crangenode.create(p2,comp_expr(true,false));
  1868. p1:=cvecnode.create(p1,p2);
  1869. end;
  1870. end;
  1871. else
  1872. begin
  1873. if p1.resultdef.typ<>undefineddef then
  1874. Message(parser_e_invalid_qualifier);
  1875. p1.destroy;
  1876. p1:=cerrornode.create;
  1877. comp_expr(true,false);
  1878. again:=false;
  1879. end;
  1880. end;
  1881. do_typecheckpass(p1);
  1882. until not try_to_consume(_COMMA);
  1883. consume(_RECKKLAMMER);
  1884. { handle_variantarray eats the RECKKLAMMER and jumps here }
  1885. skipreckklammercheck:
  1886. end;
  1887. end;
  1888. _POINT :
  1889. begin
  1890. consume(_POINT);
  1891. if (p1.resultdef.typ=pointerdef) and
  1892. (m_autoderef in current_settings.modeswitches) and
  1893. { don't auto-deref objc.id, because then the code
  1894. below for supporting id.anyobjcmethod isn't triggered }
  1895. (p1.resultdef<>objc_idtype) then
  1896. begin
  1897. p1:=cderefnode.create(p1);
  1898. do_typecheckpass(p1);
  1899. end;
  1900. { procvar.<something> can never mean anything so always
  1901. try to call it in case it returns a record/object/... }
  1902. maybe_call_procvar(p1,false);
  1903. case p1.resultdef.typ of
  1904. recorddef:
  1905. begin
  1906. if token=_ID then
  1907. begin
  1908. structh:=tabstractrecorddef(p1.resultdef);
  1909. searchsym_in_record(structh,pattern,srsym,srsymtable);
  1910. if assigned(srsym) then
  1911. begin
  1912. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1913. consume(_ID);
  1914. do_member_read(structh,getaddr,srsym,p1,again,[]);
  1915. end
  1916. else
  1917. begin
  1918. Message1(sym_e_id_no_member,orgpattern);
  1919. p1.destroy;
  1920. p1:=cerrornode.create;
  1921. { try to clean up }
  1922. consume(_ID);
  1923. end;
  1924. end
  1925. else
  1926. consume(_ID);
  1927. end;
  1928. enumdef:
  1929. begin
  1930. if token=_ID then
  1931. begin
  1932. srsym:=tsym(tenumdef(p1.resultdef).symtable.Find(pattern));
  1933. p1.destroy;
  1934. if assigned(srsym) and (srsym.typ=enumsym) then
  1935. begin
  1936. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1937. p1:=genenumnode(tenumsym(srsym));
  1938. end
  1939. else
  1940. begin
  1941. Message1(sym_e_id_no_member,orgpattern);
  1942. p1:=cerrornode.create;
  1943. end;
  1944. end;
  1945. consume(_ID);
  1946. end;
  1947. variantdef:
  1948. begin
  1949. { dispatch call? }
  1950. { lhs := v.ident[parameters] -> property get
  1951. lhs := v.ident(parameters) -> method call
  1952. v.ident[parameters] := rhs -> property put
  1953. v.ident(parameters) := rhs -> also property put }
  1954. if token=_ID then
  1955. begin
  1956. dispatchstring:=orgpattern;
  1957. consume(_ID);
  1958. calltype:=dct_method;
  1959. if try_to_consume(_LKLAMMER) then
  1960. begin
  1961. p2:=parse_paras(false,true,_RKLAMMER);
  1962. consume(_RKLAMMER);
  1963. end
  1964. else if try_to_consume(_LECKKLAMMER) then
  1965. begin
  1966. p2:=parse_paras(false,true,_RECKKLAMMER);
  1967. consume(_RECKKLAMMER);
  1968. calltype:=dct_propget;
  1969. end
  1970. else
  1971. p2:=nil;
  1972. { property setter? }
  1973. if (token=_ASSIGNMENT) and not(afterassignment) then
  1974. begin
  1975. consume(_ASSIGNMENT);
  1976. { read the expression }
  1977. p3:=comp_expr(true,false);
  1978. { concat value parameter too }
  1979. p2:=ccallparanode.create(p3,p2);
  1980. p1:=translate_disp_call(p1,p2,dct_propput,dispatchstring,0,voidtype);
  1981. end
  1982. else
  1983. { this is only an approximation
  1984. setting useresult if not necessary is only a waste of time, no more, no less (FK) }
  1985. if afterassignment or in_args or (token<>_SEMICOLON) then
  1986. p1:=translate_disp_call(p1,p2,calltype,dispatchstring,0,cvarianttype)
  1987. else
  1988. p1:=translate_disp_call(p1,p2,calltype,dispatchstring,0,voidtype);
  1989. end
  1990. else { Error }
  1991. Consume(_ID);
  1992. end;
  1993. classrefdef:
  1994. begin
  1995. if token=_ID then
  1996. begin
  1997. structh:=tobjectdef(tclassrefdef(p1.resultdef).pointeddef);
  1998. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable);
  1999. if assigned(srsym) then
  2000. begin
  2001. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2002. consume(_ID);
  2003. do_member_read(structh,getaddr,srsym,p1,again,[]);
  2004. end
  2005. else
  2006. begin
  2007. Message1(sym_e_id_no_member,orgpattern);
  2008. p1.destroy;
  2009. p1:=cerrornode.create;
  2010. { try to clean up }
  2011. consume(_ID);
  2012. end;
  2013. end
  2014. else { Error }
  2015. Consume(_ID);
  2016. end;
  2017. objectdef:
  2018. begin
  2019. if token=_ID then
  2020. begin
  2021. structh:=tobjectdef(p1.resultdef);
  2022. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable);
  2023. if assigned(srsym) then
  2024. begin
  2025. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2026. consume(_ID);
  2027. do_member_read(structh,getaddr,srsym,p1,again,[]);
  2028. end
  2029. else
  2030. begin
  2031. Message1(sym_e_id_no_member,orgpattern);
  2032. p1.destroy;
  2033. p1:=cerrornode.create;
  2034. { try to clean up }
  2035. consume(_ID);
  2036. end;
  2037. end
  2038. else { Error }
  2039. Consume(_ID);
  2040. end;
  2041. pointerdef:
  2042. begin
  2043. if (p1.resultdef=objc_idtype) then
  2044. begin
  2045. { objc's id type can be used to call any
  2046. Objective-C method of any Objective-C class
  2047. type that's currently in scope }
  2048. if search_objc_method(pattern,srsym,srsymtable) then
  2049. begin
  2050. consume(_ID);
  2051. do_proc_call(srsym,srsymtable,nil,
  2052. (getaddr and not(token in [_CARET,_POINT])),
  2053. again,p1,[cnf_objc_id_call]);
  2054. { we need to know which procedure is called }
  2055. do_typecheckpass(p1);
  2056. end
  2057. else
  2058. begin
  2059. consume(_ID);
  2060. Message(parser_e_methode_id_expected);
  2061. end;
  2062. end
  2063. else
  2064. begin
  2065. Message(parser_e_invalid_qualifier);
  2066. if tpointerdef(p1.resultdef).pointeddef.typ in [recorddef,objectdef,classrefdef] then
  2067. Message(parser_h_maybe_deref_caret_missing);
  2068. end
  2069. end;
  2070. else
  2071. begin
  2072. if p1.resultdef.typ<>undefineddef then
  2073. Message(parser_e_invalid_qualifier);
  2074. p1.destroy;
  2075. p1:=cerrornode.create;
  2076. { Error }
  2077. consume(_ID);
  2078. end;
  2079. end;
  2080. end;
  2081. else
  2082. begin
  2083. { is this a procedure variable ? }
  2084. if assigned(p1.resultdef) and
  2085. (p1.resultdef.typ=procvardef) then
  2086. begin
  2087. { Typenode for typecasting or expecting a procvar }
  2088. if (p1.nodetype=typen) or
  2089. (
  2090. assigned(getprocvardef) and
  2091. equal_defs(p1.resultdef,getprocvardef)
  2092. ) then
  2093. begin
  2094. if try_to_consume(_LKLAMMER) then
  2095. begin
  2096. p1:=comp_expr(true,false);
  2097. consume(_RKLAMMER);
  2098. p1:=ctypeconvnode.create_explicit(p1,p1.resultdef);
  2099. end
  2100. else
  2101. again:=false
  2102. end
  2103. else
  2104. begin
  2105. if try_to_consume(_LKLAMMER) then
  2106. begin
  2107. p2:=parse_paras(false,false,_RKLAMMER);
  2108. consume(_RKLAMMER);
  2109. p1:=ccallnode.create_procvar(p2,p1);
  2110. { proc():= is never possible }
  2111. if token=_ASSIGNMENT then
  2112. begin
  2113. Message(parser_e_illegal_expression);
  2114. p1.free;
  2115. p1:=cerrornode.create;
  2116. again:=false;
  2117. end;
  2118. end
  2119. else
  2120. again:=false;
  2121. end;
  2122. end
  2123. else
  2124. again:=false;
  2125. end;
  2126. end;
  2127. { we only try again if p1 was changed }
  2128. if again or
  2129. (p1.nodetype=errorn) then
  2130. result:=true;
  2131. end; { while again }
  2132. end;
  2133. {---------------------------------------------
  2134. Factor (Main)
  2135. ---------------------------------------------}
  2136. var
  2137. l : longint;
  2138. ic : int64;
  2139. qc : qword;
  2140. p1 : tnode;
  2141. code : integer;
  2142. srsym : tsym;
  2143. srsymtable : TSymtable;
  2144. pd : tprocdef;
  2145. hclassdef : tobjectdef;
  2146. d : bestreal;
  2147. cur : currency;
  2148. hs,hsorg : string;
  2149. hdef : tdef;
  2150. filepos : tfileposinfo;
  2151. again,
  2152. updatefpos,
  2153. nodechanged : boolean;
  2154. begin
  2155. { can't keep a copy of p1 and compare pointers afterwards, because
  2156. p1 may be freed and reallocated in the same place! }
  2157. updatefpos:=false;
  2158. p1:=nil;
  2159. filepos:=current_tokenpos;
  2160. again:=false;
  2161. if token=_ID then
  2162. begin
  2163. again:=true;
  2164. { Handle references to self }
  2165. if (idtoken=_SELF) and
  2166. not(block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) and
  2167. assigned(current_structdef) then
  2168. begin
  2169. p1:=load_self_node;
  2170. consume(_ID);
  2171. again:=true;
  2172. end
  2173. else
  2174. factor_read_id(p1,again);
  2175. if assigned(p1) then
  2176. begin
  2177. { factor_read_id will set the filepos to after the id,
  2178. and in case of _SELF the filepos will already be the
  2179. same as filepos (so setting it again doesn't hurt). }
  2180. p1.fileinfo:=filepos;
  2181. filepos:=current_tokenpos;
  2182. end;
  2183. { handle post fix operators }
  2184. updatefpos:=postfixoperators(p1,again);
  2185. end
  2186. else
  2187. begin
  2188. updatefpos:=true;
  2189. case token of
  2190. _RETURN :
  2191. begin
  2192. consume(_RETURN);
  2193. if not(token in [_SEMICOLON,_ELSE,_END]) then
  2194. p1 := cexitnode.create(comp_expr(true,false))
  2195. else
  2196. p1 := cexitnode.create(nil);
  2197. end;
  2198. _INHERITED :
  2199. begin
  2200. again:=true;
  2201. consume(_INHERITED);
  2202. if assigned(current_procinfo) and
  2203. assigned(current_structdef) and
  2204. (current_structdef.typ=objectdef) then
  2205. begin
  2206. hclassdef:=tobjectdef(current_structdef).childof;
  2207. { Objective-C categories *replace* methods in the class
  2208. they extend, or add methods to it. So calling an
  2209. inherited method always calls the method inherited from
  2210. the parent of the extended class }
  2211. if is_objccategory(current_structdef) then
  2212. hclassdef:=hclassdef.childof;
  2213. { if inherited; only then we need the method with
  2214. the same name }
  2215. if token <> _ID then
  2216. begin
  2217. hs:=current_procinfo.procdef.procsym.name;
  2218. hsorg:=current_procinfo.procdef.procsym.realname;
  2219. anon_inherited:=true;
  2220. { For message methods we need to search using the message
  2221. number or string }
  2222. pd:=tprocdef(tprocsym(current_procinfo.procdef.procsym).ProcdefList[0]);
  2223. srdef:=nil;
  2224. if (po_msgint in pd.procoptions) then
  2225. searchsym_in_class_by_msgint(hclassdef,pd.messageinf.i,srdef,srsym,srsymtable)
  2226. else
  2227. if (po_msgstr in pd.procoptions) then
  2228. searchsym_in_class_by_msgstr(hclassdef,pd.messageinf.str^,srsym,srsymtable)
  2229. else
  2230. searchsym_in_class(hclassdef,tobjectdef(current_structdef),hs,srsym,srsymtable);
  2231. end
  2232. else
  2233. begin
  2234. hs:=pattern;
  2235. hsorg:=orgpattern;
  2236. consume(_ID);
  2237. anon_inherited:=false;
  2238. searchsym_in_class(hclassdef,tobjectdef(current_structdef),hs,srsym,srsymtable);
  2239. end;
  2240. if assigned(srsym) then
  2241. begin
  2242. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2243. { load the procdef from the inherited class and
  2244. not from self }
  2245. case srsym.typ of
  2246. procsym:
  2247. begin
  2248. hdef:=hclassdef;
  2249. if (po_classmethod in current_procinfo.procdef.procoptions) or
  2250. (po_staticmethod in current_procinfo.procdef.procoptions) then
  2251. hdef:=tclassrefdef.create(hdef);
  2252. p1:=ctypenode.create(hdef);
  2253. end;
  2254. propertysym:
  2255. ;
  2256. else
  2257. begin
  2258. Message(parser_e_methode_id_expected);
  2259. p1:=cerrornode.create;
  2260. end;
  2261. end;
  2262. do_member_read(hclassdef,getaddr,srsym,p1,again,[cnf_inherited,cnf_anon_inherited]);
  2263. end
  2264. else
  2265. begin
  2266. if anon_inherited then
  2267. begin
  2268. { For message methods we need to call DefaultHandler }
  2269. if (po_msgint in pd.procoptions) or
  2270. (po_msgstr in pd.procoptions) then
  2271. begin
  2272. searchsym_in_class(hclassdef,hclassdef,'DEFAULTHANDLER',srsym,srsymtable);
  2273. if not assigned(srsym) or
  2274. (srsym.typ<>procsym) then
  2275. internalerror(200303171);
  2276. p1:=nil;
  2277. do_proc_call(srsym,srsym.owner,hclassdef,false,again,p1,[]);
  2278. end
  2279. else
  2280. begin
  2281. { we need to ignore the inherited; }
  2282. p1:=cnothingnode.create;
  2283. end;
  2284. end
  2285. else
  2286. begin
  2287. Message1(sym_e_id_no_member,hsorg);
  2288. p1:=cerrornode.create;
  2289. end;
  2290. again:=false;
  2291. end;
  2292. { turn auto inheriting off }
  2293. anon_inherited:=false;
  2294. end
  2295. else
  2296. begin
  2297. Message(parser_e_generic_methods_only_in_methods);
  2298. again:=false;
  2299. p1:=cerrornode.create;
  2300. end;
  2301. postfixoperators(p1,again);
  2302. end;
  2303. _INTCONST :
  2304. begin
  2305. {Try first wether the value fits in an int64.}
  2306. val(pattern,ic,code);
  2307. if code=0 then
  2308. begin
  2309. consume(_INTCONST);
  2310. int_to_type(ic,hdef);
  2311. p1:=cordconstnode.create(ic,hdef,true);
  2312. end
  2313. else
  2314. begin
  2315. { try qword next }
  2316. val(pattern,qc,code);
  2317. if code=0 then
  2318. begin
  2319. consume(_INTCONST);
  2320. int_to_type(qc,hdef);
  2321. p1:=cordconstnode.create(qc,hdef,true);
  2322. end;
  2323. end;
  2324. if code<>0 then
  2325. begin
  2326. { finally float }
  2327. val(pattern,d,code);
  2328. if code<>0 then
  2329. begin
  2330. Message(parser_e_invalid_integer);
  2331. consume(_INTCONST);
  2332. l:=1;
  2333. p1:=cordconstnode.create(l,sinttype,true);
  2334. end
  2335. else
  2336. begin
  2337. consume(_INTCONST);
  2338. p1:=crealconstnode.create(d,pbestrealtype^);
  2339. end;
  2340. end
  2341. else
  2342. { the necessary range checking has already been done by val }
  2343. tordconstnode(p1).rangecheck:=false;
  2344. end;
  2345. _REALNUMBER :
  2346. begin
  2347. val(pattern,d,code);
  2348. if code<>0 then
  2349. begin
  2350. Message(parser_e_error_in_real);
  2351. d:=1.0;
  2352. end;
  2353. consume(_REALNUMBER);
  2354. {$ifdef FPC_REAL2REAL_FIXED}
  2355. if current_settings.fputype=fpu_none then
  2356. Message(parser_e_unsupported_real);
  2357. if (current_settings.minfpconstprec=s32real) and
  2358. (d = single(d)) then
  2359. p1:=crealconstnode.create(d,s32floattype)
  2360. else if (current_settings.minfpconstprec=s64real) and
  2361. (d = double(d)) then
  2362. p1:=crealconstnode.create(d,s64floattype)
  2363. else
  2364. {$endif FPC_REAL2REAL_FIXED}
  2365. p1:=crealconstnode.create(d,pbestrealtype^);
  2366. {$ifdef FPC_HAS_STR_CURRENCY}
  2367. val(pattern,cur,code);
  2368. if code=0 then
  2369. trealconstnode(p1).value_currency:=cur;
  2370. {$endif FPC_HAS_STR_CURRENCY}
  2371. end;
  2372. _STRING :
  2373. begin
  2374. string_dec(hdef,true);
  2375. { STRING can be also a type cast }
  2376. if try_to_consume(_LKLAMMER) then
  2377. begin
  2378. p1:=comp_expr(true,false);
  2379. consume(_RKLAMMER);
  2380. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2381. { handle postfix operators here e.g. string(a)[10] }
  2382. again:=true;
  2383. postfixoperators(p1,again);
  2384. end
  2385. else
  2386. p1:=ctypenode.create(hdef);
  2387. end;
  2388. _FILE :
  2389. begin
  2390. hdef:=cfiletype;
  2391. consume(_FILE);
  2392. { FILE can be also a type cast }
  2393. if try_to_consume(_LKLAMMER) then
  2394. begin
  2395. p1:=comp_expr(true,false);
  2396. consume(_RKLAMMER);
  2397. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2398. { handle postfix operators here e.g. string(a)[10] }
  2399. again:=true;
  2400. postfixoperators(p1,again);
  2401. end
  2402. else
  2403. begin
  2404. p1:=ctypenode.create(hdef);
  2405. end;
  2406. end;
  2407. _CSTRING :
  2408. begin
  2409. p1:=cstringconstnode.createpchar(ansistring2pchar(cstringpattern),length(cstringpattern));
  2410. consume(_CSTRING);
  2411. end;
  2412. _CCHAR :
  2413. begin
  2414. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  2415. consume(_CCHAR);
  2416. end;
  2417. _CWSTRING:
  2418. begin
  2419. p1:=cstringconstnode.createwstr(patternw);
  2420. consume(_CWSTRING);
  2421. end;
  2422. _CWCHAR:
  2423. begin
  2424. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  2425. consume(_CWCHAR);
  2426. end;
  2427. _KLAMMERAFFE :
  2428. begin
  2429. consume(_KLAMMERAFFE);
  2430. got_addrn:=true;
  2431. { support both @<x> and @(<x>) }
  2432. if try_to_consume(_LKLAMMER) then
  2433. begin
  2434. p1:=factor(true,false);
  2435. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2436. begin
  2437. again:=true;
  2438. postfixoperators(p1,again);
  2439. end
  2440. else
  2441. consume(_RKLAMMER);
  2442. end
  2443. else
  2444. p1:=factor(true,false);
  2445. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2446. begin
  2447. again:=true;
  2448. postfixoperators(p1,again);
  2449. end;
  2450. got_addrn:=false;
  2451. p1:=caddrnode.create(p1);
  2452. p1.fileinfo:=filepos;
  2453. if cs_typed_addresses in current_settings.localswitches then
  2454. include(p1.flags,nf_typedaddr);
  2455. { Store the procvar that we are expecting, the
  2456. addrn will use the information to find the correct
  2457. procdef or it will return an error }
  2458. if assigned(getprocvardef) and
  2459. (taddrnode(p1).left.nodetype = loadn) then
  2460. taddrnode(p1).getprocvardef:=getprocvardef;
  2461. end;
  2462. _LKLAMMER :
  2463. begin
  2464. consume(_LKLAMMER);
  2465. p1:=comp_expr(true,false);
  2466. consume(_RKLAMMER);
  2467. { it's not a good solution }
  2468. { but (a+b)^ makes some problems }
  2469. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2470. begin
  2471. again:=true;
  2472. postfixoperators(p1,again);
  2473. end;
  2474. end;
  2475. _LECKKLAMMER :
  2476. begin
  2477. consume(_LECKKLAMMER);
  2478. p1:=factor_read_set;
  2479. consume(_RECKKLAMMER);
  2480. end;
  2481. _PLUS :
  2482. begin
  2483. consume(_PLUS);
  2484. p1:=factor(false,false);
  2485. p1:=cunaryplusnode.create(p1);
  2486. end;
  2487. _MINUS :
  2488. begin
  2489. consume(_MINUS);
  2490. if (token = _INTCONST) then
  2491. begin
  2492. { ugly hack, but necessary to be able to parse }
  2493. { -9223372036854775808 as int64 (JM) }
  2494. pattern := '-'+pattern;
  2495. p1:=sub_expr(oppower,false,false);
  2496. { -1 ** 4 should be - (1 ** 4) and not
  2497. (-1) ** 4
  2498. This was the reason of tw0869.pp test failure PM }
  2499. if p1.nodetype=starstarn then
  2500. begin
  2501. if tbinarynode(p1).left.nodetype=ordconstn then
  2502. begin
  2503. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  2504. p1:=cunaryminusnode.create(p1);
  2505. end
  2506. else if tbinarynode(p1).left.nodetype=realconstn then
  2507. begin
  2508. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  2509. trealconstnode(tbinarynode(p1).left).value_currency:=-trealconstnode(tbinarynode(p1).left).value_currency;
  2510. p1:=cunaryminusnode.create(p1);
  2511. end
  2512. else
  2513. internalerror(20021029);
  2514. end;
  2515. end
  2516. else
  2517. begin
  2518. p1:=sub_expr(oppower,false,false);
  2519. p1:=cunaryminusnode.create(p1);
  2520. end;
  2521. end;
  2522. _OP_NOT :
  2523. begin
  2524. consume(_OP_NOT);
  2525. p1:=factor(false,false);
  2526. p1:=cnotnode.create(p1);
  2527. end;
  2528. _TRUE :
  2529. begin
  2530. consume(_TRUE);
  2531. p1:=cordconstnode.create(1,booltype,false);
  2532. end;
  2533. _FALSE :
  2534. begin
  2535. consume(_FALSE);
  2536. p1:=cordconstnode.create(0,booltype,false);
  2537. end;
  2538. _NIL :
  2539. begin
  2540. consume(_NIL);
  2541. p1:=cnilnode.create;
  2542. { It's really ugly code nil^, but delphi allows it }
  2543. if token in [_CARET] then
  2544. begin
  2545. again:=true;
  2546. postfixoperators(p1,again);
  2547. end;
  2548. end;
  2549. _OBJCPROTOCOL:
  2550. begin
  2551. { The @protocol keyword is used in two ways in Objective-C:
  2552. 1) to declare protocols (~ Object Pascal interfaces)
  2553. 2) to obtain the metaclass (~ Object Pascal) "class of")
  2554. of a declared protocol
  2555. This code is for handling the second case. Because of 1),
  2556. we cannot simply use a system unit symbol.
  2557. }
  2558. consume(_OBJCPROTOCOL);
  2559. consume(_LKLAMMER);
  2560. p1:=factor(false,false);
  2561. consume(_RKLAMMER);
  2562. p1:=cinlinenode.create(in_objc_protocol_x,false,p1);
  2563. end;
  2564. else
  2565. begin
  2566. Message(parser_e_illegal_expression);
  2567. p1:=cerrornode.create;
  2568. { recover }
  2569. consume(token);
  2570. end;
  2571. end;
  2572. end;
  2573. { generate error node if no node is created }
  2574. if not assigned(p1) then
  2575. begin
  2576. {$ifdef EXTDEBUG}
  2577. Comment(V_Warning,'factor: p1=nil');
  2578. {$endif}
  2579. p1:=cerrornode.create;
  2580. updatefpos:=true;
  2581. end;
  2582. { get the resultdef for the node }
  2583. if (not assigned(p1.resultdef)) then
  2584. begin
  2585. do_typecheckpass_changed(p1,nodechanged);
  2586. updatefpos:=updatefpos or nodechanged;
  2587. end;
  2588. if assigned(p1) and
  2589. updatefpos then
  2590. p1.fileinfo:=filepos;
  2591. factor:=p1;
  2592. end;
  2593. {$maxfpuregisters default}
  2594. {****************************************************************************
  2595. Sub_Expr
  2596. ****************************************************************************}
  2597. const
  2598. { Warning these stay be ordered !! }
  2599. operator_levels:array[Toperator_precedence] of set of NOTOKEN..last_operator=
  2600. ([_LT,_LTE,_GT,_GTE,_EQ,_NE,_OP_IN],
  2601. [_PLUS,_MINUS,_OP_OR,_PIPE,_OP_XOR],
  2602. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  2603. _OP_AS,_OP_IS,_OP_AND,_AMPERSAND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  2604. [_STARSTAR] );
  2605. function sub_expr(pred_level:Toperator_precedence;accept_equal,typeonly:boolean):tnode;
  2606. {Reads a subexpression while the operators are of the current precedence
  2607. level, or any higher level. Replaces the old term, simpl_expr and
  2608. simpl2_expr.}
  2609. var
  2610. p1,p2 : tnode;
  2611. oldt : Ttoken;
  2612. filepos : tfileposinfo;
  2613. begin
  2614. if pred_level=highest_precedence then
  2615. p1:=factor(false,typeonly)
  2616. else
  2617. p1:=sub_expr(succ(pred_level),true,typeonly);
  2618. repeat
  2619. if (token in [NOTOKEN..last_operator]) and
  2620. (token in operator_levels[pred_level]) and
  2621. ((token<>_EQ) or accept_equal) then
  2622. begin
  2623. oldt:=token;
  2624. filepos:=current_tokenpos;
  2625. consume(token);
  2626. if pred_level=highest_precedence then
  2627. p2:=factor(false,false)
  2628. else
  2629. p2:=sub_expr(succ(pred_level),true,typeonly);
  2630. case oldt of
  2631. _PLUS :
  2632. p1:=caddnode.create(addn,p1,p2);
  2633. _MINUS :
  2634. p1:=caddnode.create(subn,p1,p2);
  2635. _STAR :
  2636. p1:=caddnode.create(muln,p1,p2);
  2637. _SLASH :
  2638. p1:=caddnode.create(slashn,p1,p2);
  2639. _EQ:
  2640. p1:=caddnode.create(equaln,p1,p2);
  2641. _GT :
  2642. p1:=caddnode.create(gtn,p1,p2);
  2643. _LT :
  2644. p1:=caddnode.create(ltn,p1,p2);
  2645. _GTE :
  2646. p1:=caddnode.create(gten,p1,p2);
  2647. _LTE :
  2648. p1:=caddnode.create(lten,p1,p2);
  2649. _SYMDIF :
  2650. p1:=caddnode.create(symdifn,p1,p2);
  2651. _STARSTAR :
  2652. p1:=caddnode.create(starstarn,p1,p2);
  2653. _OP_AS :
  2654. p1:=casnode.create(p1,p2);
  2655. _OP_IN :
  2656. p1:=cinnode.create(p1,p2);
  2657. _OP_IS :
  2658. p1:=cisnode.create(p1,p2);
  2659. _OP_OR,
  2660. _PIPE {macpas only} :
  2661. begin
  2662. p1:=caddnode.create(orn,p1,p2);
  2663. if (oldt = _PIPE) then
  2664. include(p1.flags,nf_short_bool);
  2665. end;
  2666. _OP_AND,
  2667. _AMPERSAND {macpas only} :
  2668. begin
  2669. p1:=caddnode.create(andn,p1,p2);
  2670. if (oldt = _AMPERSAND) then
  2671. include(p1.flags,nf_short_bool);
  2672. end;
  2673. _OP_DIV :
  2674. p1:=cmoddivnode.create(divn,p1,p2);
  2675. _OP_NOT :
  2676. p1:=cnotnode.create(p1);
  2677. _OP_MOD :
  2678. p1:=cmoddivnode.create(modn,p1,p2);
  2679. _OP_SHL :
  2680. p1:=cshlshrnode.create(shln,p1,p2);
  2681. _OP_SHR :
  2682. p1:=cshlshrnode.create(shrn,p1,p2);
  2683. _OP_XOR :
  2684. p1:=caddnode.create(xorn,p1,p2);
  2685. _ASSIGNMENT :
  2686. p1:=cassignmentnode.create(p1,p2);
  2687. _NE :
  2688. p1:=caddnode.create(unequaln,p1,p2);
  2689. end;
  2690. p1.fileinfo:=filepos;
  2691. end
  2692. else
  2693. break;
  2694. until false;
  2695. sub_expr:=p1;
  2696. end;
  2697. function comp_expr(accept_equal,typeonly:boolean):tnode;
  2698. var
  2699. oldafterassignment : boolean;
  2700. p1 : tnode;
  2701. begin
  2702. oldafterassignment:=afterassignment;
  2703. afterassignment:=true;
  2704. p1:=sub_expr(opcompare,accept_equal,typeonly);
  2705. { get the resultdef for this expression }
  2706. if not assigned(p1.resultdef) then
  2707. do_typecheckpass(p1);
  2708. afterassignment:=oldafterassignment;
  2709. comp_expr:=p1;
  2710. end;
  2711. function expr(dotypecheck : boolean) : tnode;
  2712. var
  2713. p1,p2 : tnode;
  2714. filepos : tfileposinfo;
  2715. oldafterassignment,
  2716. updatefpos : boolean;
  2717. begin
  2718. oldafterassignment:=afterassignment;
  2719. p1:=sub_expr(opcompare,true,false);
  2720. { get the resultdef for this expression }
  2721. if not assigned(p1.resultdef) and
  2722. dotypecheck then
  2723. do_typecheckpass(p1);
  2724. filepos:=current_tokenpos;
  2725. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2726. afterassignment:=true;
  2727. updatefpos:=true;
  2728. case token of
  2729. _POINTPOINT :
  2730. begin
  2731. consume(_POINTPOINT);
  2732. p2:=sub_expr(opcompare,true,false);
  2733. p1:=crangenode.create(p1,p2);
  2734. end;
  2735. _ASSIGNMENT :
  2736. begin
  2737. consume(_ASSIGNMENT);
  2738. if (p1.resultdef.typ=procvardef) then
  2739. getprocvardef:=tprocvardef(p1.resultdef);
  2740. p2:=sub_expr(opcompare,true,false);
  2741. if assigned(getprocvardef) then
  2742. handle_procvar(getprocvardef,p2);
  2743. getprocvardef:=nil;
  2744. p1:=cassignmentnode.create(p1,p2);
  2745. end;
  2746. _PLUSASN :
  2747. begin
  2748. consume(_PLUSASN);
  2749. p2:=sub_expr(opcompare,true,false);
  2750. p1:=gen_c_style_operator(addn,p1,p2);
  2751. end;
  2752. _MINUSASN :
  2753. begin
  2754. consume(_MINUSASN);
  2755. p2:=sub_expr(opcompare,true,false);
  2756. p1:=gen_c_style_operator(subn,p1,p2);
  2757. end;
  2758. _STARASN :
  2759. begin
  2760. consume(_STARASN );
  2761. p2:=sub_expr(opcompare,true,false);
  2762. p1:=gen_c_style_operator(muln,p1,p2);
  2763. end;
  2764. _SLASHASN :
  2765. begin
  2766. consume(_SLASHASN );
  2767. p2:=sub_expr(opcompare,true,false);
  2768. p1:=gen_c_style_operator(slashn,p1,p2);
  2769. end;
  2770. else
  2771. updatefpos:=false;
  2772. end;
  2773. { get the resultdef for this expression }
  2774. if not assigned(p1.resultdef) and
  2775. dotypecheck then
  2776. do_typecheckpass(p1);
  2777. afterassignment:=oldafterassignment;
  2778. if updatefpos then
  2779. p1.fileinfo:=filepos;
  2780. expr:=p1;
  2781. end;
  2782. function get_intconst:TConstExprInt;
  2783. {Reads an expression, tries to evalute it and check if it is an integer
  2784. constant. Then the constant is returned.}
  2785. var
  2786. p:tnode;
  2787. begin
  2788. result:=0;
  2789. p:=comp_expr(true,false);
  2790. if not codegenerror then
  2791. begin
  2792. if (p.nodetype<>ordconstn) or
  2793. not(is_integer(p.resultdef)) then
  2794. Message(parser_e_illegal_expression)
  2795. else
  2796. result:=tordconstnode(p).value;
  2797. end;
  2798. p.free;
  2799. end;
  2800. function get_stringconst:string;
  2801. {Reads an expression, tries to evaluate it and checks if it is a string
  2802. constant. Then the constant is returned.}
  2803. var
  2804. p:tnode;
  2805. begin
  2806. get_stringconst:='';
  2807. p:=comp_expr(true,false);
  2808. if p.nodetype<>stringconstn then
  2809. begin
  2810. if (p.nodetype=ordconstn) and is_char(p.resultdef) then
  2811. get_stringconst:=char(int64(tordconstnode(p).value))
  2812. else
  2813. Message(parser_e_illegal_expression);
  2814. end
  2815. else
  2816. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2817. p.free;
  2818. end;
  2819. end.