pdecsub.pas 83 KB

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