2
0

pdecsub.pas 98 KB

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