pdecsub.pas 87 KB

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