pexpr.pas 106 KB

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