pdecsub.pas 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  3. Does the parsing of the procedures/functions
  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 pdecsub;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. tokens,symconst,symtype,symdef,symsym;
  22. type
  23. tpdflag=(
  24. pd_body, { directive needs a body }
  25. pd_implemen, { directive can be used implementation section }
  26. pd_interface, { directive can be used interface section }
  27. pd_object, { directive can be used object declaration }
  28. pd_procvar, { directive can be used procvar declaration }
  29. pd_notobject, { directive can not be used object declaration }
  30. pd_notobjintf, { directive can not be used interface declaration }
  31. pd_notprocvar, { directive can not be used procvar declaration }
  32. pd_dispinterface,{ directive can be used with dispinterface methods }
  33. pd_cppobject { directive can be used with cppclass }
  34. );
  35. tpdflags=set of tpdflag;
  36. function check_proc_directive(isprocvar:boolean):boolean;
  37. procedure insert_funcret_local(pd:tprocdef);
  38. function proc_add_definition(var currpd:tprocdef):boolean;
  39. function proc_get_importname(pd:tprocdef):string;
  40. procedure proc_set_mangledname(pd:tprocdef);
  41. procedure handle_calling_convention(pd:tabstractprocdef);
  42. procedure parse_parameter_dec(pd:tabstractprocdef);
  43. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  44. procedure parse_var_proc_directives(sym:tsym);
  45. procedure parse_object_proc_directives(pd:tabstractprocdef);
  46. function parse_proc_head(aclass:tobjectdef;potype:tproctypeoption;var pd:tprocdef):boolean;
  47. function parse_proc_dec(aclass:tobjectdef):tprocdef;
  48. implementation
  49. uses
  50. SysUtils,
  51. { common }
  52. cutils,cclasses,
  53. { global }
  54. globtype,globals,verbose,constexp,
  55. systems,
  56. cpuinfo,
  57. { symtable }
  58. symbase,symtable,defutil,defcmp,paramgr,cpupara,
  59. { pass 1 }
  60. fmodule,node,htypechk,
  61. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  62. { parser }
  63. scanner,
  64. pbase,pexpr,ptype,pdecl
  65. ;
  66. const
  67. { Please leave this here, this module should NOT use
  68. these variables.
  69. Declaring it as string here results in an error when compiling (PFV) }
  70. current_procinfo = 'error';
  71. procedure insert_funcret_para(pd:tabstractprocdef);
  72. var
  73. storepos : tfileposinfo;
  74. vs : tparavarsym;
  75. paranr : word;
  76. begin
  77. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  78. not is_void(pd.returndef) and
  79. paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
  80. begin
  81. storepos:=current_tokenpos;
  82. if pd.typ=procdef then
  83. current_tokenpos:=tprocdef(pd).fileinfo;
  84. { For left to right add it at the end to be delphi compatible }
  85. if pd.proccalloption in (pushleftright_pocalls+[pocall_safecall]) then
  86. paranr:=paranr_result_leftright
  87. else
  88. paranr:=paranr_result;
  89. { Generate result variable accessing function result }
  90. vs:=tparavarsym.create('$result',paranr,vs_var,pd.returndef,[vo_is_funcret,vo_is_hidden_para]);
  91. vs.symoptions:=[sp_public];
  92. pd.parast.insert(vs);
  93. { Store the this symbol as funcretsym for procedures }
  94. if pd.typ=procdef then
  95. tprocdef(pd).funcretsym:=vs;
  96. current_tokenpos:=storepos;
  97. end;
  98. end;
  99. procedure insert_parentfp_para(pd:tabstractprocdef);
  100. var
  101. storepos : tfileposinfo;
  102. vs : tparavarsym;
  103. begin
  104. if pd.parast.symtablelevel>normal_function_level then
  105. begin
  106. storepos:=current_tokenpos;
  107. if pd.typ=procdef then
  108. current_tokenpos:=tprocdef(pd).fileinfo;
  109. { Generate result variable accessing function result, it
  110. can't be put in a register since it must be accessable
  111. from the framepointer }
  112. vs:=tparavarsym.create('$parentfp',paranr_parentfp,vs_value
  113. ,voidpointertype,[vo_is_parentfp,vo_is_hidden_para]);
  114. vs.varregable:=vr_none;
  115. vs.symoptions:=[sp_public];
  116. pd.parast.insert(vs);
  117. current_tokenpos:=storepos;
  118. end;
  119. end;
  120. procedure insert_self_and_vmt_para(pd:tabstractprocdef);
  121. var
  122. storepos : tfileposinfo;
  123. vs : tparavarsym;
  124. hdef : tdef;
  125. vsp : tvarspez;
  126. begin
  127. if (pd.typ=procvardef) and
  128. pd.is_methodpointer then
  129. begin
  130. { Generate self variable }
  131. vs:=tparavarsym.create('$self',paranr_self,vs_value,voidpointertype,[vo_is_self,vo_is_hidden_para]);
  132. vs.symoptions:=[sp_public];
  133. pd.parast.insert(vs);
  134. end
  135. else
  136. begin
  137. if (pd.typ=procdef) and
  138. assigned(tprocdef(pd)._class) and
  139. (pd.parast.symtablelevel=normal_function_level) then
  140. begin
  141. { static class methods have no hidden self/vmt pointer }
  142. if (po_staticmethod in pd.procoptions) and
  143. (po_classmethod in pd.procoptions) then
  144. exit;
  145. storepos:=current_tokenpos;
  146. current_tokenpos:=tprocdef(pd).fileinfo;
  147. { Generate VMT variable for constructor/destructor }
  148. if (pd.proctypeoption in [potype_constructor,potype_destructor]) and not(is_cppclass(tprocdef(pd)._class)) then
  149. begin
  150. { can't use classrefdef as type because inheriting
  151. will then always file because of a type mismatch }
  152. vs:=tparavarsym.create('$vmt',paranr_vmt,vs_value,voidpointertype,[vo_is_vmt,vo_is_hidden_para]);
  153. vs.symoptions:=[sp_public];
  154. pd.parast.insert(vs);
  155. end;
  156. { Generate self variable, for classes we need
  157. to use the generic voidpointer to be compatible with
  158. methodpointers }
  159. vsp:=vs_value;
  160. if (po_staticmethod in pd.procoptions) or
  161. (po_classmethod in pd.procoptions) then
  162. hdef:=tclassrefdef.create(tprocdef(pd)._class)
  163. else
  164. begin
  165. if is_object(tprocdef(pd)._class) then
  166. vsp:=vs_var;
  167. hdef:=tprocdef(pd)._class;
  168. end;
  169. vs:=tparavarsym.create('$self',paranr_self,vsp,hdef,[vo_is_self,vo_is_hidden_para]);
  170. vs.symoptions:=[sp_public];
  171. pd.parast.insert(vs);
  172. current_tokenpos:=storepos;
  173. end;
  174. end;
  175. end;
  176. procedure insert_funcret_local(pd:tprocdef);
  177. var
  178. storepos : tfileposinfo;
  179. vs : tlocalvarsym;
  180. aliasvs : tabsolutevarsym;
  181. sl : tpropaccesslist;
  182. hs : string;
  183. begin
  184. { The result from constructors and destructors can't be accessed directly }
  185. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  186. not is_void(pd.returndef) then
  187. begin
  188. storepos:=current_tokenpos;
  189. current_tokenpos:=pd.fileinfo;
  190. { We need to insert a varsym for the result in the localst
  191. when it is returning in a register }
  192. if not paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
  193. begin
  194. vs:=tlocalvarsym.create('$result',vs_value,pd.returndef,[vo_is_funcret]);
  195. pd.localst.insert(vs);
  196. pd.funcretsym:=vs;
  197. end;
  198. { insert the name of the procedure as alias for the function result,
  199. we can't use realname because that will not work for compilerprocs
  200. as the name is lowercase and unreachable from the code }
  201. if assigned(pd.resultname) then
  202. hs:=pd.resultname^
  203. else
  204. hs:=pd.procsym.name;
  205. sl:=tpropaccesslist.create;
  206. sl.addsym(sl_load,pd.funcretsym);
  207. aliasvs:=tabsolutevarsym.create_ref(hs,pd.returndef,sl);
  208. include(aliasvs.varoptions,vo_is_funcret);
  209. tlocalsymtable(pd.localst).insert(aliasvs);
  210. { insert result also if support is on }
  211. if (m_result in current_settings.modeswitches) then
  212. begin
  213. sl:=tpropaccesslist.create;
  214. sl.addsym(sl_load,pd.funcretsym);
  215. aliasvs:=tabsolutevarsym.create_ref('RESULT',pd.returndef,sl);
  216. include(aliasvs.varoptions,vo_is_funcret);
  217. include(aliasvs.varoptions,vo_is_result);
  218. tlocalsymtable(pd.localst).insert(aliasvs);
  219. end;
  220. current_tokenpos:=storepos;
  221. end;
  222. end;
  223. procedure insert_hidden_para(p:TObject;arg:pointer);
  224. var
  225. hvs : tparavarsym;
  226. pd : tabstractprocdef absolute arg;
  227. begin
  228. if (tsym(p).typ<>paravarsym) then
  229. exit;
  230. with tparavarsym(p) do
  231. begin
  232. { We need a local copy for a value parameter when only the
  233. address is pushed. Open arrays and Array of Const are
  234. an exception because they are allocated at runtime and the
  235. address that is pushed is patched }
  236. if (varspez=vs_value) and
  237. paramanager.push_addr_param(varspez,vardef,pd.proccalloption) and
  238. not(is_open_array(vardef) or
  239. is_array_of_const(vardef)) then
  240. include(varoptions,vo_has_local_copy);
  241. { needs high parameter ? }
  242. if paramanager.push_high_param(varspez,vardef,pd.proccalloption) then
  243. begin
  244. hvs:=tparavarsym.create('$high'+name,paranr+1,vs_const,sinttype,[vo_is_high_para,vo_is_hidden_para]);
  245. hvs.symoptions:=[sp_public];
  246. owner.insert(hvs);
  247. end
  248. else
  249. begin
  250. { Give a warning that cdecl routines does not include high()
  251. support }
  252. if (pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) and
  253. paramanager.push_high_param(varspez,vardef,pocall_default) then
  254. begin
  255. if is_open_string(vardef) then
  256. Message(parser_w_cdecl_no_openstring);
  257. if not (po_external in pd.procoptions) then
  258. Message(parser_w_cdecl_has_no_high);
  259. end;
  260. if (vardef.typ=formaldef) and (Tformaldef(vardef).typed) then
  261. begin
  262. hvs:=tparavarsym.create('$typinfo'+name,paranr+1,vs_const,voidpointertype,
  263. [vo_is_typinfo_para,vo_is_hidden_para]);
  264. owner.insert(hvs);
  265. end;
  266. end;
  267. end;
  268. end;
  269. procedure check_c_para(pd:Tabstractprocdef);
  270. var
  271. i,
  272. lastparaidx : longint;
  273. sym : TSym;
  274. begin
  275. lastparaidx:=pd.parast.SymList.Count-1;
  276. for i:=0 to pd.parast.SymList.Count-1 do
  277. begin
  278. sym:=tsym(pd.parast.SymList[i]);
  279. if (sym.typ=paravarsym) and
  280. (tparavarsym(sym).vardef.typ=arraydef) then
  281. begin
  282. if not is_variant_array(tparavarsym(sym).vardef) and
  283. not is_array_of_const(tparavarsym(sym).vardef) and
  284. (tparavarsym(sym).varspez<>vs_var) then
  285. Message(parser_h_c_arrays_are_references);
  286. if is_array_of_const(tparavarsym(sym).vardef) and
  287. (i<lastparaidx) and
  288. (tsym(pd.parast.SymList[i+1]).typ=paravarsym) and
  289. not(vo_is_high_para in tparavarsym(pd.parast.SymList[i+1]).varoptions) then
  290. Message(parser_e_C_array_of_const_must_be_last);
  291. end;
  292. end;
  293. end;
  294. procedure check_msg_para(p:TObject;arg:pointer);
  295. begin
  296. if (tsym(p).typ<>paravarsym) then
  297. exit;
  298. with tparavarsym(p) do
  299. begin
  300. { Count parameters }
  301. if (paranr>=10) then
  302. inc(plongint(arg)^);
  303. { First parameter must be var }
  304. if (paranr=10) and
  305. (varspez<>vs_var) then
  306. Message(parser_e_ill_msg_param);
  307. end;
  308. end;
  309. procedure set_addr_param_regable(p:TObject;arg:pointer);
  310. begin
  311. if (tsym(p).typ<>paravarsym) then
  312. exit;
  313. with tparavarsym(p) do
  314. begin
  315. if not vardef.needs_inittable and
  316. paramanager.push_addr_param(varspez,vardef,tprocdef(arg).proccalloption) then
  317. varregable:=vr_intreg;
  318. end;
  319. end;
  320. procedure parse_parameter_dec(pd:tabstractprocdef);
  321. {
  322. handle_procvar needs the same changes
  323. }
  324. type
  325. tppv = (pv_none,pv_proc,pv_func);
  326. var
  327. sc : TFPObjectList;
  328. hdef : tdef;
  329. arrayelementdef : tdef;
  330. vs : tparavarsym;
  331. i : longint;
  332. srsym : tsym;
  333. pv : tprocvardef;
  334. varspez : Tvarspez;
  335. defaultvalue : tconstsym;
  336. defaultrequired : boolean;
  337. old_object_option : tsymoptions;
  338. old_block_type : tblock_type;
  339. currparast : tparasymtable;
  340. parseprocvar : tppv;
  341. explicit_paraloc : boolean;
  342. locationstr : string;
  343. paranr : integer;
  344. dummytype : ttypesym;
  345. begin
  346. old_object_option:=current_object_option;
  347. old_block_type:=block_type;
  348. explicit_paraloc:=false;
  349. consume(_LKLAMMER);
  350. { Delphi/Kylix supports nonsense like }
  351. { procedure p(); }
  352. if try_to_consume(_RKLAMMER) and
  353. not(m_tp7 in current_settings.modeswitches) then
  354. exit;
  355. { parsing a proc or procvar ? }
  356. currparast:=tparasymtable(pd.parast);
  357. { reset }
  358. sc:=TFPObjectList.create(false);
  359. defaultrequired:=false;
  360. paranr:=0;
  361. { the variables are always public }
  362. current_object_option:=[sp_public];
  363. inc(testcurobject);
  364. block_type:=bt_type;
  365. repeat
  366. parseprocvar:=pv_none;
  367. if try_to_consume(_VAR) then
  368. varspez:=vs_var
  369. else
  370. if try_to_consume(_CONST) then
  371. varspez:=vs_const
  372. else
  373. if (m_out in current_settings.modeswitches) and
  374. try_to_consume(_OUT) then
  375. varspez:=vs_out
  376. else
  377. if (m_mac in current_settings.modeswitches) and
  378. try_to_consume(_POINTPOINTPOINT) then
  379. begin
  380. include(pd.procoptions,po_varargs);
  381. break;
  382. end
  383. else
  384. if (m_mac in current_settings.modeswitches) and
  385. try_to_consume(_PROCEDURE) then
  386. begin
  387. parseprocvar:=pv_proc;
  388. varspez:=vs_const;
  389. end
  390. else
  391. if (m_mac in current_settings.modeswitches) and
  392. try_to_consume(_FUNCTION) then
  393. begin
  394. parseprocvar:=pv_func;
  395. varspez:=vs_const;
  396. end
  397. else
  398. varspez:=vs_value;
  399. defaultvalue:=nil;
  400. hdef:=nil;
  401. { read identifiers and insert with error type }
  402. sc.clear;
  403. repeat
  404. inc(paranr);
  405. vs:=tparavarsym.create(orgpattern,paranr*10,varspez,generrordef,[]);
  406. currparast.insert(vs);
  407. if assigned(vs.owner) then
  408. sc.add(vs)
  409. else
  410. vs.free;
  411. consume(_ID);
  412. until not try_to_consume(_COMMA);
  413. locationstr:='';
  414. { macpas anonymous procvar }
  415. if parseprocvar<>pv_none then
  416. begin
  417. pv:=tprocvardef.create(normal_function_level);
  418. if token=_LKLAMMER then
  419. parse_parameter_dec(pv);
  420. if parseprocvar=pv_func then
  421. begin
  422. consume(_COLON);
  423. single_type(pv.returndef,false);
  424. end;
  425. hdef:=pv;
  426. { possible proc directives }
  427. if check_proc_directive(true) then
  428. begin
  429. dummytype:=ttypesym.create('unnamed',hdef);
  430. parse_var_proc_directives(tsym(dummytype));
  431. dummytype.typedef:=nil;
  432. hdef.typesym:=nil;
  433. dummytype.free;
  434. end;
  435. { Add implicit hidden parameters and function result }
  436. handle_calling_convention(pv);
  437. end
  438. else
  439. { read type declaration, force reading for value and const paras }
  440. if (token=_COLON) or (varspez=vs_value) then
  441. begin
  442. consume(_COLON);
  443. { check for an open array }
  444. if token=_ARRAY then
  445. begin
  446. consume(_ARRAY);
  447. consume(_OF);
  448. { define range and type of range }
  449. hdef:=tarraydef.create(0,-1,s32inttype);
  450. { array of const ? }
  451. if (token=_CONST) and (m_objpas in current_settings.modeswitches) then
  452. begin
  453. consume(_CONST);
  454. srsym:=search_system_type('TVARREC');
  455. tarraydef(hdef).elementdef:=ttypesym(srsym).typedef;
  456. include(tarraydef(hdef).arrayoptions,ado_IsArrayOfConst);
  457. end
  458. else
  459. begin
  460. { define field type }
  461. single_type(arrayelementdef,false);
  462. tarraydef(hdef).elementdef:=arrayelementdef;
  463. end;
  464. end
  465. else
  466. begin
  467. if (m_mac in current_settings.modeswitches) then
  468. try_to_consume(_UNIV); {currently does nothing}
  469. if try_to_consume(_TYPE) then
  470. hdef:=ctypedformaltype
  471. else
  472. single_type(hdef,false);
  473. { open string ? }
  474. if (varspez in [vs_out,vs_var]) and
  475. (cs_openstring in current_settings.moduleswitches) and
  476. is_shortstring(hdef) then
  477. hdef:=openshortstringtype;
  478. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  479. begin
  480. if (idtoken=_LOCATION) then
  481. begin
  482. consume(_LOCATION);
  483. locationstr:=pattern;
  484. consume(_CSTRING);
  485. end
  486. else
  487. begin
  488. if explicit_paraloc then
  489. Message(parser_e_paraloc_all_paras);
  490. locationstr:='';
  491. end;
  492. end
  493. else
  494. locationstr:='';
  495. { default parameter }
  496. if (m_default_para in current_settings.modeswitches) then
  497. begin
  498. if try_to_consume(_EQUAL) then
  499. begin
  500. vs:=tparavarsym(sc[0]);
  501. if sc.count>1 then
  502. Message(parser_e_default_value_only_one_para);
  503. { prefix 'def' to the parameter name }
  504. defaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo);
  505. if assigned(defaultvalue) then
  506. begin
  507. include(defaultvalue.symoptions,sp_internal);
  508. pd.parast.insert(defaultvalue);
  509. end;
  510. defaultrequired:=true;
  511. end
  512. else
  513. begin
  514. if defaultrequired then
  515. Message1(parser_e_default_value_expected_for_para,vs.name);
  516. end;
  517. end;
  518. end;
  519. end
  520. else
  521. hdef:=cformaltype;
  522. { File types are only allowed for var and out parameters }
  523. if (hdef.typ=filedef) and
  524. not(varspez in [vs_out,vs_var]) then
  525. CGMessage(cg_e_file_must_call_by_reference);
  526. for i:=0 to sc.count-1 do
  527. begin
  528. vs:=tparavarsym(sc[i]);
  529. { update varsym }
  530. vs.vardef:=hdef;
  531. vs.defaultconstsym:=defaultvalue;
  532. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  533. begin
  534. if locationstr<>'' then
  535. begin
  536. if sc.count>1 then
  537. Message(parser_e_paraloc_only_one_para);
  538. if (paranr>1) and not(explicit_paraloc) then
  539. Message(parser_e_paraloc_all_paras);
  540. explicit_paraloc:=true;
  541. include(vs.varoptions,vo_has_explicit_paraloc);
  542. if not(paramanager.parseparaloc(vs,upper(locationstr))) then
  543. message(parser_e_illegal_explicit_paraloc);
  544. end
  545. else
  546. if explicit_paraloc then
  547. Message(parser_e_paraloc_all_paras);
  548. end;
  549. end;
  550. until not try_to_consume(_SEMICOLON);
  551. if explicit_paraloc then
  552. begin
  553. pd.has_paraloc_info:=true;
  554. include(pd.procoptions,po_explicitparaloc);
  555. end;
  556. { remove parasymtable from stack }
  557. sc.free;
  558. { reset object options }
  559. dec(testcurobject);
  560. current_object_option:=old_object_option;
  561. block_type:=old_block_type;
  562. consume(_RKLAMMER);
  563. end;
  564. function parse_proc_head(aclass:tobjectdef;potype:tproctypeoption;var pd:tprocdef):boolean;
  565. var
  566. hs : string;
  567. orgsp,sp : TIDString;
  568. srsym : tsym;
  569. srsymtable : TSymtable;
  570. checkstack : psymtablestackitem;
  571. storepos,
  572. procstartfilepos : tfileposinfo;
  573. searchagain : boolean;
  574. st,
  575. genericst : TSymtable;
  576. aprocsym : tprocsym;
  577. popclass : boolean;
  578. ImplIntf : TImplementedInterface;
  579. old_parse_generic : boolean;
  580. begin
  581. { Save the position where this procedure really starts }
  582. procstartfilepos:=current_tokenpos;
  583. old_parse_generic:=parse_generic;
  584. result:=false;
  585. pd:=nil;
  586. aprocsym:=nil;
  587. if (potype=potype_operator) then
  588. begin
  589. sp:=overloaded_names[optoken];
  590. orgsp:=sp;
  591. end
  592. else
  593. begin
  594. sp:=pattern;
  595. orgsp:=orgpattern;
  596. consume(_ID);
  597. end;
  598. { examine interface map: function/procedure iname.functionname=locfuncname }
  599. if assigned(aclass) and
  600. assigned(aclass.ImplementedInterfaces) and
  601. (aclass.ImplementedInterfaces.count>0) and
  602. try_to_consume(_POINT) then
  603. begin
  604. storepos:=current_tokenpos;
  605. current_tokenpos:=procstartfilepos;
  606. { get interface syms}
  607. searchsym(sp,srsym,srsymtable);
  608. if not assigned(srsym) then
  609. begin
  610. identifier_not_found(orgsp);
  611. srsym:=generrorsym;
  612. end;
  613. current_tokenpos:=storepos;
  614. { qualifier is interface? }
  615. ImplIntf:=nil;
  616. if (srsym.typ=typesym) and
  617. (ttypesym(srsym).typedef.typ=objectdef) then
  618. ImplIntf:=aclass.find_implemented_interface(tobjectdef(ttypesym(srsym).typedef));
  619. if ImplIntf=nil then
  620. Message(parser_e_interface_id_expected);
  621. consume(_ID);
  622. { Create unique name <interface>.<method> }
  623. hs:=sp+'.'+pattern;
  624. consume(_EQUAL);
  625. if assigned(ImplIntf) and
  626. (token=_ID) then
  627. ImplIntf.AddMapping(hs,pattern);
  628. consume(_ID);
  629. result:=true;
  630. exit;
  631. end;
  632. { method ? }
  633. if not assigned(aclass) and
  634. (potype<>potype_operator) and
  635. (symtablestack.top.symtablelevel=main_program_level) and
  636. try_to_consume(_POINT) then
  637. begin
  638. { search for object name }
  639. storepos:=current_tokenpos;
  640. current_tokenpos:=procstartfilepos;
  641. searchsym(sp,srsym,srsymtable);
  642. if not assigned(srsym) then
  643. begin
  644. identifier_not_found(orgsp);
  645. srsym:=generrorsym;
  646. end;
  647. current_tokenpos:=storepos;
  648. { consume proc name }
  649. sp:=pattern;
  650. orgsp:=orgpattern;
  651. procstartfilepos:=current_tokenpos;
  652. consume(_ID);
  653. { qualifier is class name ? }
  654. if (srsym.typ=typesym) and
  655. (ttypesym(srsym).typedef.typ=objectdef) then
  656. begin
  657. aclass:=tobjectdef(ttypesym(srsym).typedef);
  658. srsym:=tsym(aclass.symtable.Find(sp));
  659. if assigned(srsym) then
  660. begin
  661. if srsym.typ=procsym then
  662. aprocsym:=tprocsym(srsym)
  663. else
  664. begin
  665. { we use a different error message for tp7 so it looks more compatible }
  666. if (m_fpc in current_settings.modeswitches) then
  667. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  668. else
  669. Message(parser_e_methode_id_expected);
  670. { rename the name to an unique name to avoid an
  671. error when inserting the symbol in the symtable }
  672. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  673. end;
  674. end
  675. else
  676. begin
  677. Message(parser_e_methode_id_expected);
  678. { recover by making it a normal procedure instead of method }
  679. aclass:=nil;
  680. end;
  681. end
  682. else
  683. Message(parser_e_class_id_expected);
  684. end
  685. else
  686. begin
  687. { check for constructor/destructor which is not allowed here }
  688. if (not parse_only) and
  689. (potype in [potype_constructor,potype_destructor]) then
  690. Message(parser_e_constructors_always_objects);
  691. repeat
  692. searchagain:=false;
  693. current_tokenpos:=procstartfilepos;
  694. srsymtable:=symtablestack.top;
  695. srsym:=tsym(srsymtable.Find(sp));
  696. { Also look in the globalsymtable if we didn't found
  697. the symbol in the localsymtable }
  698. if not assigned(srsym) and
  699. not(parse_only) and
  700. (srsymtable=current_module.localsymtable) and
  701. assigned(current_module.globalsymtable) then
  702. srsym:=tsym(current_module.globalsymtable.Find(sp));
  703. { Check if overloaded is a procsym }
  704. if assigned(srsym) then
  705. begin
  706. if srsym.typ=procsym then
  707. aprocsym:=tprocsym(srsym)
  708. else
  709. begin
  710. { when the other symbol is a unit symbol then hide the unit
  711. symbol, this is not supported in tp7 }
  712. if not(m_tp7 in current_settings.modeswitches) and
  713. (srsym.typ=unitsym) then
  714. begin
  715. HideSym(srsym);
  716. searchagain:=true;
  717. end
  718. else
  719. begin
  720. { we use a different error message for tp7 so it looks more compatible }
  721. if (m_fpc in current_settings.modeswitches) then
  722. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  723. else
  724. Message1(sym_e_duplicate_id,srsym.realname);
  725. { rename the name to an unique name to avoid an
  726. error when inserting the symbol in the symtable }
  727. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  728. end;
  729. end;
  730. end;
  731. until not searchagain;
  732. end;
  733. { test again if assigned, it can be reset to recover }
  734. if not assigned(aprocsym) then
  735. begin
  736. { create a new procsym and set the real filepos }
  737. current_tokenpos:=procstartfilepos;
  738. { for operator we have only one procsym for each overloaded
  739. operation }
  740. if (potype=potype_operator) then
  741. begin
  742. Aprocsym:=Tprocsym(symtablestack.top.Find(sp));
  743. if Aprocsym=nil then
  744. Aprocsym:=tprocsym.create('$'+sp);
  745. end
  746. else
  747. aprocsym:=tprocsym.create(orgsp);
  748. symtablestack.top.insert(aprocsym);
  749. end;
  750. { to get the correct symtablelevel we must ignore ObjectSymtables }
  751. st:=nil;
  752. checkstack:=symtablestack.stack;
  753. while assigned(checkstack) do
  754. begin
  755. st:=checkstack^.symtable;
  756. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  757. break;
  758. checkstack:=checkstack^.next;
  759. end;
  760. pd:=tprocdef.create(st.symtablelevel+1);
  761. pd._class:=aclass;
  762. pd.procsym:=aprocsym;
  763. pd.proctypeoption:=potype;
  764. { methods inherit df_generic or df_specialization from the objectdef }
  765. if assigned(pd._class) and
  766. (pd.parast.symtablelevel=normal_function_level) then
  767. begin
  768. if (df_generic in pd._class.defoptions) then
  769. begin
  770. include(pd.defoptions,df_generic);
  771. parse_generic:=true;
  772. end;
  773. if (df_specialization in pd._class.defoptions) then
  774. begin
  775. include(pd.defoptions,df_specialization);
  776. { Find corresponding genericdef, we need it later to
  777. replay the tokens to generate the body }
  778. if not assigned(pd._class.genericdef) then
  779. internalerror(200512113);
  780. genericst:=pd._class.genericdef.GetSymtable(gs_record);
  781. if not assigned(genericst) then
  782. internalerror(200512114);
  783. { We are parsing the same objectdef, the def index numbers
  784. are the same }
  785. pd.genericdef:=tstoreddef(genericst.DefList[pd.owner.DefList.IndexOf(pd)]);
  786. if not assigned(pd.genericdef) or
  787. (pd.genericdef.typ<>procdef) then
  788. internalerror(200512115);
  789. end;
  790. end;
  791. { methods need to be exported }
  792. if assigned(aclass) and
  793. (
  794. (symtablestack.top.symtabletype=ObjectSymtable) or
  795. (symtablestack.top.symtablelevel=main_program_level)
  796. ) then
  797. include(pd.procoptions,po_global);
  798. { symbol options that need to be kept per procdef }
  799. pd.fileinfo:=procstartfilepos;
  800. pd.symoptions:=current_object_option;
  801. { parse parameters }
  802. if token=_LKLAMMER then
  803. begin
  804. { Add ObjectSymtable to be able to find generic type definitions }
  805. popclass:=false;
  806. if assigned(pd._class) and
  807. (pd.parast.symtablelevel=normal_function_level) and
  808. (symtablestack.top.symtabletype<>ObjectSymtable) then
  809. begin
  810. symtablestack.push(pd._class.symtable);
  811. popclass:=true;
  812. end;
  813. { Add parameter symtable }
  814. if pd.parast.symtabletype<>staticsymtable then
  815. symtablestack.push(pd.parast);
  816. parse_parameter_dec(pd);
  817. if pd.parast.symtabletype<>staticsymtable then
  818. symtablestack.pop(pd.parast);
  819. if popclass then
  820. symtablestack.pop(pd._class.symtable);
  821. end;
  822. parse_generic:=old_parse_generic;
  823. result:=true;
  824. end;
  825. function parse_proc_dec(aclass:tobjectdef):tprocdef;
  826. var
  827. pd : tprocdef;
  828. isclassmethod : boolean;
  829. locationstr: string;
  830. popclass : boolean;
  831. begin
  832. locationstr:='';
  833. pd:=nil;
  834. isclassmethod:=false;
  835. { read class method }
  836. if try_to_consume(_CLASS) then
  837. begin
  838. { class method only allowed for procedures and functions }
  839. if not(token in [_FUNCTION,_PROCEDURE]) then
  840. Message(parser_e_procedure_or_function_expected);
  841. if is_interface(aclass) then
  842. Message(parser_e_no_static_method_in_interfaces)
  843. else
  844. isclassmethod:=true;
  845. end;
  846. case token of
  847. _FUNCTION :
  848. begin
  849. consume(_FUNCTION);
  850. if parse_proc_head(aclass,potype_function,pd) then
  851. begin
  852. { pd=nil when it is a interface mapping }
  853. if assigned(pd) then
  854. begin
  855. if try_to_consume(_COLON) then
  856. begin
  857. inc(testcurobject);
  858. { Add ObjectSymtable to be able to find generic type definitions }
  859. popclass:=false;
  860. if assigned(pd._class) and
  861. (pd.parast.symtablelevel=normal_function_level) and
  862. (symtablestack.top.symtabletype<>ObjectSymtable) then
  863. begin
  864. symtablestack.push(pd._class.symtable);
  865. popclass:=true;
  866. end;
  867. single_type(pd.returndef,false);
  868. if popclass then
  869. symtablestack.pop(pd._class.symtable);
  870. dec(testcurobject);
  871. if (target_info.system in [system_m68k_amiga]) then
  872. begin
  873. if (idtoken=_LOCATION) then
  874. begin
  875. if po_explicitparaloc in pd.procoptions then
  876. begin
  877. consume(_LOCATION);
  878. locationstr:=pattern;
  879. consume(_CSTRING);
  880. end
  881. else
  882. { I guess this needs a new message... (KB) }
  883. Message(parser_e_paraloc_all_paras);
  884. end
  885. else
  886. begin
  887. if po_explicitparaloc in pd.procoptions then
  888. { assign default locationstr, if none specified }
  889. { and we've arguments with explicit paraloc }
  890. locationstr:='D0';
  891. end;
  892. end;
  893. end
  894. else
  895. begin
  896. if (
  897. parse_only and
  898. not(is_interface(pd._class))
  899. ) or
  900. (m_repeat_forward in current_settings.modeswitches) then
  901. begin
  902. consume(_COLON);
  903. consume_all_until(_SEMICOLON);
  904. end;
  905. end;
  906. if isclassmethod then
  907. include(pd.procoptions,po_classmethod);
  908. end;
  909. end
  910. else
  911. begin
  912. { recover }
  913. consume(_COLON);
  914. consume_all_until(_SEMICOLON);
  915. end;
  916. end;
  917. _PROCEDURE :
  918. begin
  919. consume(_PROCEDURE);
  920. if parse_proc_head(aclass,potype_procedure,pd) then
  921. begin
  922. { pd=nil when it is a interface mapping }
  923. if assigned(pd) then
  924. begin
  925. pd.returndef:=voidtype;
  926. if isclassmethod then
  927. include(pd.procoptions,po_classmethod);
  928. end;
  929. end;
  930. end;
  931. _CONSTRUCTOR :
  932. begin
  933. consume(_CONSTRUCTOR);
  934. parse_proc_head(aclass,potype_constructor,pd);
  935. if assigned(pd) and
  936. assigned(pd._class) then
  937. begin
  938. { Set return type, class constructors return the
  939. created instance, object constructors return boolean }
  940. if is_class(pd._class) then
  941. pd.returndef:=pd._class
  942. else
  943. {$ifdef CPU64bitaddr}
  944. pd.returndef:=bool64type;
  945. {$else CPU64bitaddr}
  946. pd.returndef:=bool32type;
  947. {$endif CPU64bitaddr}
  948. end;
  949. end;
  950. _DESTRUCTOR :
  951. begin
  952. consume(_DESTRUCTOR);
  953. parse_proc_head(aclass,potype_destructor,pd);
  954. if assigned(pd) then
  955. pd.returndef:=voidtype;
  956. end;
  957. _OPERATOR :
  958. begin
  959. consume(_OPERATOR);
  960. if (token in [first_overloaded..last_overloaded]) then
  961. begin
  962. optoken:=token;
  963. end
  964. else
  965. begin
  966. case token of
  967. _CARET:
  968. Message1(parser_e_overload_operator_failed,'**');
  969. _UNEQUAL:
  970. Message1(parser_e_overload_operator_failed,'=');
  971. else
  972. Message1(parser_e_overload_operator_failed,'');
  973. end;
  974. { Use the dummy NOTOKEN that is also declared
  975. for the overloaded_operator[] }
  976. optoken:=NOTOKEN;
  977. end;
  978. consume(token);
  979. parse_proc_head(aclass,potype_operator,pd);
  980. if assigned(pd) then
  981. begin
  982. if pd.parast.symtablelevel>normal_function_level then
  983. Message(parser_e_no_local_operator);
  984. if token<>_ID then
  985. begin
  986. if not(m_result in current_settings.modeswitches) then
  987. consume(_ID);
  988. end
  989. else
  990. begin
  991. pd.resultname:=stringdup(orgpattern);
  992. consume(_ID);
  993. end;
  994. if not try_to_consume(_COLON) then
  995. begin
  996. consume(_COLON);
  997. pd.returndef:=generrordef;
  998. consume_all_until(_SEMICOLON);
  999. end
  1000. else
  1001. begin
  1002. single_type(pd.returndef,false);
  1003. if (optoken in [_EQUAL,_GT,_LT,_GTE,_LTE]) and
  1004. ((pd.returndef.typ<>orddef) or
  1005. (torddef(pd.returndef).ordtype<>pasbool)) then
  1006. Message(parser_e_comparative_operator_return_boolean);
  1007. if (optoken=_ASSIGNMENT) and
  1008. equal_defs(pd.returndef,tparavarsym(pd.parast.SymList[0]).vardef) then
  1009. message(parser_e_no_such_assignment)
  1010. else if not isoperatoracceptable(pd,optoken) then
  1011. Message(parser_e_overload_impossible);
  1012. end;
  1013. end
  1014. else
  1015. begin
  1016. { recover }
  1017. try_to_consume(_ID);
  1018. consume(_COLON);
  1019. consume_all_until(_SEMICOLON);
  1020. end;
  1021. end;
  1022. end;
  1023. { file types can't be function results }
  1024. if assigned(pd) and
  1025. (pd.returndef.typ=filedef) then
  1026. message(parser_e_illegal_function_result);
  1027. { support procedure proc stdcall export; }
  1028. if not(check_proc_directive(false)) then
  1029. consume(_SEMICOLON);
  1030. result:=pd;
  1031. if locationstr<>'' then
  1032. begin
  1033. if not(paramanager.parsefuncretloc(pd,upper(locationstr))) then
  1034. { I guess this needs a new message... (KB) }
  1035. message(parser_e_illegal_explicit_paraloc);
  1036. end;
  1037. end;
  1038. {****************************************************************************
  1039. Procedure directive handlers
  1040. ****************************************************************************}
  1041. procedure pd_far(pd:tabstractprocdef);
  1042. begin
  1043. Message1(parser_w_proc_directive_ignored,'FAR');
  1044. end;
  1045. procedure pd_near(pd:tabstractprocdef);
  1046. begin
  1047. Message1(parser_w_proc_directive_ignored,'NEAR');
  1048. end;
  1049. procedure pd_export(pd:tabstractprocdef);
  1050. begin
  1051. if pd.typ<>procdef then
  1052. internalerror(200304264);
  1053. if assigned(tprocdef(pd)._class) then
  1054. Message(parser_e_methods_dont_be_export);
  1055. if pd.parast.symtablelevel>normal_function_level then
  1056. Message(parser_e_dont_nest_export);
  1057. end;
  1058. procedure pd_forward(pd:tabstractprocdef);
  1059. begin
  1060. if pd.typ<>procdef then
  1061. internalerror(200304265);
  1062. tprocdef(pd).forwarddef:=true;
  1063. end;
  1064. procedure pd_alias(pd:tabstractprocdef);
  1065. begin
  1066. if pd.typ<>procdef then
  1067. internalerror(200304266);
  1068. consume(_COLON);
  1069. tprocdef(pd).aliasnames.insert(get_stringconst);
  1070. include(pd.procoptions,po_has_public_name);
  1071. end;
  1072. procedure pd_public(pd:tabstractprocdef);
  1073. begin
  1074. if pd.typ<>procdef then
  1075. internalerror(200304266);
  1076. if try_to_consume(_NAME) then
  1077. begin
  1078. tprocdef(pd).aliasnames.insert(get_stringconst);
  1079. include(pd.procoptions,po_has_public_name);
  1080. end;
  1081. end;
  1082. procedure pd_asmname(pd:tabstractprocdef);
  1083. begin
  1084. if pd.typ<>procdef then
  1085. internalerror(200304267);
  1086. tprocdef(pd).aliasnames.insert(target_info.Cprefix+pattern);
  1087. if token=_CCHAR then
  1088. consume(_CCHAR)
  1089. else
  1090. consume(_CSTRING);
  1091. { we don't need anything else }
  1092. tprocdef(pd).forwarddef:=false;
  1093. end;
  1094. procedure pd_internconst(pd:tabstractprocdef);
  1095. var v:Tconstexprint;
  1096. begin
  1097. if pd.typ<>procdef then
  1098. internalerror(200304268);
  1099. consume(_COLON);
  1100. v:=get_intconst;
  1101. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1102. message(parser_e_range_check_error)
  1103. else
  1104. Tprocdef(pd).extnumber:=longint(v.svalue);
  1105. end;
  1106. procedure pd_internproc(pd:tabstractprocdef);
  1107. var v:Tconstexprint;
  1108. begin
  1109. if pd.typ<>procdef then
  1110. internalerror(200304268);
  1111. consume(_COLON);
  1112. v:=get_intconst;
  1113. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1114. message(parser_e_range_check_error)
  1115. else
  1116. Tprocdef(pd).extnumber:=longint(v.svalue);
  1117. { the proc is defined }
  1118. tprocdef(pd).forwarddef:=false;
  1119. end;
  1120. procedure pd_interrupt(pd:tabstractprocdef);
  1121. begin
  1122. if pd.parast.symtablelevel>normal_function_level then
  1123. Message(parser_e_dont_nest_interrupt);
  1124. end;
  1125. procedure pd_abstract(pd:tabstractprocdef);
  1126. begin
  1127. if pd.typ<>procdef then
  1128. internalerror(200304269);
  1129. if (po_virtualmethod in pd.procoptions) then
  1130. include(pd.procoptions,po_abstractmethod)
  1131. else
  1132. Message(parser_e_only_virtual_methods_abstract);
  1133. { the method is defined }
  1134. tprocdef(pd).forwarddef:=false;
  1135. end;
  1136. procedure pd_virtual(pd:tabstractprocdef);
  1137. {$ifdef WITHDMT}
  1138. var
  1139. pt : tnode;
  1140. {$endif WITHDMT}
  1141. begin
  1142. if pd.typ<>procdef then
  1143. internalerror(2003042610);
  1144. if (pd.proctypeoption=potype_constructor) and
  1145. is_object(tprocdef(pd)._class) then
  1146. Message(parser_e_constructor_cannot_be_not_virtual);
  1147. {$ifdef WITHDMT}
  1148. if is_object(tprocdef(pd)._class) and
  1149. (token<>_SEMICOLON) then
  1150. begin
  1151. { any type of parameter is allowed here! }
  1152. pt:=comp_expr(true);
  1153. if is_constintnode(pt) then
  1154. begin
  1155. include(pd.procoptions,po_msgint);
  1156. pd.messageinf.i:=pt.value;
  1157. end
  1158. else
  1159. Message(parser_e_ill_msg_expr);
  1160. disposetree(pt);
  1161. end;
  1162. {$endif WITHDMT}
  1163. end;
  1164. procedure pd_dispid(pd:tabstractprocdef);
  1165. var pt:Tnode;
  1166. begin
  1167. if pd.typ<>procdef then
  1168. internalerror(200604301);
  1169. pt:=comp_expr(true);
  1170. if is_constintnode(pt) then
  1171. if (Tordconstnode(pt).value<int64(low(longint))) or (Tordconstnode(pt).value>int64(high(longint))) then
  1172. message(parser_e_range_check_error)
  1173. else
  1174. Tprocdef(pd).dispid:=Tordconstnode(pt).value.svalue
  1175. else
  1176. message(parser_e_dispid_must_be_ord_const);
  1177. pt.free;
  1178. end;
  1179. procedure pd_static(pd:tabstractprocdef);
  1180. begin
  1181. if (cs_static_keyword in current_settings.moduleswitches) then
  1182. begin
  1183. if pd.typ=procdef then
  1184. include(tprocdef(pd).procsym.symoptions,sp_static);
  1185. include(pd.procoptions,po_staticmethod);
  1186. end;
  1187. end;
  1188. procedure pd_override(pd:tabstractprocdef);
  1189. begin
  1190. if pd.typ<>procdef then
  1191. internalerror(2003042611);
  1192. if not(is_class_or_interface(tprocdef(pd)._class)) then
  1193. Message(parser_e_no_object_override);
  1194. end;
  1195. procedure pd_overload(pd:tabstractprocdef);
  1196. begin
  1197. if pd.typ<>procdef then
  1198. internalerror(2003042612);
  1199. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1200. end;
  1201. procedure pd_message(pd:tabstractprocdef);
  1202. var
  1203. pt : tnode;
  1204. paracnt : longint;
  1205. begin
  1206. if pd.typ<>procdef then
  1207. internalerror(2003042613);
  1208. if not is_class(tprocdef(pd)._class) then
  1209. Message(parser_e_msg_only_for_classes);
  1210. { check parameter type }
  1211. paracnt:=0;
  1212. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  1213. if paracnt<>1 then
  1214. Message(parser_e_ill_msg_param);
  1215. pt:=comp_expr(true);
  1216. if pt.nodetype=stringconstn then
  1217. begin
  1218. include(pd.procoptions,po_msgstr);
  1219. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).value_str);
  1220. end
  1221. else
  1222. if is_constintnode(pt) then
  1223. begin
  1224. include(pd.procoptions,po_msgint);
  1225. if (Tordconstnode(pt).value<int64(low(Tprocdef(pd).messageinf.i))) or
  1226. (Tordconstnode(pt).value>int64(high(Tprocdef(pd).messageinf.i))) then
  1227. message(parser_e_range_check_error)
  1228. else
  1229. Tprocdef(pd).messageinf.i:=tordconstnode(pt).value.svalue;
  1230. end
  1231. else
  1232. Message(parser_e_ill_msg_expr);
  1233. pt.free;
  1234. end;
  1235. procedure pd_reintroduce(pd:tabstractprocdef);
  1236. begin
  1237. if pd.typ<>procdef then
  1238. internalerror(200401211);
  1239. if not(is_class_or_interface(tprocdef(pd)._class)) then
  1240. Message(parser_e_no_object_reintroduce);
  1241. end;
  1242. procedure pd_syscall(pd:tabstractprocdef);
  1243. {$if defined(powerpc) or defined(m68k)}
  1244. var
  1245. vs : tparavarsym;
  1246. sym : tsym;
  1247. symtable : TSymtable;
  1248. v: Tconstexprint;
  1249. {$endif defined(powerpc) or defined(m68k)}
  1250. begin
  1251. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  1252. internalerror(2003042614);
  1253. tprocdef(pd).forwarddef:=false;
  1254. {$ifdef m68k}
  1255. if target_info.system in [system_m68k_amiga] then
  1256. begin
  1257. include(pd.procoptions,po_syscall_legacy);
  1258. if consume_sym(sym,symtable) then
  1259. begin
  1260. if (sym.typ=staticvarsym) and
  1261. (
  1262. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1263. is_32bitint(tabstractvarsym(sym).vardef)
  1264. ) then
  1265. begin
  1266. tprocdef(pd).libsym:=sym;
  1267. if po_syscall_legacy in tprocdef(pd).procoptions then
  1268. begin
  1269. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1270. paramanager.parseparaloc(vs,'A6');
  1271. pd.parast.insert(vs);
  1272. end
  1273. end
  1274. else
  1275. Message(parser_e_32bitint_or_pointer_variable_expected);
  1276. end;
  1277. (paramanager as tm68kparamanager).create_funcretloc_info(pd,calleeside);
  1278. (paramanager as tm68kparamanager).create_funcretloc_info(pd,callerside);
  1279. v:=get_intconst;
  1280. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1281. message(parser_e_range_check_error)
  1282. else
  1283. Tprocdef(pd).extnumber:=v.uvalue;
  1284. end;
  1285. {$endif m68k}
  1286. {$ifdef powerpc}
  1287. if target_info.system = system_powerpc_amiga then
  1288. begin
  1289. include(pd.procoptions,po_syscall_basesysv);
  1290. if consume_sym(sym,symtable) then
  1291. begin
  1292. if (sym.typ=staticvarsym) and
  1293. (
  1294. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1295. is_32bitint(tabstractvarsym(sym).vardef)
  1296. ) then
  1297. begin
  1298. tprocdef(pd).libsym:=sym;
  1299. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1300. pd.parast.insert(vs);
  1301. end
  1302. else
  1303. Message(parser_e_32bitint_or_pointer_variable_expected);
  1304. end;
  1305. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1306. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1307. v:=get_intconst;
  1308. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1309. message(parser_e_range_check_error)
  1310. else
  1311. Tprocdef(pd).extnumber:=v.uvalue;
  1312. end else
  1313. if target_info.system = system_powerpc_morphos then
  1314. begin
  1315. if idtoken=_LEGACY then
  1316. begin
  1317. consume(_LEGACY);
  1318. include(pd.procoptions,po_syscall_legacy);
  1319. end
  1320. else if idtoken=_SYSV then
  1321. begin
  1322. consume(_SYSV);
  1323. include(pd.procoptions,po_syscall_sysv);
  1324. end
  1325. else if idtoken=_BASESYSV then
  1326. begin
  1327. consume(_BASESYSV);
  1328. include(pd.procoptions,po_syscall_basesysv);
  1329. end
  1330. else if idtoken=_SYSVBASE then
  1331. begin
  1332. consume(_SYSVBASE);
  1333. include(pd.procoptions,po_syscall_sysvbase);
  1334. end
  1335. else if idtoken=_R12BASE then
  1336. begin
  1337. consume(_R12BASE);
  1338. include(pd.procoptions,po_syscall_r12base);
  1339. end
  1340. else
  1341. if syscall_convention='LEGACY' then
  1342. include(pd.procoptions,po_syscall_legacy)
  1343. else if syscall_convention='SYSV' then
  1344. include(pd.procoptions,po_syscall_sysv)
  1345. else if syscall_convention='BASESYSV' then
  1346. include(pd.procoptions,po_syscall_basesysv)
  1347. else if syscall_convention='SYSVBASE' then
  1348. include(pd.procoptions,po_syscall_sysvbase)
  1349. else if syscall_convention='R12BASE' then
  1350. include(pd.procoptions,po_syscall_r12base)
  1351. else
  1352. internalerror(2005010404);
  1353. if consume_sym(sym,symtable) then
  1354. begin
  1355. if (sym.typ=staticvarsym) and
  1356. (
  1357. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1358. is_32bitint(tabstractvarsym(sym).vardef)
  1359. ) then
  1360. begin
  1361. tprocdef(pd).libsym:=sym;
  1362. if po_syscall_legacy in tprocdef(pd).procoptions then
  1363. begin
  1364. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1365. paramanager.parseparaloc(vs,'A6');
  1366. pd.parast.insert(vs);
  1367. end
  1368. else if po_syscall_sysv in tprocdef(pd).procoptions then
  1369. begin
  1370. { Nothing to be done for sysv here for now, but this might change }
  1371. end
  1372. else if po_syscall_basesysv in tprocdef(pd).procoptions then
  1373. begin
  1374. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1375. pd.parast.insert(vs);
  1376. end
  1377. else if po_syscall_sysvbase in tprocdef(pd).procoptions then
  1378. begin
  1379. vs:=tparavarsym.create('$syscalllib',paranr_syscall_sysvbase,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1380. pd.parast.insert(vs);
  1381. end
  1382. else if po_syscall_r12base in tprocdef(pd).procoptions then
  1383. begin
  1384. vs:=tparavarsym.create('$syscalllib',paranr_syscall_r12base,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1385. paramanager.parseparaloc(vs,'R12');
  1386. pd.parast.insert(vs);
  1387. end
  1388. else
  1389. internalerror(2005010501);
  1390. end
  1391. else
  1392. Message(parser_e_32bitint_or_pointer_variable_expected);
  1393. end;
  1394. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1395. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1396. v:=get_intconst;
  1397. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1398. message(parser_e_range_check_error)
  1399. else
  1400. Tprocdef(pd).extnumber:=v.uvalue;
  1401. end;
  1402. {$endif powerpc}
  1403. end;
  1404. procedure pd_external(pd:tabstractprocdef);
  1405. {
  1406. If import_dll=nil the procedure is assumed to be in another
  1407. object file. In that object file it should have the name to
  1408. which import_name is pointing to. Otherwise, the procedure is
  1409. assumed to be in the DLL to which import_dll is pointing to. In
  1410. that case either import_nr<>0 or import_name<>nil is true, so
  1411. the procedure is either imported by number or by name. (DM)
  1412. }
  1413. var
  1414. hs : string;
  1415. v:Tconstexprint;
  1416. begin
  1417. if pd.typ<>procdef then
  1418. internalerror(2003042615);
  1419. with tprocdef(pd) do
  1420. begin
  1421. forwarddef:=false;
  1422. { forbid local external procedures }
  1423. if parast.symtablelevel>normal_function_level then
  1424. Message(parser_e_no_local_proc_external);
  1425. { If the procedure should be imported from a DLL, a constant string follows.
  1426. This isn't really correct, an contant string expression follows
  1427. so we check if an semicolon follows, else a string constant have to
  1428. follow (FK) }
  1429. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  1430. begin
  1431. { Always add library prefix and suffix to create an uniform name }
  1432. hs:=get_stringconst;
  1433. if ExtractFileExt(hs)='' then
  1434. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  1435. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  1436. hs:=target_info.sharedlibprefix+hs;
  1437. import_dll:=stringdup(hs);
  1438. include(procoptions,po_has_importdll);
  1439. if (idtoken=_NAME) then
  1440. begin
  1441. consume(_NAME);
  1442. import_name:=stringdup(get_stringconst);
  1443. include(procoptions,po_has_importname);
  1444. if import_name^='' then
  1445. message(parser_e_empty_import_name);
  1446. end;
  1447. if (idtoken=_INDEX) then
  1448. begin
  1449. {After the word index follows the index number in the DLL.}
  1450. consume(_INDEX);
  1451. v:=get_intconst;
  1452. if (v<int64(low(import_nr))) or (v>int64(high(import_nr))) then
  1453. message(parser_e_range_check_error)
  1454. else
  1455. import_nr:=longint(v.svalue);
  1456. end;
  1457. { default is to used the realname of the procedure }
  1458. if (import_nr=0) and not assigned(import_name) then
  1459. begin
  1460. import_name:=stringdup(procsym.realname);
  1461. include(procoptions,po_has_importname);
  1462. end;
  1463. end
  1464. else
  1465. begin
  1466. if (idtoken=_NAME) then
  1467. begin
  1468. consume(_NAME);
  1469. import_name:=stringdup(get_stringconst);
  1470. include(procoptions,po_has_importname);
  1471. if import_name^='' then
  1472. message(parser_e_empty_import_name);
  1473. end;
  1474. end;
  1475. end;
  1476. end;
  1477. type
  1478. pd_handler=procedure(pd:tabstractprocdef);
  1479. proc_dir_rec=record
  1480. idtok : ttoken;
  1481. pd_flags : tpdflags;
  1482. handler : pd_handler;
  1483. pocall : tproccalloption;
  1484. pooption : tprocoptions;
  1485. mutexclpocall : tproccalloptions;
  1486. mutexclpotype : tproctypeoptions;
  1487. mutexclpo : tprocoptions;
  1488. end;
  1489. const
  1490. {Should contain the number of procedure directives we support.}
  1491. num_proc_directives=39;
  1492. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  1493. (
  1494. (
  1495. idtok:_ABSTRACT;
  1496. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1497. handler : @pd_abstract;
  1498. pocall : pocall_none;
  1499. pooption : [po_abstractmethod];
  1500. mutexclpocall : [pocall_internproc];
  1501. mutexclpotype : [];
  1502. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  1503. ),(
  1504. idtok:_ALIAS;
  1505. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1506. handler : @pd_alias;
  1507. pocall : pocall_none;
  1508. pooption : [];
  1509. mutexclpocall : [];
  1510. mutexclpotype : [];
  1511. mutexclpo : [po_external,po_inline]
  1512. ),(
  1513. idtok:_ASMNAME;
  1514. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  1515. handler : @pd_asmname;
  1516. pocall : pocall_cdecl;
  1517. pooption : [po_external];
  1518. mutexclpocall : [pocall_internproc];
  1519. mutexclpotype : [];
  1520. mutexclpo : [po_external,po_inline]
  1521. ),(
  1522. idtok:_ASSEMBLER;
  1523. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1524. handler : nil;
  1525. pocall : pocall_none;
  1526. pooption : [po_assembler];
  1527. mutexclpocall : [];
  1528. mutexclpotype : [];
  1529. mutexclpo : [po_external]
  1530. ),(
  1531. idtok:_C; {same as cdecl for mode mac}
  1532. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1533. handler : nil;
  1534. pocall : pocall_cdecl;
  1535. pooption : [];
  1536. mutexclpocall : [];
  1537. mutexclpotype : [potype_constructor,potype_destructor];
  1538. mutexclpo : [po_assembler,po_external]
  1539. ),(
  1540. idtok:_CDECL;
  1541. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1542. handler : nil;
  1543. pocall : pocall_cdecl;
  1544. pooption : [];
  1545. mutexclpocall : [];
  1546. mutexclpotype : [potype_constructor,potype_destructor];
  1547. mutexclpo : [po_assembler,po_external]
  1548. ),(
  1549. idtok:_DISPID;
  1550. pd_flags : [pd_dispinterface];
  1551. handler : @pd_dispid;
  1552. pocall : pocall_none;
  1553. pooption : [po_dispid];
  1554. mutexclpocall : [pocall_internproc];
  1555. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1556. mutexclpo : [po_interrupt,po_external,po_inline]
  1557. ),(
  1558. idtok:_DYNAMIC;
  1559. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1560. handler : @pd_virtual;
  1561. pocall : pocall_none;
  1562. pooption : [po_virtualmethod];
  1563. mutexclpocall : [pocall_internproc];
  1564. mutexclpotype : [];
  1565. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  1566. ),(
  1567. idtok:_EXPORT;
  1568. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf];
  1569. handler : @pd_export;
  1570. pocall : pocall_none;
  1571. pooption : [po_exports,po_global];
  1572. mutexclpocall : [pocall_internproc];
  1573. mutexclpotype : [potype_constructor,potype_destructor];
  1574. mutexclpo : [po_external,po_interrupt,po_inline]
  1575. ),(
  1576. idtok:_EXTERNAL;
  1577. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject];
  1578. handler : @pd_external;
  1579. pocall : pocall_none;
  1580. pooption : [po_external];
  1581. mutexclpocall : [pocall_internproc,pocall_syscall];
  1582. { allowed for external cpp classes }
  1583. mutexclpotype : [{potype_constructor,potype_destructor}];
  1584. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  1585. ),(
  1586. idtok:_FAR;
  1587. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf];
  1588. handler : @pd_far;
  1589. pocall : pocall_none;
  1590. pooption : [];
  1591. mutexclpocall : [pocall_internproc];
  1592. mutexclpotype : [];
  1593. mutexclpo : [po_inline]
  1594. ),(
  1595. idtok:_FAR16;
  1596. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject];
  1597. handler : nil;
  1598. pocall : pocall_far16;
  1599. pooption : [];
  1600. mutexclpocall : [];
  1601. mutexclpotype : [];
  1602. mutexclpo : [po_external]
  1603. ),(
  1604. idtok:_FORWARD;
  1605. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf];
  1606. handler : @pd_forward;
  1607. pocall : pocall_none;
  1608. pooption : [];
  1609. mutexclpocall : [pocall_internproc];
  1610. mutexclpotype : [];
  1611. mutexclpo : [po_external,po_inline]
  1612. ),(
  1613. idtok:_OLDFPCCALL;
  1614. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1615. handler : nil;
  1616. pocall : pocall_oldfpccall;
  1617. pooption : [];
  1618. mutexclpocall : [];
  1619. mutexclpotype : [];
  1620. mutexclpo : []
  1621. ),(
  1622. idtok:_INLINE;
  1623. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1624. handler : nil;
  1625. pocall : pocall_none;
  1626. pooption : [po_inline];
  1627. mutexclpocall : [];
  1628. mutexclpotype : [potype_constructor,potype_destructor];
  1629. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod]
  1630. ),(
  1631. idtok:_INTERNCONST;
  1632. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf];
  1633. handler : @pd_internconst;
  1634. pocall : pocall_none;
  1635. pooption : [po_internconst];
  1636. mutexclpocall : [];
  1637. mutexclpotype : [potype_operator];
  1638. mutexclpo : []
  1639. ),(
  1640. idtok:_INTERNPROC;
  1641. pd_flags : [pd_interface,pd_notobject,pd_notobjintf];
  1642. handler : @pd_internproc;
  1643. pocall : pocall_internproc;
  1644. pooption : [];
  1645. mutexclpocall : [];
  1646. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1647. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  1648. ),(
  1649. idtok:_INTERRUPT;
  1650. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1651. handler : @pd_interrupt;
  1652. pocall : pocall_oldfpccall;
  1653. pooption : [po_interrupt];
  1654. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,
  1655. pocall_pascal,pocall_far16,pocall_oldfpccall];
  1656. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1657. mutexclpo : [po_external,po_inline]
  1658. ),(
  1659. idtok:_IOCHECK;
  1660. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1661. handler : nil;
  1662. pocall : pocall_none;
  1663. pooption : [po_iocheck];
  1664. mutexclpocall : [pocall_internproc];
  1665. mutexclpotype : [];
  1666. mutexclpo : [po_external]
  1667. ),(
  1668. idtok:_LOCAL;
  1669. pd_flags : [pd_implemen,pd_body];
  1670. handler : nil;
  1671. pocall : pocall_none;
  1672. pooption : [po_kylixlocal];
  1673. mutexclpocall : [pocall_internproc,pocall_far16];
  1674. mutexclpotype : [];
  1675. mutexclpo : [po_external,po_exports]
  1676. ),(
  1677. idtok:_MESSAGE;
  1678. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1679. handler : @pd_message;
  1680. pocall : pocall_none;
  1681. pooption : []; { can be po_msgstr or po_msgint }
  1682. mutexclpocall : [pocall_internproc];
  1683. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1684. mutexclpo : [po_interrupt,po_external,po_inline]
  1685. ),(
  1686. idtok:_MWPASCAL;
  1687. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1688. handler : nil;
  1689. pocall : pocall_mwpascal;
  1690. pooption : [];
  1691. mutexclpocall : [];
  1692. mutexclpotype : [];
  1693. mutexclpo : []
  1694. ),(
  1695. idtok:_NEAR;
  1696. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1697. handler : @pd_near;
  1698. pocall : pocall_none;
  1699. pooption : [];
  1700. mutexclpocall : [pocall_internproc];
  1701. mutexclpotype : [];
  1702. mutexclpo : []
  1703. ),(
  1704. idtok:_NOSTACKFRAME;
  1705. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1706. handler : nil;
  1707. pocall : pocall_none;
  1708. pooption : [po_nostackframe];
  1709. mutexclpocall : [pocall_internproc];
  1710. mutexclpotype : [];
  1711. mutexclpo : []
  1712. ),(
  1713. idtok:_OVERLOAD;
  1714. pd_flags : [pd_implemen,pd_interface,pd_body];
  1715. handler : @pd_overload;
  1716. pocall : pocall_none;
  1717. pooption : [po_overload];
  1718. mutexclpocall : [pocall_internproc];
  1719. mutexclpotype : [];
  1720. mutexclpo : []
  1721. ),(
  1722. idtok:_OVERRIDE;
  1723. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1724. handler : @pd_override;
  1725. pocall : pocall_none;
  1726. pooption : [po_overridingmethod,po_virtualmethod];
  1727. mutexclpocall : [pocall_internproc];
  1728. mutexclpotype : [];
  1729. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod,po_inline]
  1730. ),(
  1731. idtok:_PASCAL;
  1732. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1733. handler : nil;
  1734. pocall : pocall_pascal;
  1735. pooption : [];
  1736. mutexclpocall : [];
  1737. mutexclpotype : [potype_constructor,potype_destructor];
  1738. mutexclpo : [po_external]
  1739. ),(
  1740. idtok:_PUBLIC;
  1741. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1742. handler : @pd_public;
  1743. pocall : pocall_none;
  1744. pooption : [po_public,po_global];
  1745. mutexclpocall : [pocall_internproc];
  1746. mutexclpotype : [];
  1747. mutexclpo : [po_external,po_inline]
  1748. ),(
  1749. idtok:_REGISTER;
  1750. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1751. handler : nil;
  1752. pocall : pocall_register;
  1753. pooption : [];
  1754. mutexclpocall : [];
  1755. mutexclpotype : [potype_constructor,potype_destructor];
  1756. mutexclpo : [po_external]
  1757. ),(
  1758. idtok:_REINTRODUCE;
  1759. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1760. handler : @pd_reintroduce;
  1761. pocall : pocall_none;
  1762. pooption : [po_reintroduce];
  1763. mutexclpocall : [pocall_internproc];
  1764. mutexclpotype : [];
  1765. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  1766. ),(
  1767. idtok:_SAFECALL;
  1768. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1769. handler : nil;
  1770. pocall : pocall_safecall;
  1771. pooption : [];
  1772. mutexclpocall : [];
  1773. mutexclpotype : [potype_constructor,potype_destructor];
  1774. mutexclpo : [po_external]
  1775. ),(
  1776. idtok:_SOFTFLOAT;
  1777. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1778. handler : nil;
  1779. pocall : pocall_softfloat;
  1780. pooption : [];
  1781. mutexclpocall : [];
  1782. mutexclpotype : [potype_constructor,potype_destructor];
  1783. { it's available with po_external because the libgcc floating point routines on the arm
  1784. uses this calling convention }
  1785. mutexclpo : []
  1786. ),(
  1787. idtok:_STATIC;
  1788. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1789. handler : @pd_static;
  1790. pocall : pocall_none;
  1791. pooption : [po_staticmethod];
  1792. mutexclpocall : [pocall_internproc];
  1793. mutexclpotype : [potype_constructor,potype_destructor];
  1794. mutexclpo : [po_external,po_interrupt,po_exports,po_inline]
  1795. ),(
  1796. idtok:_STDCALL;
  1797. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1798. handler : nil;
  1799. pocall : pocall_stdcall;
  1800. pooption : [];
  1801. mutexclpocall : [];
  1802. mutexclpotype : [potype_constructor,potype_destructor];
  1803. mutexclpo : [po_external]
  1804. ),(
  1805. idtok:_SYSCALL;
  1806. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  1807. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  1808. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  1809. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  1810. handler : @pd_syscall;
  1811. pocall : pocall_syscall;
  1812. pooption : [];
  1813. mutexclpocall : [];
  1814. mutexclpotype : [potype_constructor,potype_destructor];
  1815. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  1816. ),(
  1817. idtok:_VIRTUAL;
  1818. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1819. handler : @pd_virtual;
  1820. pocall : pocall_none;
  1821. pooption : [po_virtualmethod];
  1822. mutexclpocall : [pocall_internproc];
  1823. mutexclpotype : [];
  1824. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  1825. ),(
  1826. idtok:_CPPDECL;
  1827. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1828. handler : nil;
  1829. pocall : pocall_cppdecl;
  1830. pooption : [];
  1831. mutexclpocall : [];
  1832. mutexclpotype : [potype_constructor,potype_destructor];
  1833. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  1834. ),(
  1835. idtok:_VARARGS;
  1836. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  1837. handler : nil;
  1838. pocall : pocall_none;
  1839. pooption : [po_varargs];
  1840. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  1841. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  1842. mutexclpotype : [];
  1843. mutexclpo : [po_assembler,po_interrupt,po_inline]
  1844. ),(
  1845. idtok:_COMPILERPROC;
  1846. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1847. handler : nil;
  1848. pocall : pocall_none;
  1849. pooption : [po_compilerproc];
  1850. mutexclpocall : [];
  1851. mutexclpotype : [potype_constructor,potype_destructor];
  1852. mutexclpo : [po_interrupt]
  1853. )
  1854. );
  1855. function check_proc_directive(isprocvar:boolean):boolean;
  1856. var
  1857. i : longint;
  1858. begin
  1859. result:=false;
  1860. for i:=1 to num_proc_directives do
  1861. if proc_direcdata[i].idtok=idtoken then
  1862. begin
  1863. if ((not isprocvar) or
  1864. (pd_procvar in proc_direcdata[i].pd_flags)) and
  1865. { don't eat a public directive in classes }
  1866. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  1867. result:=true;
  1868. exit;
  1869. end;
  1870. end;
  1871. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  1872. {
  1873. Parse the procedure directive, returns true if a correct directive is found
  1874. }
  1875. var
  1876. p : longint;
  1877. found : boolean;
  1878. name : TIDString;
  1879. begin
  1880. parse_proc_direc:=false;
  1881. name:=tokeninfo^[idtoken].str;
  1882. found:=false;
  1883. { Hint directive? Then exit immediatly }
  1884. if (m_hintdirective in current_settings.modeswitches) then
  1885. begin
  1886. case idtoken of
  1887. _LIBRARY,
  1888. _PLATFORM,
  1889. _UNIMPLEMENTED,
  1890. _EXPERIMENTAL,
  1891. _DEPRECATED :
  1892. exit;
  1893. end;
  1894. end;
  1895. { C directive is MAC only, because it breaks too much existing code
  1896. on other platforms (PFV) }
  1897. if (idtoken=_C) and
  1898. not(m_mac in current_settings.modeswitches) then
  1899. exit;
  1900. { retrieve data for directive if found }
  1901. for p:=1 to num_proc_directives do
  1902. if proc_direcdata[p].idtok=idtoken then
  1903. begin
  1904. found:=true;
  1905. break;
  1906. end;
  1907. { Check if the procedure directive is known }
  1908. if not found then
  1909. begin
  1910. { parsing a procvar type the name can be any
  1911. next variable !! }
  1912. if ((pdflags * [pd_procvar,pd_object])=[]) and
  1913. not(idtoken=_PROPERTY) then
  1914. Message1(parser_w_unknown_proc_directive_ignored,name);
  1915. exit;
  1916. end;
  1917. { static needs a special treatment }
  1918. if (idtoken=_STATIC) and not (cs_static_keyword in current_settings.moduleswitches) then
  1919. exit;
  1920. { check if method and directive not for object, like public.
  1921. This needs to be checked also for procvars }
  1922. if (pd_notobject in proc_direcdata[p].pd_flags) and
  1923. (symtablestack.top.symtabletype=ObjectSymtable) and
  1924. { directive allowed for cpp classes? }
  1925. not(is_cppclass(tdef(symtablestack.top.defowner)) and (pd_cppobject in proc_direcdata[p].pd_flags)) then
  1926. exit;
  1927. { Conflicts between directives ? }
  1928. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  1929. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  1930. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  1931. begin
  1932. Message1(parser_e_proc_dir_conflict,name);
  1933. exit;
  1934. end;
  1935. { set calling convention }
  1936. if proc_direcdata[p].pocall<>pocall_none then
  1937. begin
  1938. if (po_hascallingconvention in pd.procoptions) then
  1939. begin
  1940. Message2(parser_w_proc_overriding_calling,
  1941. proccalloptionStr[pd.proccalloption],
  1942. proccalloptionStr[proc_direcdata[p].pocall]);
  1943. end;
  1944. { check if the target processor supports this calling convention }
  1945. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  1946. begin
  1947. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  1948. { recover }
  1949. proc_direcdata[p].pocall:=pocall_stdcall;
  1950. end;
  1951. pd.proccalloption:=proc_direcdata[p].pocall;
  1952. include(pd.procoptions,po_hascallingconvention);
  1953. end;
  1954. if pd.typ=procdef then
  1955. begin
  1956. { Check if the directive is only for objects }
  1957. if (pd_object in proc_direcdata[p].pd_flags) and
  1958. not assigned(tprocdef(pd)._class) then
  1959. exit;
  1960. { check if method and directive not for interface }
  1961. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  1962. is_interface(tprocdef(pd)._class) then
  1963. exit;
  1964. { check if method and directive not for interface }
  1965. if is_dispinterface(tprocdef(pd)._class) and
  1966. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  1967. exit;
  1968. end;
  1969. { consume directive, and turn flag on }
  1970. consume(token);
  1971. parse_proc_direc:=true;
  1972. { Check the pd_flags if the directive should be allowed }
  1973. if (pd_interface in pdflags) and
  1974. not(pd_interface in proc_direcdata[p].pd_flags) then
  1975. begin
  1976. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  1977. exit;
  1978. end;
  1979. if (pd_implemen in pdflags) and
  1980. not(pd_implemen in proc_direcdata[p].pd_flags) then
  1981. begin
  1982. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  1983. exit;
  1984. end;
  1985. if (pd_procvar in pdflags) and
  1986. not(pd_procvar in proc_direcdata[p].pd_flags) then
  1987. begin
  1988. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  1989. exit;
  1990. end;
  1991. { Return the new pd_flags }
  1992. if not(pd_body in proc_direcdata[p].pd_flags) then
  1993. exclude(pdflags,pd_body);
  1994. { Add the correct flag }
  1995. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  1996. { Call the handler }
  1997. if pointer(proc_direcdata[p].handler)<>nil then
  1998. proc_direcdata[p].handler(pd);
  1999. end;
  2000. function proc_get_importname(pd:tprocdef):string;
  2001. function maybe_cprefix(const s:string):string;
  2002. begin
  2003. if not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  2004. result:=s
  2005. else
  2006. result:=target_info.Cprefix+s;
  2007. end;
  2008. begin
  2009. result:='';
  2010. if not(po_external in pd.procoptions) then
  2011. internalerror(200412151);
  2012. { external name or number is specified }
  2013. if assigned(pd.import_name) or (pd.import_nr<>0) then
  2014. begin
  2015. if assigned(pd.import_dll) then
  2016. begin
  2017. { If we are not using direct dll linking under win32 then imports
  2018. need to use the normal name since two functions can refer to the
  2019. same DLL function. This is also needed for compatability
  2020. with Delphi and TP7 }
  2021. (*
  2022. case target_info.system of
  2023. system_i386_emx,
  2024. system_i386_os2 :
  2025. begin
  2026. { keep normal mangledname }
  2027. if not (Assigned (PD.Import_Name)) then
  2028. Result := PD.MangledName;
  2029. end;
  2030. else
  2031. *)
  2032. if assigned(pd.import_name) then
  2033. begin
  2034. if target_info.system in (system_all_windows +
  2035. [system_i386_emx, system_i386_os2]) then
  2036. { cprefix is not used in DLL imports under Windows or OS/2 }
  2037. result:=pd.import_name^
  2038. else
  2039. result:=maybe_cprefix(pd.import_name^);
  2040. end
  2041. else
  2042. result:=ExtractFileName(pd.import_dll^)+'_index_'+tostr(pd.import_nr);
  2043. end
  2044. else
  2045. result:=maybe_cprefix(pd.import_name^);
  2046. end
  2047. else
  2048. begin
  2049. { Default names when importing variables }
  2050. case pd.proccalloption of
  2051. pocall_cdecl :
  2052. begin
  2053. if assigned(pd._class) then
  2054. result:=target_info.Cprefix+pd._class.objrealname^+'_'+pd.procsym.realname
  2055. else
  2056. result:=target_info.Cprefix+pd.procsym.realname;
  2057. end;
  2058. pocall_cppdecl :
  2059. begin
  2060. result:=target_info.Cprefix+pd.cplusplusmangledname;
  2061. end;
  2062. else
  2063. begin
  2064. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  2065. if (m_mac in current_settings.modeswitches) then
  2066. result:=tprocdef(pd).procsym.realname;
  2067. end;
  2068. end;
  2069. end;
  2070. end;
  2071. procedure proc_set_mangledname(pd:tprocdef);
  2072. var
  2073. s : string;
  2074. begin
  2075. { When the mangledname is already set we aren't allowed to change
  2076. it because it can already be used somewhere (PFV) }
  2077. if not(po_has_mangledname in pd.procoptions) then
  2078. begin
  2079. if (po_external in pd.procoptions) then
  2080. begin
  2081. { External Procedures are only allowed to change the mangledname
  2082. in their first declaration }
  2083. if (pd.forwarddef or (not pd.hasforward)) then
  2084. begin
  2085. s:=proc_get_importname(pd);
  2086. if s<>'' then
  2087. begin
  2088. { Replace ? and @ in import name }
  2089. Replace(s,'?','__q$$');
  2090. Replace(s,'@','__a$$');
  2091. pd.setmangledname(s);
  2092. end;
  2093. end;
  2094. end
  2095. else
  2096. { Normal procedures }
  2097. begin
  2098. if (po_compilerproc in pd.procoptions) then
  2099. begin
  2100. pd.setmangledname(lower(pd.procsym.name));
  2101. end;
  2102. end;
  2103. end;
  2104. { Public/exported alias names }
  2105. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  2106. not(po_has_public_name in pd.procoptions) then
  2107. begin
  2108. case pd.proccalloption of
  2109. pocall_cdecl :
  2110. begin
  2111. if assigned(pd._class) then
  2112. pd.aliasnames.insert(target_info.Cprefix+pd._class.objrealname^+'_'+pd.procsym.realname)
  2113. else
  2114. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  2115. end;
  2116. pocall_cppdecl :
  2117. begin
  2118. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  2119. end;
  2120. end;
  2121. { prevent adding the alias a second time }
  2122. include(pd.procoptions,po_has_public_name);
  2123. end;
  2124. end;
  2125. procedure handle_calling_convention(pd:tabstractprocdef);
  2126. begin
  2127. { set the default calling convention if none provided }
  2128. if not(po_hascallingconvention in pd.procoptions) then
  2129. pd.proccalloption:=current_settings.defproccall
  2130. else
  2131. begin
  2132. if pd.proccalloption=pocall_none then
  2133. internalerror(200309081);
  2134. end;
  2135. { handle proccall specific settings }
  2136. case pd.proccalloption of
  2137. pocall_cdecl,
  2138. pocall_cppdecl :
  2139. begin
  2140. { check C cdecl para types }
  2141. check_c_para(pd);
  2142. end;
  2143. pocall_far16 :
  2144. begin
  2145. { Temporary stub, must be rewritten to support OS/2 far16 }
  2146. Message1(parser_w_proc_directive_ignored,'FAR16');
  2147. end;
  2148. end;
  2149. { Inlining is enabled and supported? }
  2150. if (po_inline in pd.procoptions) and
  2151. not(cs_do_inline in current_settings.localswitches) then
  2152. begin
  2153. { Give an error if inline is not supported by the compiler mode,
  2154. otherwise only give a warning that this procedure will not be inlined }
  2155. if not(m_default_inline in current_settings.modeswitches) then
  2156. Message(parser_e_proc_inline_not_supported)
  2157. else
  2158. Message(parser_w_inlining_disabled);
  2159. exclude(pd.procoptions,po_inline);
  2160. end;
  2161. { For varargs directive also cdecl and external must be defined }
  2162. if (po_varargs in pd.procoptions) then
  2163. begin
  2164. { check first for external in the interface, if available there
  2165. then the cdecl must also be there since there is no implementation
  2166. available to contain it }
  2167. if parse_only then
  2168. begin
  2169. { if external is available, then cdecl must also be available,
  2170. procvars don't need external }
  2171. if not((po_external in pd.procoptions) or
  2172. (pd.typ=procvardef)) and
  2173. not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl,pocall_mwpascal]) then
  2174. Message(parser_e_varargs_need_cdecl_and_external);
  2175. end
  2176. else
  2177. begin
  2178. { both must be defined now }
  2179. if not((po_external in pd.procoptions) or
  2180. (pd.typ=procvardef)) or
  2181. not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl,pocall_mwpascal]) then
  2182. Message(parser_e_varargs_need_cdecl_and_external);
  2183. end;
  2184. end;
  2185. { insert hidden high parameters }
  2186. pd.parast.SymList.ForEachCall(@insert_hidden_para,pd);
  2187. { insert hidden self parameter }
  2188. insert_self_and_vmt_para(pd);
  2189. { insert funcret parameter if required }
  2190. insert_funcret_para(pd);
  2191. { Make var parameters regable, this must be done after the calling
  2192. convention is set. }
  2193. { this must be done before parentfp is insert, because getting all cases
  2194. where parentfp must be in a memory location isn't catched properly so
  2195. we put parentfp never in a register }
  2196. pd.parast.SymList.ForEachCall(@set_addr_param_regable,pd);
  2197. { insert parentfp parameter if required }
  2198. insert_parentfp_para(pd);
  2199. { Calculate parameter tlist }
  2200. pd.calcparas;
  2201. end;
  2202. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  2203. {
  2204. Parse the procedure directives. It does not matter if procedure directives
  2205. are written using ;procdir; or ['procdir'] syntax.
  2206. }
  2207. var
  2208. res : boolean;
  2209. begin
  2210. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  2211. begin
  2212. tprocdef(pd).aliasnames.insert(tprocdef(pd).procsym.realname);
  2213. include(pd.procoptions,po_public);
  2214. include(pd.procoptions,po_has_public_name);
  2215. include(pd.procoptions,po_global);
  2216. end;
  2217. while token in [_ID,_LECKKLAMMER] do
  2218. begin
  2219. if try_to_consume(_LECKKLAMMER) then
  2220. begin
  2221. repeat
  2222. parse_proc_direc(pd,pdflags);
  2223. until not try_to_consume(_COMMA);
  2224. consume(_RECKKLAMMER);
  2225. { we always expect at least '[];' }
  2226. res:=true;
  2227. end
  2228. else
  2229. begin
  2230. res:=parse_proc_direc(pd,pdflags);
  2231. end;
  2232. { A procedure directive normally followed by a semicolon, but in
  2233. a const section or reading a type we should stop when _EQUAL is found,
  2234. because a constant/default value follows }
  2235. if res then
  2236. begin
  2237. if (block_type in [bt_const,bt_type]) and
  2238. (token=_EQUAL) then
  2239. break;
  2240. { support procedure proc;stdcall export; }
  2241. if not(check_proc_directive((pd.typ=procvardef))) then
  2242. begin
  2243. { support "record p : procedure stdcall end;" and
  2244. "var p : procedure stdcall = nil;" }
  2245. if (pd_procvar in pdflags) and
  2246. (token in [_END,_RKLAMMER,_EQUAL]) then
  2247. break
  2248. else
  2249. consume(_SEMICOLON);
  2250. end;
  2251. end
  2252. else
  2253. break;
  2254. end;
  2255. end;
  2256. procedure parse_var_proc_directives(sym:tsym);
  2257. var
  2258. pdflags : tpdflags;
  2259. pd : tabstractprocdef;
  2260. begin
  2261. pdflags:=[pd_procvar];
  2262. pd:=nil;
  2263. case sym.typ of
  2264. fieldvarsym,
  2265. staticvarsym,
  2266. localvarsym,
  2267. paravarsym :
  2268. pd:=tabstractprocdef(tabstractvarsym(sym).vardef);
  2269. typesym :
  2270. pd:=tabstractprocdef(ttypesym(sym).typedef);
  2271. else
  2272. internalerror(2003042617);
  2273. end;
  2274. if pd.typ<>procvardef then
  2275. internalerror(2003042618);
  2276. { names should never be used anyway }
  2277. parse_proc_directives(pd,pdflags);
  2278. end;
  2279. procedure parse_object_proc_directives(pd:tabstractprocdef);
  2280. var
  2281. pdflags : tpdflags;
  2282. begin
  2283. pdflags:=[pd_object];
  2284. parse_proc_directives(pd,pdflags);
  2285. end;
  2286. function proc_add_definition(var currpd:tprocdef):boolean;
  2287. {
  2288. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  2289. forwarddef is found and reused it returns true
  2290. }
  2291. var
  2292. fwpd : tprocdef;
  2293. currparasym,
  2294. fwparasym : tsym;
  2295. currparacnt,
  2296. fwparacnt,
  2297. curridx,
  2298. fwidx,
  2299. i : longint;
  2300. po_comp : tprocoptions;
  2301. paracompopt: tcompare_paras_options;
  2302. forwardfound : boolean;
  2303. begin
  2304. forwardfound:=false;
  2305. { check overloaded functions if the same function already exists }
  2306. for i:=0 to tprocsym(currpd.procsym).ProcdefList.Count-1 do
  2307. begin
  2308. fwpd:=tprocdef(tprocsym(currpd.procsym).ProcdefList[i]);
  2309. { Skip overloaded definitions that are declared in other units }
  2310. if fwpd.procsym<>currpd.procsym then
  2311. continue;
  2312. { check the parameters, for delphi/tp it is possible to
  2313. leave the parameters away in the implementation (forwarddef=false).
  2314. But for an overload declared function this is not allowed }
  2315. if { check if empty implementation arguments match is allowed }
  2316. (
  2317. not(m_repeat_forward in current_settings.modeswitches) and
  2318. not(currpd.forwarddef) and
  2319. is_bareprocdef(currpd) and
  2320. not(po_overload in fwpd.procoptions)
  2321. ) or
  2322. { check arguments, we need to check only the user visible parameters. The hidden parameters
  2323. can be in a different location because of the calling convention, eg. L-R vs. R-L order (PFV) }
  2324. (
  2325. (compare_paras(currpd.paras,fwpd.paras,cp_none,[cpo_comparedefaultvalue,cpo_ignorehidden,cpo_openequalisexact])=te_exact) and
  2326. (fwpd.returndef=currpd.returndef)
  2327. ) then
  2328. begin
  2329. { Check if we've found the forwarddef, if found then
  2330. we need to update the forward def with the current
  2331. implementation settings }
  2332. if fwpd.forwarddef then
  2333. begin
  2334. forwardfound:=true;
  2335. if not(m_repeat_forward in current_settings.modeswitches) and
  2336. (fwpd.proccalloption<>currpd.proccalloption) then
  2337. paracompopt:=[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_openequalisexact]
  2338. else
  2339. paracompopt:=[cpo_comparedefaultvalue,cpo_openequalisexact];
  2340. { Check calling convention }
  2341. if (fwpd.proccalloption<>currpd.proccalloption) then
  2342. begin
  2343. { In delphi it is possible to specify the calling
  2344. convention in the interface or implementation if
  2345. there was no convention specified in the other
  2346. part }
  2347. if (m_delphi in current_settings.modeswitches) then
  2348. begin
  2349. if not(po_hascallingconvention in currpd.procoptions) then
  2350. currpd.proccalloption:=fwpd.proccalloption
  2351. else
  2352. if not(po_hascallingconvention in fwpd.procoptions) then
  2353. fwpd.proccalloption:=currpd.proccalloption
  2354. else
  2355. begin
  2356. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2357. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2358. { restore interface settings }
  2359. currpd.proccalloption:=fwpd.proccalloption;
  2360. end;
  2361. end
  2362. else
  2363. begin
  2364. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2365. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2366. { restore interface settings }
  2367. currpd.proccalloption:=fwpd.proccalloption;
  2368. end;
  2369. end;
  2370. { Check if the procedure type and return type are correct,
  2371. also the parameters must match also with the type }
  2372. if ((m_repeat_forward in current_settings.modeswitches) or
  2373. not is_bareprocdef(currpd)) and
  2374. ((compare_paras(currpd.paras,fwpd.paras,cp_all,paracompopt)<>te_exact) or
  2375. (fwpd.returndef<>currpd.returndef)) then
  2376. begin
  2377. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2378. fwpd.fullprocname(false));
  2379. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2380. break;
  2381. end;
  2382. { Check if both are declared forward }
  2383. if fwpd.forwarddef and currpd.forwarddef then
  2384. begin
  2385. MessagePos1(currpd.fileinfo,parser_e_function_already_declared_public_forward,
  2386. currpd.fullprocname(false));
  2387. end;
  2388. { internconst or internproc only need to be defined once }
  2389. if (fwpd.proccalloption=pocall_internproc) then
  2390. currpd.proccalloption:=fwpd.proccalloption
  2391. else
  2392. if (currpd.proccalloption=pocall_internproc) then
  2393. fwpd.proccalloption:=currpd.proccalloption;
  2394. { Check procedure options, Delphi requires that class is
  2395. repeated in the implementation for class methods }
  2396. if (m_fpc in current_settings.modeswitches) then
  2397. po_comp:=[po_classmethod,po_varargs,po_methodpointer,po_interrupt]
  2398. else
  2399. po_comp:=[po_classmethod,po_methodpointer];
  2400. if ((po_comp * fwpd.procoptions)<>(po_comp * currpd.procoptions)) or
  2401. (fwpd.proctypeoption <> currpd.proctypeoption) then
  2402. begin
  2403. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2404. fwpd.fullprocname(false));
  2405. tprocsym(fwpd.procsym).write_parameter_lists(fwpd);
  2406. { This error is non-fatal, we can recover }
  2407. end;
  2408. { Forward declaration is external? }
  2409. if (po_external in fwpd.procoptions) then
  2410. MessagePos(currpd.fileinfo,parser_e_proc_already_external);
  2411. { Check parameters }
  2412. if (m_repeat_forward in current_settings.modeswitches) or
  2413. (currpd.minparacount>0) then
  2414. begin
  2415. { If mangled names are equal then they have the same amount of arguments }
  2416. { We can check the names of the arguments }
  2417. { both symtables are in the same order from left to right }
  2418. curridx:=0;
  2419. fwidx:=0;
  2420. currparacnt:=currpd.parast.SymList.Count;
  2421. fwparacnt:=fwpd.parast.SymList.Count;
  2422. repeat
  2423. { skip default parameter constsyms }
  2424. while (curridx<currparacnt) and
  2425. (tsym(currpd.parast.SymList[curridx]).typ<>paravarsym) do
  2426. inc(curridx);
  2427. while (fwidx<fwparacnt) and
  2428. (tsym(fwpd.parast.SymList[fwidx]).typ<>paravarsym) do
  2429. inc(fwidx);
  2430. { stop when one of the two lists is at the end }
  2431. if (fwidx>=fwparacnt) or (curridx>=currparacnt) then
  2432. break;
  2433. { compare names of parameters, ignore implictly
  2434. renamed parameters }
  2435. currparasym:=tsym(currpd.parast.SymList[curridx]);
  2436. fwparasym:=tsym(fwpd.parast.SymList[fwidx]);
  2437. if not(sp_implicitrename in currparasym.symoptions) and
  2438. not(sp_implicitrename in fwparasym.symoptions) then
  2439. begin
  2440. if (currparasym.name<>fwparasym.name) then
  2441. begin
  2442. MessagePos3(currpd.fileinfo,parser_e_header_different_var_names,
  2443. tprocsym(currpd.procsym).realname,fwparasym.realname,currparasym.realname);
  2444. break;
  2445. end;
  2446. end;
  2447. { next parameter }
  2448. inc(curridx);
  2449. inc(fwidx);
  2450. until false;
  2451. end;
  2452. { Everything is checked, now we can update the forward declaration
  2453. with the new data from the implementation }
  2454. fwpd.forwarddef:=currpd.forwarddef;
  2455. fwpd.hasforward:=true;
  2456. fwpd.procoptions:=fwpd.procoptions+currpd.procoptions;
  2457. { marked as local but exported from unit? }
  2458. if (po_kylixlocal in fwpd.procoptions) and (fwpd.owner.symtabletype=globalsymtable) then
  2459. MessagePos(fwpd.fileinfo,type_e_cant_export_local);
  2460. if fwpd.extnumber=$ffff then
  2461. fwpd.extnumber:=currpd.extnumber;
  2462. while not currpd.aliasnames.empty do
  2463. fwpd.aliasnames.insert(currpd.aliasnames.getfirst);
  2464. { update fileinfo so position references the implementation,
  2465. also update funcretsym if it is already generated }
  2466. fwpd.fileinfo:=currpd.fileinfo;
  2467. if assigned(fwpd.funcretsym) then
  2468. fwpd.funcretsym.fileinfo:=currpd.fileinfo;
  2469. { import names }
  2470. if assigned(currpd.import_dll) then
  2471. begin
  2472. stringdispose(fwpd.import_dll);
  2473. fwpd.import_dll:=stringdup(currpd.import_dll^);
  2474. end;
  2475. if assigned(currpd.import_name) then
  2476. begin
  2477. stringdispose(fwpd.import_name);
  2478. fwpd.import_name:=stringdup(currpd.import_name^);
  2479. end;
  2480. fwpd.import_nr:=currpd.import_nr;
  2481. { for compilerproc defines we need to rename and update the
  2482. symbolname to lowercase }
  2483. if (po_compilerproc in fwpd.procoptions) then
  2484. begin
  2485. { rename to lowercase so users can't access it }
  2486. fwpd.procsym.realname:='$'+lower(fwpd.procsym.name);
  2487. { the mangeled name is already changed by the pd_compilerproc }
  2488. { handler. It must be done immediately because if we have a }
  2489. { call to a compilerproc before it's implementation is }
  2490. { encountered, it must already use the new mangled name (JM) }
  2491. end;
  2492. { Release current procdef }
  2493. currpd.owner.deletedef(currpd);
  2494. currpd:=fwpd;
  2495. end
  2496. else
  2497. begin
  2498. { abstract methods aren't forward defined, but this }
  2499. { needs another error message }
  2500. if (po_abstractmethod in fwpd.procoptions) then
  2501. MessagePos(currpd.fileinfo,parser_e_abstract_no_definition)
  2502. else
  2503. begin
  2504. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  2505. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2506. end;
  2507. end;
  2508. { we found one proc with the same arguments, there are no others
  2509. so we can stop }
  2510. break;
  2511. end;
  2512. { check for allowing overload directive }
  2513. if not(m_fpc in current_settings.modeswitches) then
  2514. begin
  2515. { overload directive turns on overloading }
  2516. if ((po_overload in currpd.procoptions) or
  2517. (po_overload in fwpd.procoptions)) then
  2518. begin
  2519. { check if all procs have overloading, but not if the proc is a method or
  2520. already declared forward, then the check is already done }
  2521. if not(fwpd.hasforward or
  2522. assigned(currpd._class) or
  2523. (currpd.forwarddef<>fwpd.forwarddef) or
  2524. ((po_overload in currpd.procoptions) and
  2525. (po_overload in fwpd.procoptions))) then
  2526. begin
  2527. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  2528. break;
  2529. end;
  2530. end
  2531. else
  2532. begin
  2533. if not(fwpd.forwarddef) then
  2534. begin
  2535. MessagePos(currpd.fileinfo,parser_e_procedure_overloading_is_off);
  2536. break;
  2537. end;
  2538. end;
  2539. end; { equal arguments }
  2540. end;
  2541. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  2542. list }
  2543. if not forwardfound then
  2544. begin
  2545. { can happen in Delphi mode }
  2546. if (currpd.proctypeoption = potype_function) and
  2547. is_void(currpd.returndef) then
  2548. MessagePos1(currpd.fileinfo,parser_e_no_funcret_specified,currpd.procsym.realname);
  2549. tprocsym(currpd.procsym).ProcdefList.Add(currpd);
  2550. end;
  2551. proc_add_definition:=forwardfound;
  2552. end;
  2553. end.