pexpr.pas 109 KB

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