pdecsub.pas 83 KB

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