pdecsub.pas 92 KB

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