pexpr.pas 109 KB

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