pexpr.pas 116 KB

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