pexpr.pas 120 KB

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