pdecsub.pas 87 KB

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