pexpr.pas 115 KB

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