pexpr.pas 107 KB

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