pdecsub.pas 99 KB

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