pdecsub.pas 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121
  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 in implementation section }
  26. pd_interface, { directive can be used in interface section }
  27. pd_object, { directive can be used with object declaration }
  28. pd_record, { directive can be used with record declaration }
  29. pd_procvar, { directive can be used with procvar declaration }
  30. pd_notobject, { directive can not be used with object declaration }
  31. pd_notrecord, { directive can not be used with record declaration }
  32. pd_notobjintf, { directive can not be used with interface declaration }
  33. pd_notprocvar, { directive can not be used with procvar declaration }
  34. pd_dispinterface,{ directive can be used with dispinterface methods }
  35. pd_cppobject, { directive can be used with cppclass }
  36. pd_objcclass, { directive can be used with objcclass }
  37. pd_objcprot, { directive can be used with objcprotocol }
  38. pd_nothelper, { directive can not be used with record/class helper declaration }
  39. pd_javaclass, { directive can be used with Java class }
  40. pd_intfjava { directive can be used with Java interface }
  41. );
  42. tpdflags=set of tpdflag;
  43. function check_proc_directive(isprocvar:boolean):boolean;
  44. function proc_add_definition(var currpd:tprocdef):boolean;
  45. function proc_get_importname(pd:tprocdef):string;
  46. procedure proc_set_mangledname(pd:tprocdef);
  47. procedure handle_calling_convention(pd:tabstractprocdef);
  48. procedure parse_parameter_dec(pd:tabstractprocdef);
  49. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  50. procedure parse_var_proc_directives(sym:tsym);
  51. procedure parse_object_proc_directives(pd:tabstractprocdef);
  52. procedure parse_record_proc_directives(pd:tabstractprocdef);
  53. function parse_proc_head(astruct:tabstractrecorddef;potype:tproctypeoption;out pd:tprocdef):boolean;
  54. function parse_proc_dec(isclassmethod:boolean;astruct:tabstractrecorddef):tprocdef;
  55. { helper functions - they insert nested objects hierarcy to the symtablestack
  56. with object hierarchy
  57. }
  58. function push_child_hierarcy(obj:tabstractrecorddef):integer;
  59. function pop_child_hierarchy(obj:tabstractrecorddef):integer;
  60. function push_nested_hierarchy(obj:tabstractrecorddef):integer;
  61. function pop_nested_hierarchy(obj:tabstractrecorddef):integer;
  62. implementation
  63. uses
  64. SysUtils,
  65. { common }
  66. cutils,cclasses,
  67. { global }
  68. globtype,globals,verbose,constexp,
  69. systems,fpccrc,
  70. cpuinfo,
  71. { symtable }
  72. symbase,symtable,symcreat,defutil,defcmp,paramgr,cpupara,
  73. { pass 1 }
  74. fmodule,node,htypechk,
  75. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  76. objcutil,
  77. { parser }
  78. scanner,
  79. pbase,pexpr,ptype,pdecl,pparautl
  80. {$ifdef jvm}
  81. ,pjvm
  82. {$endif}
  83. ;
  84. const
  85. { Please leave this here, this module should NOT use
  86. these variables.
  87. Declaring it as string here results in an error when compiling (PFV) }
  88. current_procinfo = 'error';
  89. function push_child_hierarcy(obj:tabstractrecorddef):integer;
  90. var
  91. _class,hp : tobjectdef;
  92. begin
  93. if obj.typ=recorddef then
  94. begin
  95. symtablestack.push(obj.symtable);
  96. result:=1;
  97. exit;
  98. end;
  99. result:=0;
  100. { insert class hierarchy in the reverse order }
  101. hp:=nil;
  102. repeat
  103. _class:=tobjectdef(obj);
  104. while _class.childof<>hp do
  105. _class:=_class.childof;
  106. hp:=_class;
  107. symtablestack.push(_class.symtable);
  108. inc(result);
  109. until hp=obj;
  110. end;
  111. function push_nested_hierarchy(obj:tabstractrecorddef):integer;
  112. begin
  113. result:=0;
  114. if obj.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  115. inc(result,push_nested_hierarchy(tabstractrecorddef(obj.owner.defowner)));
  116. inc(result,push_child_hierarcy(obj));
  117. end;
  118. function pop_child_hierarchy(obj:tabstractrecorddef):integer;
  119. var
  120. _class : tobjectdef;
  121. begin
  122. if obj.typ=recorddef then
  123. begin
  124. symtablestack.pop(obj.symtable);
  125. result:=1;
  126. exit;
  127. end;
  128. result:=0;
  129. _class:=tobjectdef(obj);
  130. while assigned(_class) do
  131. begin
  132. symtablestack.pop(_class.symtable);
  133. _class:=_class.childof;
  134. inc(result);
  135. end;
  136. end;
  137. function pop_nested_hierarchy(obj:tabstractrecorddef):integer;
  138. begin
  139. result:=pop_child_hierarchy(obj);
  140. if obj.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  141. inc(result,pop_nested_hierarchy(tabstractrecorddef(obj.owner.defowner)));
  142. end;
  143. procedure check_msg_para(p:TObject;arg:pointer);
  144. begin
  145. if (tsym(p).typ<>paravarsym) then
  146. exit;
  147. with tparavarsym(p) do
  148. begin
  149. { Count parameters }
  150. if (paranr>=10) then
  151. inc(plongint(arg)^);
  152. { First parameter must be var }
  153. if (paranr=10) and
  154. (varspez<>vs_var) then
  155. MessagePos(fileinfo,parser_e_ill_msg_param);
  156. end;
  157. end;
  158. procedure set_addr_param_regable(p:TObject;arg:pointer);
  159. begin
  160. if (tsym(p).typ<>paravarsym) then
  161. exit;
  162. with tparavarsym(p) do
  163. begin
  164. if not is_managed_type(vardef) and
  165. paramanager.push_addr_param(varspez,vardef,tprocdef(arg).proccalloption) then
  166. varregable:=vr_intreg;
  167. end;
  168. end;
  169. procedure parse_parameter_dec(pd:tabstractprocdef);
  170. {
  171. handle_procvar needs the same changes
  172. }
  173. type
  174. tppv = (pv_none,pv_proc,pv_func);
  175. var
  176. sc : TFPObjectList;
  177. hdef : tdef;
  178. arrayelementdef : tdef;
  179. vs : tparavarsym;
  180. i : longint;
  181. srsym : tsym;
  182. pv : tprocvardef;
  183. varspez : Tvarspez;
  184. defaultvalue : tconstsym;
  185. defaultrequired : boolean;
  186. old_block_type : tblock_type;
  187. currparast : tparasymtable;
  188. parseprocvar : tppv;
  189. locationstr : string;
  190. paranr : integer;
  191. dummytype : ttypesym;
  192. explicit_paraloc,
  193. need_array,
  194. is_univ: boolean;
  195. begin
  196. old_block_type:=block_type;
  197. explicit_paraloc:=false;
  198. consume(_LKLAMMER);
  199. { Delphi/Kylix supports nonsense like }
  200. { procedure p(); }
  201. if try_to_consume(_RKLAMMER) and
  202. not(m_tp7 in current_settings.modeswitches) then
  203. exit;
  204. { parsing a proc or procvar ? }
  205. currparast:=tparasymtable(pd.parast);
  206. { reset }
  207. sc:=TFPObjectList.create(false);
  208. defaultrequired:=false;
  209. paranr:=0;
  210. block_type:=bt_var;
  211. is_univ:=false;
  212. repeat
  213. parseprocvar:=pv_none;
  214. if try_to_consume(_VAR) then
  215. varspez:=vs_var
  216. else
  217. if try_to_consume(_CONST) then
  218. varspez:=vs_const
  219. else
  220. if (m_out in current_settings.modeswitches) and
  221. try_to_consume(_OUT) then
  222. varspez:=vs_out
  223. else
  224. if try_to_consume(_CONSTREF) then
  225. varspez:=vs_constref
  226. else
  227. if (m_mac in current_settings.modeswitches) and
  228. try_to_consume(_POINTPOINTPOINT) then
  229. begin
  230. include(pd.procoptions,po_varargs);
  231. break;
  232. end
  233. else
  234. if (m_nested_procvars in current_settings.modeswitches) and
  235. try_to_consume(_PROCEDURE) then
  236. begin
  237. parseprocvar:=pv_proc;
  238. varspez:=vs_const;
  239. end
  240. else
  241. if (m_nested_procvars in current_settings.modeswitches) and
  242. try_to_consume(_FUNCTION) then
  243. begin
  244. parseprocvar:=pv_func;
  245. varspez:=vs_const;
  246. end
  247. else
  248. varspez:=vs_value;
  249. defaultvalue:=nil;
  250. hdef:=nil;
  251. { read identifiers and insert with error type }
  252. sc.clear;
  253. repeat
  254. inc(paranr);
  255. vs:=tparavarsym.create(orgpattern,paranr*10,varspez,generrordef,[]);
  256. currparast.insert(vs);
  257. if assigned(vs.owner) then
  258. sc.add(vs)
  259. else
  260. vs.free;
  261. consume(_ID);
  262. until not try_to_consume(_COMMA);
  263. locationstr:='';
  264. { macpas anonymous procvar }
  265. if parseprocvar<>pv_none then
  266. begin
  267. { inline procvar definitions are always nested procvars }
  268. pv:=tprocvardef.create(normal_function_level+1);
  269. if token=_LKLAMMER then
  270. parse_parameter_dec(pv);
  271. if parseprocvar=pv_func then
  272. begin
  273. block_type:=bt_var_type;
  274. consume(_COLON);
  275. single_type(pv.returndef,[]);
  276. block_type:=bt_var;
  277. end;
  278. hdef:=pv;
  279. { possible proc directives }
  280. if check_proc_directive(true) then
  281. begin
  282. dummytype:=ttypesym.create('unnamed',hdef);
  283. parse_var_proc_directives(tsym(dummytype));
  284. dummytype.typedef:=nil;
  285. hdef.typesym:=nil;
  286. dummytype.free;
  287. end;
  288. { Add implicit hidden parameters and function result }
  289. handle_calling_convention(pv);
  290. {$ifdef jvm}
  291. { anonymous -> no name }
  292. jvm_create_procvar_class('',pv);
  293. {$endif}
  294. end
  295. else
  296. { read type declaration, force reading for value paras }
  297. if (token=_COLON) or (varspez=vs_value) then
  298. begin
  299. consume(_COLON);
  300. { check for an open array }
  301. need_array:=false;
  302. { bitpacked open array are not yet supported }
  303. if (token=_PACKED) and
  304. not(cs_bitpacking in current_settings.localswitches) then
  305. begin
  306. consume(_PACKED);
  307. need_array:=true;
  308. end;
  309. if (token=_ARRAY) or
  310. need_array then
  311. begin
  312. consume(_ARRAY);
  313. consume(_OF);
  314. { define range and type of range }
  315. hdef:=tarraydef.create(0,-1,s32inttype);
  316. { array of const ? }
  317. if (token=_CONST) and (m_objpas in current_settings.modeswitches) then
  318. begin
  319. consume(_CONST);
  320. srsym:=search_system_type('TVARREC');
  321. tarraydef(hdef).elementdef:=ttypesym(srsym).typedef;
  322. include(tarraydef(hdef).arrayoptions,ado_IsArrayOfConst);
  323. end
  324. else
  325. begin
  326. { define field type }
  327. single_type(arrayelementdef,[]);
  328. tarraydef(hdef).elementdef:=arrayelementdef;
  329. end;
  330. end
  331. else
  332. begin
  333. if (m_mac in current_settings.modeswitches) then
  334. is_univ:=try_to_consume(_UNIV);
  335. if try_to_consume(_TYPE) then
  336. hdef:=ctypedformaltype
  337. else
  338. begin
  339. block_type:=bt_var_type;
  340. single_type(hdef,[stoAllowSpecialization]);
  341. block_type:=bt_var;
  342. end;
  343. { open string ? }
  344. if is_shortstring(hdef) then
  345. begin
  346. case varspez of
  347. vs_var,vs_out,vs_constref:
  348. begin
  349. { not 100% Delphi-compatible: type xstr=string[255] cannot
  350. become an openstring there, while here it can }
  351. if (cs_openstring in current_settings.moduleswitches) and
  352. (tstringdef(hdef).len=255) then
  353. hdef:=openshortstringtype
  354. end;
  355. vs_value:
  356. begin
  357. { value "openstring" parameters don't make sense (the
  358. original string can never be modified, so there's no
  359. use in passing its original length), so change these
  360. into regular shortstring parameters (seems to be what
  361. Delphi also does) }
  362. if is_open_string(hdef) then
  363. hdef:=cshortstringtype;
  364. end;
  365. end;
  366. end;
  367. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  368. begin
  369. if (idtoken=_LOCATION) then
  370. begin
  371. consume(_LOCATION);
  372. locationstr:=cstringpattern;
  373. consume(_CSTRING);
  374. end
  375. else
  376. begin
  377. if explicit_paraloc then
  378. Message(parser_e_paraloc_all_paras);
  379. locationstr:='';
  380. end;
  381. end
  382. else
  383. locationstr:='';
  384. { default parameter }
  385. if (m_default_para in current_settings.modeswitches) then
  386. begin
  387. if try_to_consume(_EQ) then
  388. begin
  389. vs:=tparavarsym(sc[0]);
  390. if sc.count>1 then
  391. Message(parser_e_default_value_only_one_para);
  392. { prefix 'def' to the parameter name }
  393. defaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo);
  394. if assigned(defaultvalue) then
  395. begin
  396. include(defaultvalue.symoptions,sp_internal);
  397. pd.parast.insert(defaultvalue);
  398. end;
  399. defaultrequired:=true;
  400. end
  401. else
  402. begin
  403. if defaultrequired then
  404. Message1(parser_e_default_value_expected_for_para,vs.name);
  405. end;
  406. end;
  407. end;
  408. end
  409. else
  410. hdef:=cformaltype;
  411. { File types are only allowed for var and out parameters }
  412. if (hdef.typ=filedef) and
  413. not(varspez in [vs_out,vs_var]) then
  414. CGMessage(cg_e_file_must_call_by_reference);
  415. { Dispinterfaces are restricted to using only automatable types }
  416. if (pd.typ=procdef) and is_dispinterface(tprocdef(pd).struct) and
  417. not is_automatable(hdef) then
  418. Message1(type_e_not_automatable,hdef.typename);
  419. { univ cannot be used with types whose size is not known at compile
  420. time }
  421. if is_univ and
  422. not is_valid_univ_para_type(hdef) then
  423. Message1(parser_e_invalid_univ_para,hdef.typename);
  424. for i:=0 to sc.count-1 do
  425. begin
  426. vs:=tparavarsym(sc[i]);
  427. vs.univpara:=is_univ;
  428. { update varsym }
  429. vs.vardef:=hdef;
  430. vs.defaultconstsym:=defaultvalue;
  431. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  432. begin
  433. if locationstr<>'' then
  434. begin
  435. if sc.count>1 then
  436. Message(parser_e_paraloc_only_one_para);
  437. if (paranr>1) and not(explicit_paraloc) then
  438. Message(parser_e_paraloc_all_paras);
  439. explicit_paraloc:=true;
  440. include(vs.varoptions,vo_has_explicit_paraloc);
  441. if not(paramanager.parseparaloc(vs,upper(locationstr))) then
  442. message(parser_e_illegal_explicit_paraloc);
  443. end
  444. else
  445. if explicit_paraloc then
  446. Message(parser_e_paraloc_all_paras);
  447. end;
  448. end;
  449. until not try_to_consume(_SEMICOLON);
  450. if explicit_paraloc then
  451. begin
  452. pd.has_paraloc_info:=callerside;
  453. include(pd.procoptions,po_explicitparaloc);
  454. end;
  455. { remove parasymtable from stack }
  456. sc.free;
  457. { reset object options }
  458. block_type:=old_block_type;
  459. consume(_RKLAMMER);
  460. end;
  461. function parse_proc_head(astruct:tabstractrecorddef;potype:tproctypeoption;out pd:tprocdef):boolean;
  462. var
  463. hs : string;
  464. orgsp,sp : TIDString;
  465. srsym : tsym;
  466. checkstack : psymtablestackitem;
  467. procstartfilepos : tfileposinfo;
  468. searchagain : boolean;
  469. st,
  470. genericst: TSymtable;
  471. aprocsym : tprocsym;
  472. popclass : integer;
  473. ImplIntf : TImplementedInterface;
  474. old_parse_generic : boolean;
  475. old_current_structdef: tabstractrecorddef;
  476. old_current_genericdef,
  477. old_current_specializedef: tstoreddef;
  478. lasttoken,lastidtoken: ttoken;
  479. procedure parse_operator_name;
  480. begin
  481. if (lasttoken in [first_overloaded..last_overloaded]) then
  482. begin
  483. optoken:=token;
  484. end
  485. else
  486. begin
  487. case lasttoken of
  488. _CARET:
  489. Message1(parser_e_overload_operator_failed,'**');
  490. _ID:
  491. case lastidtoken of
  492. _ENUMERATOR:optoken:=_OP_ENUMERATOR;
  493. _EXPLICIT:optoken:=_OP_EXPLICIT;
  494. _INC:optoken:=_OP_INC;
  495. _DEC:optoken:=_OP_DEC;
  496. else
  497. if (m_delphi in current_settings.modeswitches) then
  498. case lastidtoken of
  499. _IMPLICIT:optoken:=_ASSIGNMENT;
  500. _NEGATIVE:optoken:=_MINUS;
  501. _POSITIVE:optoken:=_PLUS;
  502. _LOGICALNOT:optoken:=_OP_NOT;
  503. _IN:optoken:=_OP_IN;
  504. _EQUAL:optoken:=_EQ;
  505. _NOTEQUAL:optoken:=_NE;
  506. _GREATERTHAN:optoken:=_GT;
  507. _GREATERTHANOREQUAL:optoken:=_GTE;
  508. _LESSTHAN:optoken:=_LT;
  509. _LESSTHANOREQUAL:optoken:=_LTE;
  510. _ADD:optoken:=_PLUS;
  511. _SUBTRACT:optoken:=_MINUS;
  512. _MULTIPLY:optoken:=_STAR;
  513. _DIVIDE:optoken:=_SLASH;
  514. _INTDIVIDE:optoken:=_OP_DIV;
  515. _MODULUS:optoken:=_OP_MOD;
  516. _LEFTSHIFT:optoken:=_OP_SHL;
  517. _RIGHTSHIFT:optoken:=_OP_SHR;
  518. _LOGICALAND:optoken:=_OP_AND;
  519. _LOGICALOR:optoken:=_OP_OR;
  520. _LOGICALXOR:optoken:=_OP_XOR;
  521. _BITWISEAND:optoken:=_OP_AND;
  522. _BITWISEOR:optoken:=_OP_OR;
  523. _BITWISEXOR:optoken:=_OP_XOR;
  524. else
  525. Message1(parser_e_overload_operator_failed,'');
  526. end
  527. else
  528. Message1(parser_e_overload_operator_failed,'');
  529. end
  530. else
  531. Message1(parser_e_overload_operator_failed,'');
  532. end;
  533. end;
  534. sp:=overloaded_names[optoken];
  535. orgsp:=sp;
  536. end;
  537. procedure consume_proc_name;
  538. begin
  539. lasttoken:=token;
  540. lastidtoken:=idtoken;
  541. if potype=potype_operator then
  542. optoken:=NOTOKEN;
  543. if (potype=potype_operator) and (token<>_ID) then
  544. begin
  545. parse_operator_name;
  546. consume(token);
  547. end
  548. else
  549. begin
  550. sp:=pattern;
  551. orgsp:=orgpattern;
  552. consume(_ID);
  553. end;
  554. end;
  555. function search_object_name(sp:TIDString;gen_error:boolean):tsym;
  556. var
  557. storepos:tfileposinfo;
  558. srsymtable:TSymtable;
  559. begin
  560. storepos:=current_tokenpos;
  561. current_tokenpos:=procstartfilepos;
  562. searchsym(sp,result,srsymtable);
  563. if not assigned(result) then
  564. begin
  565. if gen_error then
  566. identifier_not_found(orgsp);
  567. result:=generrorsym;
  568. end;
  569. current_tokenpos:=storepos;
  570. end;
  571. function consume_generic_type_parameter:boolean;
  572. var
  573. i:integer;
  574. ok:boolean;
  575. sym:tsym;
  576. begin
  577. result:=not assigned(astruct)and(m_delphi in current_settings.modeswitches);
  578. if result then
  579. begin
  580. { a generic type parameter? }
  581. srsym:=search_object_name(sp,false);
  582. if (srsym.typ=typesym) and
  583. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) then
  584. begin
  585. astruct:=tabstractrecorddef(ttypesym(srsym).typedef);
  586. if (df_generic in astruct.defoptions) and try_to_consume(_LT) then
  587. begin
  588. ok:=true;
  589. i:=0;
  590. repeat
  591. if ok and (token=_ID) then
  592. begin
  593. ok:=false;
  594. while i<astruct.symtable.SymList.Count-1 do
  595. begin
  596. sym:=tsym(astruct.symtable.SymList[i]);
  597. if sp_generic_para in sym.symoptions then
  598. begin
  599. ok:=sym.Name=pattern;
  600. inc(i);
  601. break;
  602. end;
  603. inc(i);
  604. end;
  605. if not ok then
  606. Message1(type_e_generic_declaration_does_not_match,astruct.RttiName);
  607. end;
  608. consume(_ID);
  609. until not try_to_consume(_COMMA);
  610. if ok then
  611. while i<astruct.symtable.SymList.Count-1 do
  612. begin
  613. sym:=tsym(astruct.symtable.SymList[i]);
  614. if sp_generic_para in sym.symoptions then
  615. begin
  616. Message1(type_e_generic_declaration_does_not_match,astruct.RttiName);
  617. break;
  618. end;
  619. inc(i);
  620. end;
  621. consume(_GT);
  622. end
  623. else
  624. if (df_generic in astruct.defoptions) and (token=_POINT) then
  625. begin
  626. Message1(type_e_generic_declaration_does_not_match,astruct.RttiName);
  627. end
  628. else
  629. begin
  630. { not a method. routine name just accidentally match some structure name }
  631. astruct:=nil;
  632. if try_to_consume(_LT) then
  633. begin
  634. Message(type_e_type_parameters_are_not_allowed_here);
  635. repeat
  636. consume(_ID);
  637. until not try_to_consume(_COMMA);
  638. consume(_GT);
  639. end;
  640. end;
  641. end;
  642. end;
  643. end;
  644. begin
  645. { Save the position where this procedure really starts }
  646. procstartfilepos:=current_tokenpos;
  647. old_parse_generic:=parse_generic;
  648. result:=false;
  649. pd:=nil;
  650. aprocsym:=nil;
  651. consume_proc_name;
  652. { examine interface map: function/procedure iname.functionname=locfuncname }
  653. if assigned(astruct) and
  654. (astruct.typ=objectdef) and
  655. assigned(tobjectdef(astruct).ImplementedInterfaces) and
  656. (tobjectdef(astruct).ImplementedInterfaces.count>0) and
  657. try_to_consume(_POINT) then
  658. begin
  659. srsym:=search_object_name(sp,true);
  660. { qualifier is interface? }
  661. ImplIntf:=nil;
  662. if (srsym.typ=typesym) and
  663. (ttypesym(srsym).typedef.typ=objectdef) then
  664. ImplIntf:=tobjectdef(astruct).find_implemented_interface(tobjectdef(ttypesym(srsym).typedef));
  665. if ImplIntf=nil then
  666. Message(parser_e_interface_id_expected);
  667. { must be a directly implemented interface }
  668. if Assigned(ImplIntf.ImplementsGetter) then
  669. Message2(parser_e_implements_no_mapping,ImplIntf.IntfDef.typename,astruct.objrealname^);
  670. consume(_ID);
  671. { Create unique name <interface>.<method> }
  672. hs:=sp+'.'+pattern;
  673. consume(_EQ);
  674. if assigned(ImplIntf) and
  675. (token=_ID) then
  676. ImplIntf.AddMapping(hs,pattern);
  677. consume(_ID);
  678. result:=true;
  679. exit;
  680. end;
  681. { method ? }
  682. if (consume_generic_type_parameter or not assigned(astruct)) and
  683. (symtablestack.top.symtablelevel=main_program_level) and
  684. try_to_consume(_POINT) then
  685. begin
  686. repeat
  687. searchagain:=false;
  688. if not assigned(astruct) then
  689. srsym:=search_object_name(sp,true);
  690. { consume proc name }
  691. procstartfilepos:=current_tokenpos;
  692. consume_proc_name;
  693. { qualifier is class name ? }
  694. if (srsym.typ=typesym) and
  695. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) then
  696. begin
  697. astruct:=tabstractrecorddef(ttypesym(srsym).typedef);
  698. if (token<>_POINT) then
  699. if (potype in [potype_class_constructor,potype_class_destructor]) then
  700. sp:=lower(sp)
  701. else
  702. if (potype=potype_operator)and(optoken=NOTOKEN) then
  703. parse_operator_name;
  704. srsym:=tsym(astruct.symtable.Find(sp));
  705. if assigned(srsym) then
  706. begin
  707. if srsym.typ=procsym then
  708. aprocsym:=tprocsym(srsym)
  709. else
  710. if (srsym.typ=typesym) and
  711. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) then
  712. begin
  713. searchagain:=true;
  714. consume(_POINT);
  715. end
  716. else
  717. begin
  718. { we use a different error message for tp7 so it looks more compatible }
  719. if (m_fpc in current_settings.modeswitches) then
  720. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  721. else
  722. Message(parser_e_methode_id_expected);
  723. { rename the name to an unique name to avoid an
  724. error when inserting the symbol in the symtable }
  725. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  726. end;
  727. end
  728. else
  729. begin
  730. Message(parser_e_methode_id_expected);
  731. { recover by making it a normal procedure instead of method }
  732. astruct:=nil;
  733. end;
  734. end
  735. else
  736. Message(parser_e_class_id_expected);
  737. until not searchagain;
  738. end
  739. else
  740. begin
  741. { check for constructor/destructor/class operators which are not allowed here }
  742. if (not parse_only) and
  743. ((potype in [potype_constructor,potype_destructor,
  744. potype_class_constructor,potype_class_destructor]) or
  745. ((potype=potype_operator) and (m_delphi in current_settings.modeswitches))) then
  746. Message(parser_e_only_methods_allowed);
  747. repeat
  748. searchagain:=false;
  749. current_tokenpos:=procstartfilepos;
  750. if (potype=potype_operator)and(optoken=NOTOKEN) then
  751. parse_operator_name;
  752. srsym:=tsym(symtablestack.top.Find(sp));
  753. { Also look in the globalsymtable if we didn't found
  754. the symbol in the localsymtable }
  755. if not assigned(srsym) and
  756. not(parse_only) and
  757. (symtablestack.top=current_module.localsymtable) and
  758. assigned(current_module.globalsymtable) then
  759. srsym:=tsym(current_module.globalsymtable.Find(sp));
  760. { Check if overloaded is a procsym }
  761. if assigned(srsym) then
  762. begin
  763. if srsym.typ=procsym then
  764. aprocsym:=tprocsym(srsym)
  765. else
  766. begin
  767. { when the other symbol is a unit symbol then hide the unit
  768. symbol, this is not supported in tp7 }
  769. if not(m_tp7 in current_settings.modeswitches) and
  770. (srsym.typ=unitsym) then
  771. begin
  772. HideSym(srsym);
  773. searchagain:=true;
  774. end
  775. else
  776. begin
  777. { we use a different error message for tp7 so it looks more compatible }
  778. if (m_fpc in current_settings.modeswitches) then
  779. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  780. else
  781. Message1(sym_e_duplicate_id,srsym.realname);
  782. { rename the name to an unique name to avoid an
  783. error when inserting the symbol in the symtable }
  784. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  785. end;
  786. end;
  787. end;
  788. until not searchagain;
  789. end;
  790. { test again if assigned, it can be reset to recover }
  791. if not assigned(aprocsym) then
  792. begin
  793. { create a new procsym and set the real filepos }
  794. current_tokenpos:=procstartfilepos;
  795. { for operator we have only one procsym for each overloaded
  796. operation }
  797. if (potype=potype_operator) then
  798. begin
  799. aprocsym:=Tprocsym(symtablestack.top.Find(sp));
  800. if aprocsym=nil then
  801. aprocsym:=tprocsym.create('$'+sp);
  802. end
  803. else
  804. if (potype in [potype_class_constructor,potype_class_destructor]) then
  805. aprocsym:=tprocsym.create('$'+lower(sp))
  806. else
  807. aprocsym:=tprocsym.create(orgsp);
  808. symtablestack.top.insert(aprocsym);
  809. end;
  810. { to get the correct symtablelevel we must ignore ObjectSymtables }
  811. st:=nil;
  812. checkstack:=symtablestack.stack;
  813. while assigned(checkstack) do
  814. begin
  815. st:=checkstack^.symtable;
  816. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  817. break;
  818. checkstack:=checkstack^.next;
  819. end;
  820. pd:=tprocdef.create(st.symtablelevel+1);
  821. pd.struct:=astruct;
  822. pd.procsym:=aprocsym;
  823. pd.proctypeoption:=potype;
  824. { methods inherit df_generic or df_specialization from the objectdef }
  825. if assigned(pd.struct) and
  826. (pd.parast.symtablelevel=normal_function_level) then
  827. begin
  828. if (df_generic in pd.struct.defoptions) then
  829. begin
  830. include(pd.defoptions,df_generic);
  831. parse_generic:=true;
  832. end;
  833. if (df_specialization in pd.struct.defoptions) then
  834. begin
  835. include(pd.defoptions,df_specialization);
  836. { Find corresponding genericdef, we need it later to
  837. replay the tokens to generate the body }
  838. if not assigned(pd.struct.genericdef) then
  839. internalerror(200512113);
  840. genericst:=pd.struct.genericdef.GetSymtable(gs_record);
  841. if not assigned(genericst) then
  842. internalerror(200512114);
  843. { We are parsing the same objectdef, the def index numbers
  844. are the same }
  845. pd.genericdef:=tstoreddef(genericst.DefList[pd.owner.DefList.IndexOf(pd)]);
  846. if not assigned(pd.genericdef) or
  847. (pd.genericdef.typ<>procdef) then
  848. internalerror(200512115);
  849. end;
  850. end;
  851. { methods need to be exported }
  852. if assigned(astruct) and
  853. (
  854. (symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) or
  855. (symtablestack.top.symtablelevel=main_program_level)
  856. ) then
  857. include(pd.procoptions,po_global);
  858. { symbol options that need to be kept per procdef }
  859. pd.fileinfo:=procstartfilepos;
  860. pd.visibility:=symtablestack.top.currentvisibility;
  861. if symtablestack.top.currentlyoptional then
  862. include(pd.procoptions,po_optional);
  863. { parse parameters }
  864. if token=_LKLAMMER then
  865. begin
  866. { Add ObjectSymtable to be able to find nested type definitions }
  867. popclass:=0;
  868. if assigned(pd.struct) and
  869. (pd.parast.symtablelevel=normal_function_level) and
  870. not(symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) then
  871. begin
  872. popclass:=push_nested_hierarchy(pd.struct);
  873. old_current_structdef:=current_structdef;
  874. old_current_genericdef:=current_genericdef;
  875. old_current_specializedef:=current_specializedef;
  876. current_structdef:=pd.struct;
  877. if assigned(current_structdef) and (df_generic in current_structdef.defoptions) then
  878. current_genericdef:=current_structdef;
  879. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  880. current_specializedef:=current_structdef;
  881. end;
  882. { Add parameter symtable }
  883. if pd.parast.symtabletype<>staticsymtable then
  884. symtablestack.push(pd.parast);
  885. parse_parameter_dec(pd);
  886. if pd.parast.symtabletype<>staticsymtable then
  887. symtablestack.pop(pd.parast);
  888. if popclass>0 then
  889. begin
  890. current_structdef:=old_current_structdef;
  891. current_genericdef:=old_current_genericdef;
  892. current_specializedef:=old_current_specializedef;
  893. dec(popclass,pop_nested_hierarchy(pd.struct));
  894. if popclass<>0 then
  895. internalerror(201011260); // 11 nov 2010 index 0
  896. end;
  897. end;
  898. parse_generic:=old_parse_generic;
  899. result:=true;
  900. end;
  901. function parse_proc_dec(isclassmethod:boolean;astruct:tabstractrecorddef):tprocdef;
  902. var
  903. pd: tprocdef;
  904. locationstr: string;
  905. i: integer;
  906. found: boolean;
  907. procedure read_returndef(pd: tprocdef);
  908. var
  909. popclass: integer;
  910. old_parse_generic: boolean;
  911. old_current_structdef: tabstractrecorddef;
  912. old_current_genericdef,
  913. old_current_specializedef: tstoreddef;
  914. begin
  915. old_parse_generic:=parse_generic;
  916. { Add ObjectSymtable to be able to find generic type definitions }
  917. popclass:=0;
  918. if assigned(pd.struct) and
  919. (pd.parast.symtablelevel=normal_function_level) and
  920. not (symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) then
  921. begin
  922. popclass:=push_nested_hierarchy(pd.struct);
  923. parse_generic:=(df_generic in pd.struct.defoptions);
  924. old_current_structdef:=current_structdef;
  925. old_current_genericdef:=current_genericdef;
  926. old_current_specializedef:=current_specializedef;
  927. current_structdef:=pd.struct;
  928. if assigned(current_structdef) and (df_generic in current_structdef.defoptions) then
  929. current_genericdef:=current_structdef;
  930. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  931. current_specializedef:=current_structdef;
  932. end;
  933. single_type(pd.returndef,[stoAllowSpecialization]);
  934. if is_dispinterface(pd.struct) and not is_automatable(pd.returndef) then
  935. Message1(type_e_not_automatable,pd.returndef.typename);
  936. if popclass>0 then
  937. begin
  938. current_structdef:=old_current_structdef;
  939. current_genericdef:=old_current_genericdef;
  940. current_specializedef:=old_current_specializedef;
  941. dec(popclass,pop_nested_hierarchy(pd.struct));
  942. if popclass<>0 then
  943. internalerror(201012020);
  944. end;
  945. parse_generic:=old_parse_generic;
  946. end;
  947. begin
  948. locationstr:='';
  949. pd:=nil;
  950. case token of
  951. _FUNCTION :
  952. begin
  953. consume(_FUNCTION);
  954. if parse_proc_head(astruct,potype_function,pd) then
  955. begin
  956. { pd=nil when it is a interface mapping }
  957. if assigned(pd) then
  958. begin
  959. if try_to_consume(_COLON) then
  960. begin
  961. read_returndef(pd);
  962. if (target_info.system in [system_m68k_amiga]) then
  963. begin
  964. if (idtoken=_LOCATION) then
  965. begin
  966. if po_explicitparaloc in pd.procoptions then
  967. begin
  968. consume(_LOCATION);
  969. locationstr:=cstringpattern;
  970. consume(_CSTRING);
  971. end
  972. else
  973. { I guess this needs a new message... (KB) }
  974. Message(parser_e_paraloc_all_paras);
  975. end
  976. else
  977. begin
  978. if po_explicitparaloc in pd.procoptions then
  979. { assign default locationstr, if none specified }
  980. { and we've arguments with explicit paraloc }
  981. locationstr:='D0';
  982. end;
  983. end;
  984. end
  985. else
  986. begin
  987. if (
  988. parse_only and
  989. not(is_interface(pd.struct))
  990. ) or
  991. (m_repeat_forward in current_settings.modeswitches) then
  992. begin
  993. consume(_COLON);
  994. consume_all_until(_SEMICOLON);
  995. end;
  996. end;
  997. if isclassmethod then
  998. include(pd.procoptions,po_classmethod);
  999. end;
  1000. end
  1001. else
  1002. begin
  1003. { recover }
  1004. consume(_COLON);
  1005. consume_all_until(_SEMICOLON);
  1006. end;
  1007. end;
  1008. _PROCEDURE :
  1009. begin
  1010. consume(_PROCEDURE);
  1011. if parse_proc_head(astruct,potype_procedure,pd) then
  1012. begin
  1013. { pd=nil when it is an interface mapping }
  1014. if assigned(pd) then
  1015. begin
  1016. pd.returndef:=voidtype;
  1017. if isclassmethod then
  1018. include(pd.procoptions,po_classmethod);
  1019. end;
  1020. end;
  1021. end;
  1022. _CONSTRUCTOR :
  1023. begin
  1024. consume(_CONSTRUCTOR);
  1025. if isclassmethod then
  1026. parse_proc_head(astruct,potype_class_constructor,pd)
  1027. else
  1028. parse_proc_head(astruct,potype_constructor,pd);
  1029. if not isclassmethod and
  1030. assigned(pd) and
  1031. assigned(pd.struct) then
  1032. begin
  1033. { Set return type, class constructors return the
  1034. created instance, object constructors return boolean }
  1035. if is_class(pd.struct) or
  1036. is_record(pd.struct) or
  1037. is_javaclass(pd.struct) then
  1038. pd.returndef:=pd.struct
  1039. else
  1040. {$ifdef CPU64bitaddr}
  1041. pd.returndef:=bool64type;
  1042. {$else CPU64bitaddr}
  1043. pd.returndef:=bool32type;
  1044. {$endif CPU64bitaddr}
  1045. end
  1046. else
  1047. pd.returndef:=voidtype;
  1048. end;
  1049. _DESTRUCTOR :
  1050. begin
  1051. consume(_DESTRUCTOR);
  1052. if isclassmethod then
  1053. parse_proc_head(astruct,potype_class_destructor,pd)
  1054. else
  1055. parse_proc_head(astruct,potype_destructor,pd);
  1056. if assigned(pd) then
  1057. pd.returndef:=voidtype;
  1058. end;
  1059. else
  1060. if (token=_OPERATOR) or
  1061. (isclassmethod and (idtoken=_OPERATOR)) then
  1062. begin
  1063. consume(_OPERATOR);
  1064. parse_proc_head(astruct,potype_operator,pd);
  1065. if assigned(pd) then
  1066. begin
  1067. { operators always need to be searched in all units }
  1068. include(pd.procoptions,po_overload);
  1069. if pd.parast.symtablelevel>normal_function_level then
  1070. Message(parser_e_no_local_operator);
  1071. if isclassmethod then
  1072. include(pd.procoptions,po_classmethod);
  1073. if token<>_ID then
  1074. begin
  1075. if not(m_result in current_settings.modeswitches) then
  1076. consume(_ID);
  1077. end
  1078. else
  1079. begin
  1080. pd.resultname:=stringdup(orgpattern);
  1081. consume(_ID);
  1082. end;
  1083. if not try_to_consume(_COLON) then
  1084. begin
  1085. consume(_COLON);
  1086. pd.returndef:=generrordef;
  1087. consume_all_until(_SEMICOLON);
  1088. end
  1089. else
  1090. begin
  1091. read_returndef(pd);
  1092. { check that class operators have either return type of structure or }
  1093. { at least one argument of that type }
  1094. if (po_classmethod in pd.procoptions) and
  1095. (pd.returndef <> pd.struct) then
  1096. begin
  1097. found:=false;
  1098. for i := 0 to pd.parast.SymList.Count - 1 do
  1099. if tparavarsym(pd.parast.SymList[i]).vardef=pd.struct then
  1100. begin
  1101. found:=true;
  1102. break;
  1103. end;
  1104. if not found then
  1105. Message1(parser_e_at_least_one_argument_must_be_of_type,pd.struct.RttiName);
  1106. end;
  1107. if (optoken in [_EQ,_NE,_GT,_LT,_GTE,_LTE,_OP_IN]) and
  1108. ((pd.returndef.typ<>orddef) or
  1109. (torddef(pd.returndef).ordtype<>pasbool8)) then
  1110. Message(parser_e_comparative_operator_return_boolean);
  1111. if (optoken in [_ASSIGNMENT,_OP_EXPLICIT]) and
  1112. equal_defs(pd.returndef,tparavarsym(pd.parast.SymList[0]).vardef) and
  1113. (pd.returndef.typ<>undefineddef) and (tparavarsym(pd.parast.SymList[0]).vardef.typ<>undefineddef) then
  1114. message(parser_e_no_such_assignment)
  1115. else if not isoperatoracceptable(pd,optoken) then
  1116. Message(parser_e_overload_impossible);
  1117. end;
  1118. end
  1119. else
  1120. begin
  1121. { recover }
  1122. try_to_consume(_ID);
  1123. consume(_COLON);
  1124. consume_all_until(_SEMICOLON);
  1125. end;
  1126. end;
  1127. end;
  1128. { file types can't be function results }
  1129. if assigned(pd) and
  1130. (pd.returndef.typ=filedef) then
  1131. message(parser_e_illegal_function_result);
  1132. { support procedure proc stdcall export; }
  1133. if not(check_proc_directive(false)) then
  1134. begin
  1135. if (token=_COLON) then
  1136. begin
  1137. message(parser_e_field_not_allowed_here);
  1138. consume_all_until(_SEMICOLON);
  1139. end;
  1140. consume(_SEMICOLON);
  1141. end;
  1142. result:=pd;
  1143. if locationstr<>'' then
  1144. begin
  1145. if not(paramanager.parsefuncretloc(pd,upper(locationstr))) then
  1146. { I guess this needs a new message... (KB) }
  1147. message(parser_e_illegal_explicit_paraloc);
  1148. end;
  1149. end;
  1150. {****************************************************************************
  1151. Procedure directive handlers
  1152. ****************************************************************************}
  1153. procedure pd_far(pd:tabstractprocdef);
  1154. begin
  1155. Message1(parser_w_proc_directive_ignored,'FAR');
  1156. end;
  1157. procedure pd_near(pd:tabstractprocdef);
  1158. begin
  1159. Message1(parser_w_proc_directive_ignored,'NEAR');
  1160. end;
  1161. procedure pd_export(pd:tabstractprocdef);
  1162. begin
  1163. if pd.typ<>procdef then
  1164. internalerror(200304264);
  1165. if assigned(tprocdef(pd).struct) then
  1166. Message(parser_e_methods_dont_be_export);
  1167. if pd.parast.symtablelevel>normal_function_level then
  1168. Message(parser_e_dont_nest_export);
  1169. end;
  1170. procedure pd_forward(pd:tabstractprocdef);
  1171. begin
  1172. if pd.typ<>procdef then
  1173. internalerror(200304265);
  1174. tprocdef(pd).forwarddef:=true;
  1175. end;
  1176. procedure pd_alias(pd:tabstractprocdef);
  1177. begin
  1178. if pd.typ<>procdef then
  1179. internalerror(200304266);
  1180. consume(_COLON);
  1181. tprocdef(pd).aliasnames.insert(get_stringconst);
  1182. include(pd.procoptions,po_has_public_name);
  1183. end;
  1184. procedure pd_public(pd:tabstractprocdef);
  1185. begin
  1186. if pd.typ<>procdef then
  1187. internalerror(200304266);
  1188. if try_to_consume(_NAME) then
  1189. begin
  1190. tprocdef(pd).aliasnames.insert(get_stringconst);
  1191. include(pd.procoptions,po_has_public_name);
  1192. end;
  1193. end;
  1194. procedure pd_asmname(pd:tabstractprocdef);
  1195. begin
  1196. if pd.typ<>procdef then
  1197. internalerror(200304267);
  1198. if token=_CCHAR then
  1199. begin
  1200. tprocdef(pd).aliasnames.insert(target_info.Cprefix+pattern);
  1201. consume(_CCHAR)
  1202. end
  1203. else
  1204. begin
  1205. tprocdef(pd).aliasnames.insert(target_info.Cprefix+cstringpattern);
  1206. consume(_CSTRING);
  1207. end;
  1208. { we don't need anything else }
  1209. tprocdef(pd).forwarddef:=false;
  1210. end;
  1211. procedure pd_internconst(pd:tabstractprocdef);
  1212. var v:Tconstexprint;
  1213. begin
  1214. if pd.typ<>procdef then
  1215. internalerror(200304268);
  1216. consume(_COLON);
  1217. v:=get_intconst;
  1218. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1219. message(parser_e_range_check_error)
  1220. else
  1221. Tprocdef(pd).extnumber:=longint(v.svalue);
  1222. end;
  1223. procedure pd_internproc(pd:tabstractprocdef);
  1224. var v:Tconstexprint;
  1225. begin
  1226. if pd.typ<>procdef then
  1227. internalerror(200304268);
  1228. consume(_COLON);
  1229. v:=get_intconst;
  1230. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1231. message(parser_e_range_check_error)
  1232. else
  1233. Tprocdef(pd).extnumber:=longint(v.svalue);
  1234. { the proc is defined }
  1235. tprocdef(pd).forwarddef:=false;
  1236. end;
  1237. procedure pd_interrupt(pd:tabstractprocdef);
  1238. var v: Tconstexprint;
  1239. begin
  1240. if pd.parast.symtablelevel>normal_function_level then
  1241. Message(parser_e_dont_nest_interrupt);
  1242. {$ifdef FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  1243. if target_info.system in systems_interrupt_table then
  1244. begin
  1245. if token<>_SEMICOLON then
  1246. begin
  1247. pd.proccalloption:=pocall_interrupt;
  1248. v:=get_intconst;
  1249. Tprocdef(pd).interruptvector:=v.uvalue;
  1250. end;
  1251. end;
  1252. {$endif FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  1253. end;
  1254. procedure pd_abstract(pd:tabstractprocdef);
  1255. begin
  1256. if pd.typ<>procdef then
  1257. internalerror(200304269);
  1258. if is_objectpascal_helper(tprocdef(pd).struct) then
  1259. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_ABSTRACT].str);
  1260. if assigned(tprocdef(pd).struct) and
  1261. (oo_is_sealed in tprocdef(pd).struct.objectoptions) then
  1262. Message(parser_e_sealed_class_cannot_have_abstract_methods)
  1263. else if (po_virtualmethod in pd.procoptions) then
  1264. begin
  1265. include(pd.procoptions,po_abstractmethod);
  1266. { one more abstract method }
  1267. inc(tobjectdef(pd.owner.defowner).abstractcnt);
  1268. end
  1269. else
  1270. Message(parser_e_only_virtual_methods_abstract);
  1271. { the method is defined }
  1272. tprocdef(pd).forwarddef:=false;
  1273. end;
  1274. procedure pd_final(pd:tabstractprocdef);
  1275. begin
  1276. if pd.typ<>procdef then
  1277. internalerror(200910170);
  1278. if is_objectpascal_helper(tprocdef(pd).struct) and
  1279. (m_objfpc in current_settings.modeswitches) then
  1280. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_FINAL].str);
  1281. if (po_virtualmethod in pd.procoptions) then
  1282. include(pd.procoptions,po_finalmethod)
  1283. else
  1284. Message(parser_e_only_virtual_methods_final);
  1285. end;
  1286. procedure pd_enumerator(pd:tabstractprocdef);
  1287. begin
  1288. if pd.typ<>procdef then
  1289. internalerror(200910250);
  1290. if (token = _ID) then
  1291. begin
  1292. if pattern='MOVENEXT' then
  1293. begin
  1294. if oo_has_enumerator_movenext in tprocdef(pd).struct.objectoptions then
  1295. message(parser_e_only_one_enumerator_movenext);
  1296. pd.calcparas;
  1297. if (pd.proctypeoption = potype_function) and is_boolean(pd.returndef) and
  1298. (pd.minparacount = 0) then
  1299. begin
  1300. include(tprocdef(pd).struct.objectoptions, oo_has_enumerator_movenext);
  1301. include(pd.procoptions,po_enumerator_movenext);
  1302. end
  1303. else
  1304. Message(parser_e_enumerator_movenext_is_not_valid)
  1305. end
  1306. else
  1307. Message1(parser_e_invalid_enumerator_identifier, pattern);
  1308. consume(token);
  1309. end
  1310. else
  1311. Message(parser_e_enumerator_identifier_required);
  1312. end;
  1313. procedure pd_virtual(pd:tabstractprocdef);
  1314. {$ifdef WITHDMT}
  1315. var
  1316. pt : tnode;
  1317. {$endif WITHDMT}
  1318. begin
  1319. if (not assigned(pd.owner.defowner) or
  1320. not is_java_class_or_interface(tdef(pd.owner.defowner))) and
  1321. (po_external in pd.procoptions) then
  1322. Message1(parser_e_proc_dir_conflict,'EXTERNAL');
  1323. if pd.typ<>procdef then
  1324. internalerror(2003042610);
  1325. if (pd.proctypeoption=potype_constructor) and
  1326. is_object(tprocdef(pd).struct) then
  1327. Message(parser_e_constructor_cannot_be_not_virtual);
  1328. if is_objectpascal_helper(tprocdef(pd).struct) and
  1329. (m_objfpc in current_settings.modeswitches) then
  1330. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_VIRTUAL].str);
  1331. {$ifdef WITHDMT}
  1332. if is_object(tprocdef(pd).struct) and
  1333. (token<>_SEMICOLON) then
  1334. begin
  1335. { any type of parameter is allowed here! }
  1336. pt:=comp_expr(true);
  1337. if is_constintnode(pt) then
  1338. begin
  1339. include(pd.procoptions,po_msgint);
  1340. pd.messageinf.i:=pt.value;
  1341. end
  1342. else
  1343. Message(parser_e_ill_msg_expr);
  1344. disposetree(pt);
  1345. end;
  1346. {$endif WITHDMT}
  1347. end;
  1348. procedure pd_dispid(pd:tabstractprocdef);
  1349. var pt:Tnode;
  1350. begin
  1351. if pd.typ<>procdef then
  1352. internalerror(200604301);
  1353. pt:=comp_expr(true,false);
  1354. if is_constintnode(pt) then
  1355. if (Tordconstnode(pt).value<int64(low(longint))) or (Tordconstnode(pt).value>int64(high(longint))) then
  1356. message(parser_e_range_check_error)
  1357. else
  1358. Tprocdef(pd).dispid:=Tordconstnode(pt).value.svalue
  1359. else
  1360. message(parser_e_dispid_must_be_ord_const);
  1361. pt.free;
  1362. end;
  1363. procedure pd_static(pd:tabstractprocdef);
  1364. begin
  1365. if pd.typ=procdef then
  1366. include(tprocdef(pd).procsym.symoptions,sp_static);
  1367. include(pd.procoptions,po_staticmethod);
  1368. end;
  1369. procedure pd_override(pd:tabstractprocdef);
  1370. begin
  1371. if pd.typ<>procdef then
  1372. internalerror(2003042611);
  1373. if is_objectpascal_helper(tprocdef(pd).struct) then
  1374. begin
  1375. if m_objfpc in current_settings.modeswitches then
  1376. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_OVERRIDE].str)
  1377. end
  1378. else if not(is_class_or_interface_or_objc_or_java(tprocdef(pd).struct)) then
  1379. Message(parser_e_no_object_override)
  1380. else if is_objccategory(tprocdef(pd).struct) then
  1381. Message(parser_e_no_category_override)
  1382. else if (po_external in pd.procoptions) and
  1383. not is_objc_class_or_protocol(tprocdef(pd).struct) and
  1384. not is_cppclass(tprocdef(pd).struct) and
  1385. not is_java_class_or_interface(tprocdef(pd).struct) then
  1386. Message1(parser_e_proc_dir_conflict,'OVERRIDE');
  1387. end;
  1388. procedure pd_overload(pd:tabstractprocdef);
  1389. begin
  1390. if pd.typ<>procdef then
  1391. internalerror(2003042612);
  1392. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1393. end;
  1394. procedure pd_message(pd:tabstractprocdef);
  1395. var
  1396. pt : tnode;
  1397. paracnt : longint;
  1398. begin
  1399. if pd.typ<>procdef then
  1400. internalerror(2003042613);
  1401. if is_objectpascal_helper(tprocdef(pd).struct) then
  1402. begin
  1403. if m_objfpc in current_settings.modeswitches then
  1404. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_MESSAGE].str);
  1405. end
  1406. else
  1407. if not is_class(tprocdef(pd).struct) and
  1408. not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1409. Message(parser_e_msg_only_for_classes);
  1410. if ([po_msgstr,po_msgint]*pd.procoptions)<>[] then
  1411. Message(parser_e_multiple_messages);
  1412. { check parameter type }
  1413. if not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1414. begin
  1415. if po_external in pd.procoptions then
  1416. Message1(parser_e_proc_dir_conflict,'MESSAGE');
  1417. paracnt:=0;
  1418. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  1419. if paracnt<>1 then
  1420. Message(parser_e_ill_msg_param);
  1421. end;
  1422. pt:=comp_expr(true,false);
  1423. { message is 1-character long }
  1424. if is_constcharnode(pt) then
  1425. begin
  1426. include(pd.procoptions,po_msgstr);
  1427. tprocdef(pd).messageinf.str:=stringdup(chr(byte(tordconstnode(pt).value.uvalue and $FF)));
  1428. end
  1429. else if pt.nodetype=stringconstn then
  1430. begin
  1431. include(pd.procoptions,po_msgstr);
  1432. if (tstringconstnode(pt).len>255) then
  1433. Message(parser_e_message_string_too_long);
  1434. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).value_str);
  1435. end
  1436. else
  1437. if is_constintnode(pt) and
  1438. (is_class(tprocdef(pd).struct) or
  1439. is_objectpascal_helper(tprocdef(pd).struct)) then
  1440. begin
  1441. include(pd.procoptions,po_msgint);
  1442. if (Tordconstnode(pt).value<int64(low(Tprocdef(pd).messageinf.i))) or
  1443. (Tordconstnode(pt).value>int64(high(Tprocdef(pd).messageinf.i))) then
  1444. message(parser_e_range_check_error)
  1445. else
  1446. Tprocdef(pd).messageinf.i:=tordconstnode(pt).value.svalue;
  1447. end
  1448. else
  1449. Message(parser_e_ill_msg_expr);
  1450. { check whether the selector name is valid in case of Objective-C }
  1451. if (po_msgstr in pd.procoptions) and
  1452. is_objc_class_or_protocol(tprocdef(pd).struct) and
  1453. not objcvalidselectorname(@tprocdef(pd).messageinf.str^[1],length(tprocdef(pd).messageinf.str^)) then
  1454. Message1(type_e_invalid_objc_selector_name,tprocdef(pd).messageinf.str^);
  1455. pt.free;
  1456. end;
  1457. procedure pd_reintroduce(pd:tabstractprocdef);
  1458. begin
  1459. if pd.typ<>procdef then
  1460. internalerror(200401211);
  1461. if is_objectpascal_helper(tprocdef(pd).struct) then
  1462. begin
  1463. if m_objfpc in current_settings.modeswitches then
  1464. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_REINTRODUCE].str);
  1465. end
  1466. else
  1467. if not(is_class_or_interface_or_object(tprocdef(pd).struct)) and
  1468. not(is_objccategory(tprocdef(pd).struct)) then
  1469. Message(parser_e_no_object_reintroduce);
  1470. end;
  1471. procedure pd_syscall(pd:tabstractprocdef);
  1472. {$if defined(powerpc) or defined(m68k)}
  1473. var
  1474. vs : tparavarsym;
  1475. sym : tsym;
  1476. symtable : TSymtable;
  1477. v: Tconstexprint;
  1478. {$endif defined(powerpc) or defined(m68k)}
  1479. begin
  1480. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  1481. internalerror(2003042614);
  1482. tprocdef(pd).forwarddef:=false;
  1483. {$ifdef m68k}
  1484. if target_info.system in [system_m68k_amiga] then
  1485. begin
  1486. include(pd.procoptions,po_syscall_legacy);
  1487. if consume_sym(sym,symtable) then
  1488. begin
  1489. if (sym.typ=staticvarsym) and
  1490. (
  1491. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1492. is_32bitint(tabstractvarsym(sym).vardef)
  1493. ) then
  1494. begin
  1495. tprocdef(pd).libsym:=sym;
  1496. if po_syscall_legacy in tprocdef(pd).procoptions then
  1497. begin
  1498. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1499. paramanager.parseparaloc(vs,'A6');
  1500. pd.parast.insert(vs);
  1501. end
  1502. end
  1503. else
  1504. Message(parser_e_32bitint_or_pointer_variable_expected);
  1505. end;
  1506. (paramanager as tm68kparamanager).create_funcretloc_info(pd,calleeside);
  1507. (paramanager as tm68kparamanager).create_funcretloc_info(pd,callerside);
  1508. v:=get_intconst;
  1509. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1510. message(parser_e_range_check_error)
  1511. else
  1512. Tprocdef(pd).extnumber:=v.uvalue;
  1513. end;
  1514. {$endif m68k}
  1515. {$ifdef powerpc}
  1516. if target_info.system = system_powerpc_amiga then
  1517. begin
  1518. include(pd.procoptions,po_syscall_basesysv);
  1519. if consume_sym(sym,symtable) then
  1520. begin
  1521. if (sym.typ=staticvarsym) and
  1522. (
  1523. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1524. is_32bitint(tabstractvarsym(sym).vardef)
  1525. ) then
  1526. begin
  1527. tprocdef(pd).libsym:=sym;
  1528. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1529. pd.parast.insert(vs);
  1530. end
  1531. else
  1532. Message(parser_e_32bitint_or_pointer_variable_expected);
  1533. end;
  1534. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1535. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1536. v:=get_intconst;
  1537. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1538. message(parser_e_range_check_error)
  1539. else
  1540. Tprocdef(pd).extnumber:=v.uvalue;
  1541. end else
  1542. if target_info.system = system_powerpc_morphos then
  1543. begin
  1544. if idtoken=_LEGACY then
  1545. begin
  1546. consume(_LEGACY);
  1547. include(pd.procoptions,po_syscall_legacy);
  1548. end
  1549. else if idtoken=_SYSV then
  1550. begin
  1551. consume(_SYSV);
  1552. include(pd.procoptions,po_syscall_sysv);
  1553. end
  1554. else if idtoken=_BASESYSV then
  1555. begin
  1556. consume(_BASESYSV);
  1557. include(pd.procoptions,po_syscall_basesysv);
  1558. end
  1559. else if idtoken=_SYSVBASE then
  1560. begin
  1561. consume(_SYSVBASE);
  1562. include(pd.procoptions,po_syscall_sysvbase);
  1563. end
  1564. else if idtoken=_R12BASE then
  1565. begin
  1566. consume(_R12BASE);
  1567. include(pd.procoptions,po_syscall_r12base);
  1568. end
  1569. else
  1570. if syscall_convention='LEGACY' then
  1571. include(pd.procoptions,po_syscall_legacy)
  1572. else if syscall_convention='SYSV' then
  1573. include(pd.procoptions,po_syscall_sysv)
  1574. else if syscall_convention='BASESYSV' then
  1575. include(pd.procoptions,po_syscall_basesysv)
  1576. else if syscall_convention='SYSVBASE' then
  1577. include(pd.procoptions,po_syscall_sysvbase)
  1578. else if syscall_convention='R12BASE' then
  1579. include(pd.procoptions,po_syscall_r12base)
  1580. else
  1581. internalerror(2005010404);
  1582. if consume_sym(sym,symtable) then
  1583. begin
  1584. if (sym.typ=staticvarsym) and
  1585. (
  1586. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1587. is_32bitint(tabstractvarsym(sym).vardef)
  1588. ) then
  1589. begin
  1590. tprocdef(pd).libsym:=sym;
  1591. if po_syscall_legacy in tprocdef(pd).procoptions then
  1592. begin
  1593. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1594. paramanager.parseparaloc(vs,'A6');
  1595. pd.parast.insert(vs);
  1596. end
  1597. else if po_syscall_sysv in tprocdef(pd).procoptions then
  1598. begin
  1599. { Nothing to be done for sysv here for now, but this might change }
  1600. end
  1601. else if po_syscall_basesysv in tprocdef(pd).procoptions then
  1602. begin
  1603. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1604. pd.parast.insert(vs);
  1605. end
  1606. else if po_syscall_sysvbase in tprocdef(pd).procoptions then
  1607. begin
  1608. vs:=tparavarsym.create('$syscalllib',paranr_syscall_sysvbase,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1609. pd.parast.insert(vs);
  1610. end
  1611. else if po_syscall_r12base in tprocdef(pd).procoptions then
  1612. begin
  1613. vs:=tparavarsym.create('$syscalllib',paranr_syscall_r12base,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1614. paramanager.parseparaloc(vs,'R12');
  1615. pd.parast.insert(vs);
  1616. end
  1617. else
  1618. internalerror(2005010501);
  1619. end
  1620. else
  1621. Message(parser_e_32bitint_or_pointer_variable_expected);
  1622. end;
  1623. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1624. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1625. v:=get_intconst;
  1626. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1627. message(parser_e_range_check_error)
  1628. else
  1629. Tprocdef(pd).extnumber:=v.uvalue;
  1630. end;
  1631. {$endif powerpc}
  1632. end;
  1633. procedure pd_external(pd:tabstractprocdef);
  1634. {
  1635. If import_dll=nil the procedure is assumed to be in another
  1636. object file. In that object file it should have the name to
  1637. which import_name is pointing to. Otherwise, the procedure is
  1638. assumed to be in the DLL to which import_dll is pointing to. In
  1639. that case either import_nr<>0 or import_name<>nil is true, so
  1640. the procedure is either imported by number or by name. (DM)
  1641. }
  1642. var
  1643. hs : string;
  1644. v:Tconstexprint;
  1645. is_java_external: boolean;
  1646. begin
  1647. if pd.typ<>procdef then
  1648. internalerror(2003042615);
  1649. { Allow specifying a separate external name for methods in external Java
  1650. because its identifier naming constraints are laxer than FPC's
  1651. (e.g., case sensitive).
  1652. Limitation: only allows specifying the symbol name and not the package name,
  1653. and only for external classes/interfaces }
  1654. is_java_external:=
  1655. (pd.typ=procdef) and
  1656. is_java_class_or_interface(tdef(pd.owner.defowner)) and
  1657. (oo_is_external in tobjectdef(pd.owner.defowner).objectoptions);
  1658. with tprocdef(pd) do
  1659. begin
  1660. forwarddef:=false;
  1661. { forbid local external procedures }
  1662. if parast.symtablelevel>normal_function_level then
  1663. Message(parser_e_no_local_proc_external);
  1664. { If the procedure should be imported from a DLL, a constant string follows.
  1665. This isn't really correct, an contant string expression follows
  1666. so we check if an semicolon follows, else a string constant have to
  1667. follow (FK) }
  1668. if not is_java_external and
  1669. not(token=_SEMICOLON) and not(idtoken=_NAME) then
  1670. begin
  1671. { Always add library prefix and suffix to create an uniform name }
  1672. hs:=get_stringconst;
  1673. if ExtractFileExt(hs)='' then
  1674. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  1675. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  1676. hs:=target_info.sharedlibprefix+hs;
  1677. { the JVM expects java/lang/Object rather than java.lang.Object }
  1678. if target_info.system=system_jvm_java32 then
  1679. Replace(hs,'.','/');
  1680. import_dll:=stringdup(hs);
  1681. include(procoptions,po_has_importdll);
  1682. if (idtoken=_NAME) then
  1683. begin
  1684. consume(_NAME);
  1685. import_name:=stringdup(get_stringconst);
  1686. include(procoptions,po_has_importname);
  1687. if import_name^='' then
  1688. message(parser_e_empty_import_name);
  1689. end;
  1690. if (idtoken=_INDEX) then
  1691. begin
  1692. {After the word index follows the index number in the DLL.}
  1693. consume(_INDEX);
  1694. v:=get_intconst;
  1695. if (v<int64(low(import_nr))) or (v>int64(high(import_nr))) then
  1696. message(parser_e_range_check_error)
  1697. else
  1698. import_nr:=longint(v.svalue);
  1699. end;
  1700. { default is to used the realname of the procedure }
  1701. if (import_nr=0) and not assigned(import_name) then
  1702. begin
  1703. import_name:=stringdup(procsym.realname);
  1704. include(procoptions,po_has_importname);
  1705. end;
  1706. end
  1707. else
  1708. begin
  1709. if (idtoken=_NAME) or
  1710. is_java_external then
  1711. begin
  1712. consume(_NAME);
  1713. import_name:=stringdup(get_stringconst);
  1714. include(procoptions,po_has_importname);
  1715. if import_name^='' then
  1716. message(parser_e_empty_import_name);
  1717. end;
  1718. end;
  1719. end;
  1720. end;
  1721. procedure pd_weakexternal(pd:tabstractprocdef);
  1722. begin
  1723. if not(target_info.system in systems_weak_linking) then
  1724. message(parser_e_weak_external_not_supported)
  1725. else
  1726. pd_external(pd);
  1727. end;
  1728. type
  1729. pd_handler=procedure(pd:tabstractprocdef);
  1730. proc_dir_rec=record
  1731. idtok : ttoken;
  1732. pd_flags : tpdflags;
  1733. handler : pd_handler;
  1734. pocall : tproccalloption;
  1735. pooption : tprocoptions;
  1736. mutexclpocall : tproccalloptions;
  1737. mutexclpotype : tproctypeoptions;
  1738. mutexclpo : tprocoptions;
  1739. end;
  1740. const
  1741. {Should contain the number of procedure directives we support.}
  1742. num_proc_directives=42;
  1743. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  1744. (
  1745. (
  1746. idtok:_ABSTRACT;
  1747. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  1748. handler : @pd_abstract;
  1749. pocall : pocall_none;
  1750. pooption : [po_abstractmethod];
  1751. mutexclpocall : [pocall_internproc];
  1752. mutexclpotype : [];
  1753. mutexclpo : [po_exports,po_interrupt,po_inline]
  1754. ),(
  1755. idtok:_ALIAS;
  1756. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1757. handler : @pd_alias;
  1758. pocall : pocall_none;
  1759. pooption : [];
  1760. mutexclpocall : [];
  1761. mutexclpotype : [];
  1762. mutexclpo : [po_external,po_inline]
  1763. ),(
  1764. idtok:_ASMNAME;
  1765. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  1766. handler : @pd_asmname;
  1767. pocall : pocall_cdecl;
  1768. pooption : [po_external];
  1769. mutexclpocall : [pocall_internproc];
  1770. mutexclpotype : [];
  1771. mutexclpo : [po_external,po_inline]
  1772. ),(
  1773. idtok:_ASSEMBLER;
  1774. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1775. handler : nil;
  1776. pocall : pocall_none;
  1777. pooption : [po_assembler];
  1778. mutexclpocall : [];
  1779. mutexclpotype : [];
  1780. mutexclpo : [po_external]
  1781. ),(
  1782. idtok:_C; {same as cdecl for mode mac}
  1783. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1784. handler : nil;
  1785. pocall : pocall_cdecl;
  1786. pooption : [];
  1787. mutexclpocall : [];
  1788. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1789. mutexclpo : [po_assembler,po_external]
  1790. ),(
  1791. idtok:_CDECL;
  1792. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1793. handler : nil;
  1794. pocall : pocall_cdecl;
  1795. pooption : [];
  1796. mutexclpocall : [];
  1797. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1798. mutexclpo : [po_assembler,po_external]
  1799. ),(
  1800. idtok:_DISPID;
  1801. pd_flags : [pd_dispinterface];
  1802. handler : @pd_dispid;
  1803. pocall : pocall_none;
  1804. pooption : [po_dispid];
  1805. mutexclpocall : [pocall_internproc];
  1806. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1807. mutexclpo : [po_interrupt,po_external,po_inline]
  1808. ),(
  1809. idtok:_DYNAMIC;
  1810. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord];
  1811. handler : @pd_virtual;
  1812. pocall : pocall_none;
  1813. pooption : [po_virtualmethod];
  1814. mutexclpocall : [pocall_internproc];
  1815. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  1816. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  1817. ),(
  1818. idtok:_EXPORT;
  1819. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf,pd_notrecord,pd_nothelper];
  1820. handler : @pd_export;
  1821. pocall : pocall_none;
  1822. pooption : [po_exports,po_global];
  1823. mutexclpocall : [pocall_internproc];
  1824. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1825. mutexclpo : [po_external,po_interrupt,po_inline]
  1826. ),(
  1827. idtok:_EXTERNAL;
  1828. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper,pd_javaclass,pd_intfjava];
  1829. handler : @pd_external;
  1830. pocall : pocall_none;
  1831. pooption : [po_external];
  1832. mutexclpocall : [pocall_internproc,pocall_syscall];
  1833. { allowed for external cpp classes }
  1834. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  1835. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  1836. ),(
  1837. idtok:_FAR;
  1838. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  1839. handler : @pd_far;
  1840. pocall : pocall_none;
  1841. pooption : [];
  1842. mutexclpocall : [pocall_internproc];
  1843. mutexclpotype : [];
  1844. mutexclpo : [po_inline]
  1845. ),(
  1846. idtok:_FAR16;
  1847. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject,pd_notrecord,pd_nothelper];
  1848. handler : nil;
  1849. pocall : pocall_far16;
  1850. pooption : [];
  1851. mutexclpocall : [];
  1852. mutexclpotype : [];
  1853. mutexclpo : [po_external]
  1854. ),(
  1855. idtok:_FINAL;
  1856. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  1857. handler : @pd_final;
  1858. pocall : pocall_none;
  1859. pooption : [po_finalmethod];
  1860. mutexclpocall : [pocall_internproc];
  1861. mutexclpotype : [];
  1862. mutexclpo : [po_exports,po_interrupt,po_inline]
  1863. ),(
  1864. idtok:_FORWARD;
  1865. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  1866. handler : @pd_forward;
  1867. pocall : pocall_none;
  1868. pooption : [];
  1869. mutexclpocall : [pocall_internproc];
  1870. mutexclpotype : [];
  1871. mutexclpo : [po_external,po_inline]
  1872. ),(
  1873. idtok:_OLDFPCCALL;
  1874. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1875. handler : nil;
  1876. pocall : pocall_oldfpccall;
  1877. pooption : [];
  1878. mutexclpocall : [];
  1879. mutexclpotype : [];
  1880. mutexclpo : []
  1881. ),(
  1882. idtok:_INLINE;
  1883. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1884. handler : nil;
  1885. pocall : pocall_none;
  1886. pooption : [po_inline];
  1887. mutexclpocall : [];
  1888. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1889. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod]
  1890. ),(
  1891. idtok:_INTERNCONST;
  1892. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  1893. handler : @pd_internconst;
  1894. pocall : pocall_none;
  1895. pooption : [po_internconst];
  1896. mutexclpocall : [];
  1897. mutexclpotype : [potype_operator];
  1898. mutexclpo : []
  1899. ),(
  1900. idtok:_INTERNPROC;
  1901. pd_flags : [pd_interface,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  1902. handler : @pd_internproc;
  1903. pocall : pocall_internproc;
  1904. pooption : [];
  1905. mutexclpocall : [];
  1906. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1907. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  1908. ),(
  1909. idtok:_INTERRUPT;
  1910. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  1911. handler : @pd_interrupt;
  1912. pocall : pocall_oldfpccall;
  1913. pooption : [po_interrupt];
  1914. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,
  1915. pocall_pascal,pocall_far16,pocall_oldfpccall];
  1916. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1917. mutexclpo : [po_external,po_inline]
  1918. ),(
  1919. idtok:_IOCHECK;
  1920. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1921. handler : nil;
  1922. pocall : pocall_none;
  1923. pooption : [po_iocheck];
  1924. mutexclpocall : [pocall_internproc];
  1925. mutexclpotype : [];
  1926. mutexclpo : [po_external]
  1927. ),(
  1928. idtok:_LOCAL;
  1929. pd_flags : [pd_implemen,pd_body];
  1930. handler : nil;
  1931. pocall : pocall_none;
  1932. pooption : [po_kylixlocal];
  1933. mutexclpocall : [pocall_internproc,pocall_far16];
  1934. mutexclpotype : [];
  1935. mutexclpo : [po_external,po_exports]
  1936. ),(
  1937. idtok:_MESSAGE;
  1938. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_objcprot,pd_notrecord];
  1939. handler : @pd_message;
  1940. pocall : pocall_none;
  1941. pooption : []; { can be po_msgstr or po_msgint }
  1942. mutexclpocall : [pocall_internproc];
  1943. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1944. mutexclpo : [po_interrupt,po_inline]
  1945. ),(
  1946. idtok:_MWPASCAL;
  1947. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1948. handler : nil;
  1949. pocall : pocall_mwpascal;
  1950. pooption : [];
  1951. mutexclpocall : [];
  1952. mutexclpotype : [];
  1953. mutexclpo : []
  1954. ),(
  1955. idtok:_NEAR;
  1956. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf,pd_notrecord,pd_nothelper];
  1957. handler : @pd_near;
  1958. pocall : pocall_none;
  1959. pooption : [];
  1960. mutexclpocall : [pocall_internproc];
  1961. mutexclpotype : [];
  1962. mutexclpo : []
  1963. ),(
  1964. idtok:_NOSTACKFRAME;
  1965. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1966. handler : nil;
  1967. pocall : pocall_none;
  1968. pooption : [po_nostackframe];
  1969. mutexclpocall : [pocall_internproc];
  1970. mutexclpotype : [];
  1971. mutexclpo : []
  1972. ),(
  1973. idtok:_OVERLOAD;
  1974. pd_flags : [pd_implemen,pd_interface,pd_body,pd_javaclass,pd_intfjava];
  1975. handler : @pd_overload;
  1976. pocall : pocall_none;
  1977. pooption : [po_overload];
  1978. mutexclpocall : [pocall_internproc];
  1979. mutexclpotype : [];
  1980. mutexclpo : []
  1981. ),(
  1982. idtok:_OVERRIDE;
  1983. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_javaclass,pd_intfjava,pd_notrecord];
  1984. handler : @pd_override;
  1985. pocall : pocall_none;
  1986. pooption : [po_overridingmethod,po_virtualmethod];
  1987. mutexclpocall : [pocall_internproc];
  1988. mutexclpotype : [];
  1989. mutexclpo : [po_exports,po_interrupt,po_virtualmethod,po_inline]
  1990. ),(
  1991. idtok:_PASCAL;
  1992. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1993. handler : nil;
  1994. pocall : pocall_pascal;
  1995. pooption : [];
  1996. mutexclpocall : [];
  1997. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1998. mutexclpo : [po_external]
  1999. ),(
  2000. idtok:_PUBLIC;
  2001. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2002. handler : @pd_public;
  2003. pocall : pocall_none;
  2004. pooption : [po_public,po_global];
  2005. mutexclpocall : [pocall_internproc];
  2006. mutexclpotype : [];
  2007. mutexclpo : [po_external,po_inline]
  2008. ),(
  2009. idtok:_REGISTER;
  2010. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2011. handler : nil;
  2012. pocall : pocall_register;
  2013. pooption : [];
  2014. mutexclpocall : [];
  2015. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2016. mutexclpo : [po_external]
  2017. ),(
  2018. idtok:_REINTRODUCE;
  2019. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_notrecord];
  2020. handler : @pd_reintroduce;
  2021. pocall : pocall_none;
  2022. pooption : [po_reintroduce];
  2023. mutexclpocall : [pocall_internproc];
  2024. mutexclpotype : [];
  2025. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  2026. ),(
  2027. idtok:_SAFECALL;
  2028. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2029. handler : nil;
  2030. pocall : pocall_safecall;
  2031. pooption : [];
  2032. mutexclpocall : [];
  2033. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2034. mutexclpo : [po_external]
  2035. ),(
  2036. idtok:_SOFTFLOAT;
  2037. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2038. handler : nil;
  2039. pocall : pocall_softfloat;
  2040. pooption : [];
  2041. mutexclpocall : [];
  2042. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2043. { it's available with po_external because the libgcc floating point routines on the arm
  2044. uses this calling convention }
  2045. mutexclpo : []
  2046. ),(
  2047. idtok:_STATIC;
  2048. pd_flags : [pd_interface,pd_implemen,pd_body,pd_object,pd_record,pd_javaclass,pd_notobjintf];
  2049. handler : @pd_static;
  2050. pocall : pocall_none;
  2051. pooption : [po_staticmethod];
  2052. mutexclpocall : [pocall_internproc];
  2053. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2054. mutexclpo : [po_interrupt,po_exports]
  2055. ),(
  2056. idtok:_STDCALL;
  2057. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2058. handler : nil;
  2059. pocall : pocall_stdcall;
  2060. pooption : [];
  2061. mutexclpocall : [];
  2062. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2063. mutexclpo : [po_external]
  2064. ),(
  2065. idtok:_SYSCALL;
  2066. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  2067. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  2068. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  2069. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  2070. handler : @pd_syscall;
  2071. pocall : pocall_syscall;
  2072. pooption : [];
  2073. mutexclpocall : [];
  2074. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2075. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  2076. ),(
  2077. idtok:_VIRTUAL;
  2078. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2079. handler : @pd_virtual;
  2080. pocall : pocall_none;
  2081. pooption : [po_virtualmethod];
  2082. mutexclpocall : [pocall_internproc];
  2083. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2084. mutexclpo : [po_interrupt,po_exports,po_overridingmethod,po_inline]
  2085. ),(
  2086. idtok:_CPPDECL;
  2087. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2088. handler : nil;
  2089. pocall : pocall_cppdecl;
  2090. pooption : [];
  2091. mutexclpocall : [];
  2092. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2093. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  2094. ),(
  2095. idtok:_VARARGS;
  2096. pd_flags : [pd_interface,pd_implemen,pd_procvar,pd_objcclass,pd_objcprot];
  2097. handler : nil;
  2098. pocall : pocall_none;
  2099. pooption : [po_varargs];
  2100. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  2101. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  2102. mutexclpotype : [];
  2103. mutexclpo : [po_assembler,po_interrupt,po_inline]
  2104. ),(
  2105. idtok:_COMPILERPROC;
  2106. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2107. handler : nil;
  2108. pocall : pocall_none;
  2109. pooption : [po_compilerproc];
  2110. mutexclpocall : [];
  2111. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2112. mutexclpo : [po_interrupt]
  2113. ),(
  2114. idtok:_WEAKEXTERNAL;
  2115. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper];
  2116. handler : @pd_weakexternal;
  2117. pocall : pocall_none;
  2118. { mark it both external and weak external, so we don't have to
  2119. adapt all code for external symbols to also check for weak external
  2120. }
  2121. pooption : [po_external,po_weakexternal];
  2122. mutexclpocall : [pocall_internproc,pocall_syscall];
  2123. { allowed for external cpp classes }
  2124. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2125. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2126. ),(
  2127. idtok:_ENUMERATOR;
  2128. pd_flags : [pd_interface,pd_object,pd_record];
  2129. handler : @pd_enumerator;
  2130. pocall : pocall_none;
  2131. pooption : [];
  2132. mutexclpocall : [pocall_internproc];
  2133. mutexclpotype : [];
  2134. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  2135. )
  2136. );
  2137. function check_proc_directive(isprocvar:boolean):boolean;
  2138. var
  2139. i : longint;
  2140. begin
  2141. result:=false;
  2142. for i:=1 to num_proc_directives do
  2143. if proc_direcdata[i].idtok=idtoken then
  2144. begin
  2145. if ((not isprocvar) or
  2146. (pd_procvar in proc_direcdata[i].pd_flags)) and
  2147. { don't eat a public directive in classes }
  2148. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  2149. result:=true;
  2150. exit;
  2151. end;
  2152. end;
  2153. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  2154. {
  2155. Parse the procedure directive, returns true if a correct directive is found
  2156. }
  2157. var
  2158. p : longint;
  2159. found : boolean;
  2160. name : TIDString;
  2161. begin
  2162. parse_proc_direc:=false;
  2163. name:=tokeninfo^[idtoken].str;
  2164. found:=false;
  2165. { Hint directive? Then exit immediatly }
  2166. if (m_hintdirective in current_settings.modeswitches) then
  2167. begin
  2168. case idtoken of
  2169. _LIBRARY,
  2170. _PLATFORM,
  2171. _UNIMPLEMENTED,
  2172. _EXPERIMENTAL,
  2173. _DEPRECATED :
  2174. exit;
  2175. end;
  2176. end;
  2177. { C directive is MacPas only, because it breaks too much existing code
  2178. on other platforms (PFV) }
  2179. if (idtoken=_C) and
  2180. not(m_mac in current_settings.modeswitches) then
  2181. exit;
  2182. { retrieve data for directive if found }
  2183. for p:=1 to num_proc_directives do
  2184. if proc_direcdata[p].idtok=idtoken then
  2185. begin
  2186. found:=true;
  2187. break;
  2188. end;
  2189. { Check if the procedure directive is known }
  2190. if not found then
  2191. begin
  2192. { parsing a procvar type the name can be any
  2193. next variable !! }
  2194. if ((pdflags * [pd_procvar,pd_object,pd_record,pd_objcclass,pd_objcprot])=[]) and
  2195. not(idtoken=_PROPERTY) then
  2196. Message1(parser_w_unknown_proc_directive_ignored,name);
  2197. exit;
  2198. end;
  2199. { check if method and directive not for object, like public.
  2200. This needs to be checked also for procvars }
  2201. if (pd_notobject in proc_direcdata[p].pd_flags) and
  2202. (symtablestack.top.symtabletype=ObjectSymtable) and
  2203. { directive allowed for cpp classes? }
  2204. not((pd_cppobject in proc_direcdata[p].pd_flags) and is_cppclass(tdef(symtablestack.top.defowner))) and
  2205. not((pd_javaclass in proc_direcdata[p].pd_flags) and is_javaclass(tdef(symtablestack.top.defowner))) and
  2206. not((pd_intfjava in proc_direcdata[p].pd_flags) and is_javainterface(tdef(symtablestack.top.defowner))) then
  2207. exit;
  2208. if (pd_notrecord in proc_direcdata[p].pd_flags) and
  2209. (symtablestack.top.symtabletype=recordsymtable) then
  2210. exit;
  2211. { check if method and directive not for java class }
  2212. if not(pd_javaclass in proc_direcdata[p].pd_flags) and
  2213. is_javaclass(tdef(symtablestack.top.defowner)) then
  2214. exit;
  2215. { check if method and directive not for java interface }
  2216. if not(pd_intfjava in proc_direcdata[p].pd_flags) and
  2217. is_javainterface(tdef(symtablestack.top.defowner)) then
  2218. exit;
  2219. { Conflicts between directives ? }
  2220. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  2221. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  2222. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  2223. begin
  2224. Message1(parser_e_proc_dir_conflict,name);
  2225. exit;
  2226. end;
  2227. { set calling convention }
  2228. if proc_direcdata[p].pocall<>pocall_none then
  2229. begin
  2230. if (po_hascallingconvention in pd.procoptions) then
  2231. begin
  2232. Message2(parser_w_proc_overriding_calling,
  2233. proccalloptionStr[pd.proccalloption],
  2234. proccalloptionStr[proc_direcdata[p].pocall]);
  2235. end;
  2236. { check if the target processor supports this calling convention }
  2237. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  2238. begin
  2239. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  2240. { recover }
  2241. proc_direcdata[p].pocall:=pocall_stdcall;
  2242. end;
  2243. pd.proccalloption:=proc_direcdata[p].pocall;
  2244. include(pd.procoptions,po_hascallingconvention);
  2245. end;
  2246. if pd.typ=procdef then
  2247. begin
  2248. { Check if the directive is only for objects }
  2249. if (pd_object in proc_direcdata[p].pd_flags) and
  2250. not assigned(tprocdef(pd).struct) then
  2251. exit;
  2252. { Check if the directive is only for records }
  2253. if (pd_record in proc_direcdata[p].pd_flags) and
  2254. not assigned(tprocdef(pd).struct) then
  2255. exit;
  2256. { check if method and directive not for interface }
  2257. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  2258. is_interface(tprocdef(pd).struct) then
  2259. exit;
  2260. { check if method and directive not for interface }
  2261. if is_dispinterface(tprocdef(pd).struct) and
  2262. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  2263. exit;
  2264. { check if method and directive not for objcclass }
  2265. if is_objcclass(tprocdef(pd).struct) and
  2266. not(pd_objcclass in proc_direcdata[p].pd_flags) then
  2267. exit;
  2268. { check if method and directive not for objcprotocol }
  2269. if is_objcprotocol(tprocdef(pd).struct) and
  2270. not(pd_objcprot in proc_direcdata[p].pd_flags) then
  2271. exit;
  2272. { check if method and directive not for record/class helper }
  2273. if is_objectpascal_helper(tprocdef(pd).struct) and
  2274. (pd_nothelper in proc_direcdata[p].pd_flags) then
  2275. end;
  2276. { consume directive, and turn flag on }
  2277. consume(token);
  2278. parse_proc_direc:=true;
  2279. { Check the pd_flags if the directive should be allowed }
  2280. if (pd_interface in pdflags) and
  2281. not(pd_interface in proc_direcdata[p].pd_flags) then
  2282. begin
  2283. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  2284. exit;
  2285. end;
  2286. if (pd_implemen in pdflags) and
  2287. not(pd_implemen in proc_direcdata[p].pd_flags) then
  2288. begin
  2289. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  2290. exit;
  2291. end;
  2292. if (pd_procvar in pdflags) and
  2293. not(pd_procvar in proc_direcdata[p].pd_flags) then
  2294. begin
  2295. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  2296. exit;
  2297. end;
  2298. { Return the new pd_flags }
  2299. if not(pd_body in proc_direcdata[p].pd_flags) then
  2300. exclude(pdflags,pd_body);
  2301. { Add the correct flag }
  2302. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  2303. { Call the handler }
  2304. if pointer(proc_direcdata[p].handler)<>nil then
  2305. proc_direcdata[p].handler(pd);
  2306. end;
  2307. function proc_get_importname(pd:tprocdef):string;
  2308. var
  2309. dllname, importname : string;
  2310. begin
  2311. result:='';
  2312. if not(po_external in pd.procoptions) then
  2313. internalerror(200412151);
  2314. { external name or number is specified }
  2315. if assigned(pd.import_name) or (pd.import_nr<>0) then
  2316. begin
  2317. if assigned(pd.import_dll) then
  2318. dllname:=pd.import_dll^
  2319. else
  2320. dllname:='';
  2321. if assigned(pd.import_name) then
  2322. importname:=pd.import_name^
  2323. else
  2324. importname:='';
  2325. proc_get_importname:=make_dllmangledname(dllname,
  2326. importname,pd.import_nr,pd.proccalloption);
  2327. end
  2328. else
  2329. begin
  2330. { Default names when importing variables }
  2331. case pd.proccalloption of
  2332. pocall_cdecl :
  2333. begin
  2334. if assigned(pd.struct) then
  2335. result:=target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname
  2336. else
  2337. result:=target_info.Cprefix+pd.procsym.realname;
  2338. end;
  2339. pocall_cppdecl :
  2340. begin
  2341. result:=target_info.Cprefix+pd.cplusplusmangledname;
  2342. end;
  2343. else
  2344. begin
  2345. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  2346. { but according to MacPas mode description
  2347. Cprefix should still be used PM }
  2348. if (m_mac in current_settings.modeswitches) then
  2349. result:=target_info.Cprefix+tprocdef(pd).procsym.realname;
  2350. end;
  2351. end;
  2352. end;
  2353. end;
  2354. procedure proc_set_mangledname(pd:tprocdef);
  2355. var
  2356. s : string;
  2357. begin
  2358. { When the mangledname is already set we aren't allowed to change
  2359. it because it can already be used somewhere (PFV) }
  2360. if not(po_has_mangledname in pd.procoptions) then
  2361. begin
  2362. if (po_external in pd.procoptions) then
  2363. begin
  2364. { External Procedures are only allowed to change the mangledname
  2365. in their first declaration }
  2366. if (pd.forwarddef or (not pd.hasforward)) then
  2367. begin
  2368. s:=proc_get_importname(pd);
  2369. if s<>'' then
  2370. begin
  2371. pd.setmangledname(s);
  2372. end;
  2373. end;
  2374. end
  2375. else
  2376. { Normal procedures }
  2377. begin
  2378. if (po_compilerproc in pd.procoptions) then
  2379. begin
  2380. pd.setmangledname(lower(pd.procsym.name));
  2381. end;
  2382. end;
  2383. end;
  2384. { Public/exported alias names }
  2385. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  2386. not(po_has_public_name in pd.procoptions) then
  2387. begin
  2388. case pd.proccalloption of
  2389. pocall_cdecl :
  2390. begin
  2391. if assigned(pd.struct) then
  2392. pd.aliasnames.insert(target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname)
  2393. else
  2394. begin
  2395. { Export names are not mangled on Windows and OS/2, see also pexports.pas }
  2396. if (target_info.system in (systems_all_windows+[system_i386_emx, system_i386_os2])) and
  2397. (po_exports in pd.procoptions) then
  2398. pd.aliasnames.insert(pd.procsym.realname)
  2399. else
  2400. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  2401. end;
  2402. end;
  2403. pocall_cppdecl :
  2404. begin
  2405. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  2406. end;
  2407. end;
  2408. { prevent adding the alias a second time }
  2409. include(pd.procoptions,po_has_public_name);
  2410. end;
  2411. end;
  2412. procedure handle_calling_convention(pd:tabstractprocdef);
  2413. begin
  2414. { set the default calling convention if none provided }
  2415. if (pd.typ=procdef) and
  2416. (is_objc_class_or_protocol(tprocdef(pd).struct) or
  2417. is_cppclass(tprocdef(pd).struct)) then
  2418. begin
  2419. { none of the explicit calling conventions should be allowed }
  2420. if (po_hascallingconvention in pd.procoptions) then
  2421. internalerror(2009032501);
  2422. if is_cppclass(tprocdef(pd).struct) then
  2423. pd.proccalloption:=pocall_cppdecl
  2424. else
  2425. pd.proccalloption:=pocall_cdecl;
  2426. end
  2427. else if not(po_hascallingconvention in pd.procoptions) then
  2428. pd.proccalloption:=current_settings.defproccall
  2429. else
  2430. begin
  2431. if pd.proccalloption=pocall_none then
  2432. internalerror(200309081);
  2433. end;
  2434. { handle proccall specific settings }
  2435. case pd.proccalloption of
  2436. pocall_cdecl,
  2437. pocall_cppdecl :
  2438. begin
  2439. { check C cdecl para types }
  2440. check_c_para(pd);
  2441. end;
  2442. pocall_far16 :
  2443. begin
  2444. { Temporary stub, must be rewritten to support OS/2 far16 }
  2445. Message1(parser_w_proc_directive_ignored,'FAR16');
  2446. end;
  2447. end;
  2448. { Inlining is enabled and supported? }
  2449. if (po_inline in pd.procoptions) and
  2450. not(cs_do_inline in current_settings.localswitches) then
  2451. begin
  2452. { Give an error if inline is not supported by the compiler mode,
  2453. otherwise only give a warning that this procedure will not be inlined }
  2454. if not(m_default_inline in current_settings.modeswitches) then
  2455. Message(parser_e_proc_inline_not_supported)
  2456. else
  2457. Message(parser_w_inlining_disabled);
  2458. exclude(pd.procoptions,po_inline);
  2459. end;
  2460. { For varargs directive also cdecl and external must be defined }
  2461. if (po_varargs in pd.procoptions) then
  2462. begin
  2463. { check first for external in the interface, if available there
  2464. then the cdecl must also be there since there is no implementation
  2465. available to contain it }
  2466. if parse_only then
  2467. begin
  2468. { if external is available, then cdecl must also be available,
  2469. procvars don't need external }
  2470. if not((po_external in pd.procoptions) or
  2471. (pd.typ=procvardef) or
  2472. { for objcclasses this is checked later, because the entire
  2473. class may be external. }
  2474. is_objc_class_or_protocol(tprocdef(pd).struct)) and
  2475. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal])) then
  2476. Message(parser_e_varargs_need_cdecl_and_external);
  2477. end
  2478. else
  2479. begin
  2480. { both must be defined now }
  2481. if not((po_external in pd.procoptions) or
  2482. (pd.typ=procvardef)) or
  2483. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal])) then
  2484. Message(parser_e_varargs_need_cdecl_and_external);
  2485. end;
  2486. end;
  2487. { insert hidden high parameters }
  2488. pd.parast.SymList.ForEachCall(@insert_hidden_para,pd);
  2489. { insert hidden self parameter }
  2490. insert_self_and_vmt_para(pd);
  2491. { insert funcret parameter if required }
  2492. insert_funcret_para(pd);
  2493. { Make var parameters regable, this must be done after the calling
  2494. convention is set. }
  2495. { this must be done before parentfp is insert, because getting all cases
  2496. where parentfp must be in a memory location isn't catched properly so
  2497. we put parentfp never in a register }
  2498. pd.parast.SymList.ForEachCall(@set_addr_param_regable,pd);
  2499. { insert parentfp parameter if required }
  2500. insert_parentfp_para(pd);
  2501. { Calculate parameter tlist }
  2502. pd.calcparas;
  2503. end;
  2504. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  2505. {
  2506. Parse the procedure directives. It does not matter if procedure directives
  2507. are written using ;procdir; or ['procdir'] syntax.
  2508. }
  2509. var
  2510. res : boolean;
  2511. begin
  2512. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  2513. begin
  2514. tprocdef(pd).aliasnames.insert(target_info.Cprefix+tprocdef(pd).procsym.realname);
  2515. include(pd.procoptions,po_public);
  2516. include(pd.procoptions,po_has_public_name);
  2517. include(pd.procoptions,po_global);
  2518. end;
  2519. { methods from external class definitions are all external themselves }
  2520. if (pd.typ=procdef) and
  2521. assigned(tprocdef(pd).struct) and
  2522. (tprocdef(pd).struct.typ=objectdef) and
  2523. (oo_is_external in tobjectdef(tprocdef(pd).struct).objectoptions) then
  2524. tprocdef(pd).make_external;
  2525. { Class constructors and destructor are static class methods in real. }
  2526. { There are many places in the compiler where either class or static }
  2527. { method flag changes the behavior. It is simplier to add them to }
  2528. { the class constructors/destructors options than to fix all the }
  2529. { occurencies. (Paul) }
  2530. if pd.proctypeoption in [potype_class_constructor,potype_class_destructor] then
  2531. begin
  2532. include(pd.procoptions,po_classmethod);
  2533. include(pd.procoptions,po_staticmethod);
  2534. end;
  2535. while token in [_ID,_LECKKLAMMER] do
  2536. begin
  2537. if try_to_consume(_LECKKLAMMER) then
  2538. begin
  2539. repeat
  2540. parse_proc_direc(pd,pdflags);
  2541. until not try_to_consume(_COMMA);
  2542. consume(_RECKKLAMMER);
  2543. { we always expect at least '[];' }
  2544. res:=true;
  2545. end
  2546. else
  2547. begin
  2548. res:=parse_proc_direc(pd,pdflags);
  2549. end;
  2550. { A procedure directive normally followed by a semicolon, but in
  2551. a const section or reading a type we should stop when _EQ is found,
  2552. because a constant/default value follows }
  2553. if res then
  2554. begin
  2555. if (block_type=bt_const_type) and
  2556. (token=_EQ) then
  2557. break;
  2558. { support procedure proc;stdcall export; }
  2559. if not(check_proc_directive((pd.typ=procvardef))) then
  2560. begin
  2561. { support "record p : procedure stdcall end;" and
  2562. "var p : procedure stdcall = nil;" }
  2563. if (pd_procvar in pdflags) and
  2564. (token in [_END,_RKLAMMER,_EQ]) then
  2565. break
  2566. else
  2567. begin
  2568. if (token=_COLON) then
  2569. begin
  2570. Message(parser_e_field_not_allowed_here);
  2571. consume_all_until(_SEMICOLON);
  2572. end;
  2573. consume(_SEMICOLON)
  2574. end;
  2575. end;
  2576. end
  2577. else
  2578. break;
  2579. end;
  2580. end;
  2581. procedure parse_var_proc_directives(sym:tsym);
  2582. var
  2583. pdflags : tpdflags;
  2584. pd : tabstractprocdef;
  2585. begin
  2586. pdflags:=[pd_procvar];
  2587. pd:=nil;
  2588. case sym.typ of
  2589. fieldvarsym,
  2590. staticvarsym,
  2591. localvarsym,
  2592. paravarsym :
  2593. pd:=tabstractprocdef(tabstractvarsym(sym).vardef);
  2594. typesym :
  2595. pd:=tabstractprocdef(ttypesym(sym).typedef);
  2596. else
  2597. internalerror(2003042617);
  2598. end;
  2599. if pd.typ<>procvardef then
  2600. internalerror(2003042618);
  2601. { names should never be used anyway }
  2602. parse_proc_directives(pd,pdflags);
  2603. end;
  2604. procedure parse_object_proc_directives(pd:tabstractprocdef);
  2605. var
  2606. pdflags : tpdflags;
  2607. begin
  2608. pdflags:=[pd_object];
  2609. parse_proc_directives(pd,pdflags);
  2610. end;
  2611. procedure parse_record_proc_directives(pd:tabstractprocdef);
  2612. var
  2613. pdflags : tpdflags;
  2614. begin
  2615. pdflags:=[pd_record];
  2616. parse_proc_directives(pd,pdflags);
  2617. end;
  2618. function proc_add_definition(var currpd:tprocdef):boolean;
  2619. {
  2620. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  2621. forwarddef is found and reused it returns true
  2622. }
  2623. var
  2624. fwpd : tprocdef;
  2625. currparasym,
  2626. fwparasym : tsym;
  2627. currparacnt,
  2628. fwparacnt,
  2629. curridx,
  2630. fwidx,
  2631. i : longint;
  2632. po_comp : tprocoptions;
  2633. paracompopt: tcompare_paras_options;
  2634. forwardfound : boolean;
  2635. symentry: TSymEntry;
  2636. begin
  2637. forwardfound:=false;
  2638. { check overloaded functions if the same function already exists }
  2639. for i:=0 to tprocsym(currpd.procsym).ProcdefList.Count-1 do
  2640. begin
  2641. fwpd:=tprocdef(tprocsym(currpd.procsym).ProcdefList[i]);
  2642. { can happen for internally generated routines }
  2643. if (fwpd=currpd) then
  2644. begin
  2645. result:=true;
  2646. exit;
  2647. end;
  2648. { Skip overloaded definitions that are declared in other units }
  2649. if fwpd.procsym<>currpd.procsym then
  2650. continue;
  2651. { check the parameters, for delphi/tp it is possible to
  2652. leave the parameters away in the implementation (forwarddef=false).
  2653. But for an overload declared function this is not allowed }
  2654. if { check if empty implementation arguments match is allowed }
  2655. (
  2656. not(m_repeat_forward in current_settings.modeswitches) and
  2657. not(currpd.forwarddef) and
  2658. is_bareprocdef(currpd) and
  2659. not(po_overload in fwpd.procoptions)
  2660. ) or
  2661. { check arguments, we need to check only the user visible parameters. The hidden parameters
  2662. can be in a different location because of the calling convention, eg. L-R vs. R-L order (PFV) }
  2663. (
  2664. (compare_paras(currpd.paras,fwpd.paras,cp_none,[cpo_comparedefaultvalue,cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv])=te_exact) and
  2665. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)=te_exact)
  2666. ) then
  2667. begin
  2668. { Check if we've found the forwarddef, if found then
  2669. we need to update the forward def with the current
  2670. implementation settings }
  2671. if fwpd.forwarddef then
  2672. begin
  2673. forwardfound:=true;
  2674. if not(m_repeat_forward in current_settings.modeswitches) and
  2675. (fwpd.proccalloption<>currpd.proccalloption) then
  2676. paracompopt:=[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv]
  2677. else
  2678. paracompopt:=[cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv];
  2679. { Check calling convention }
  2680. if (fwpd.proccalloption<>currpd.proccalloption) then
  2681. begin
  2682. { In delphi it is possible to specify the calling
  2683. convention in the interface or implementation if
  2684. there was no convention specified in the other
  2685. part }
  2686. if (m_delphi in current_settings.modeswitches) then
  2687. begin
  2688. if not(po_hascallingconvention in currpd.procoptions) then
  2689. currpd.proccalloption:=fwpd.proccalloption
  2690. else
  2691. if not(po_hascallingconvention in fwpd.procoptions) then
  2692. fwpd.proccalloption:=currpd.proccalloption
  2693. else
  2694. begin
  2695. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2696. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2697. { restore interface settings }
  2698. currpd.proccalloption:=fwpd.proccalloption;
  2699. end;
  2700. end
  2701. else
  2702. begin
  2703. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2704. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2705. { restore interface settings }
  2706. currpd.proccalloption:=fwpd.proccalloption;
  2707. end;
  2708. end;
  2709. { Check static }
  2710. if (po_staticmethod in fwpd.procoptions) then
  2711. begin
  2712. if not (po_staticmethod in currpd.procoptions) then
  2713. begin
  2714. include(currpd.procoptions, po_staticmethod);
  2715. if (po_classmethod in currpd.procoptions) then
  2716. begin
  2717. { remove self from the hidden paras }
  2718. symentry:=currpd.parast.Find('self');
  2719. if symentry<>nil then
  2720. begin
  2721. currpd.parast.Delete(symentry);
  2722. currpd.calcparas;
  2723. end;
  2724. end;
  2725. end;
  2726. end;
  2727. { Check if the procedure type and return type are correct,
  2728. also the parameters must match also with the type }
  2729. if ((m_repeat_forward in current_settings.modeswitches) or
  2730. not is_bareprocdef(currpd)) and
  2731. ((compare_paras(currpd.paras,fwpd.paras,cp_all,paracompopt)<>te_exact) or
  2732. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)<>te_exact)) then
  2733. begin
  2734. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2735. fwpd.fullprocname(false));
  2736. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2737. break;
  2738. end;
  2739. { Check if both are declared forward }
  2740. if fwpd.forwarddef and currpd.forwarddef then
  2741. begin
  2742. MessagePos1(currpd.fileinfo,parser_e_function_already_declared_public_forward,
  2743. currpd.fullprocname(false));
  2744. end;
  2745. { internconst or internproc only need to be defined once }
  2746. if (fwpd.proccalloption=pocall_internproc) then
  2747. currpd.proccalloption:=fwpd.proccalloption
  2748. else
  2749. if (currpd.proccalloption=pocall_internproc) then
  2750. fwpd.proccalloption:=currpd.proccalloption;
  2751. { Check procedure options, Delphi requires that class is
  2752. repeated in the implementation for class methods }
  2753. if (m_fpc in current_settings.modeswitches) then
  2754. po_comp:=[po_classmethod,po_varargs,po_methodpointer,po_interrupt]
  2755. else
  2756. po_comp:=[po_classmethod,po_methodpointer];
  2757. if ((po_comp * fwpd.procoptions)<>(po_comp * currpd.procoptions)) or
  2758. (fwpd.proctypeoption <> currpd.proctypeoption) then
  2759. begin
  2760. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2761. fwpd.fullprocname(false));
  2762. tprocsym(fwpd.procsym).write_parameter_lists(fwpd);
  2763. { This error is non-fatal, we can recover }
  2764. end;
  2765. { Forward declaration is external? }
  2766. if (po_external in fwpd.procoptions) then
  2767. MessagePos(currpd.fileinfo,parser_e_proc_already_external);
  2768. { Check parameters }
  2769. if (m_repeat_forward in current_settings.modeswitches) or
  2770. (currpd.minparacount>0) then
  2771. begin
  2772. { If mangled names are equal then they have the same amount of arguments }
  2773. { We can check the names of the arguments }
  2774. { both symtables are in the same order from left to right }
  2775. curridx:=0;
  2776. fwidx:=0;
  2777. currparacnt:=currpd.parast.SymList.Count;
  2778. fwparacnt:=fwpd.parast.SymList.Count;
  2779. repeat
  2780. { skip default parameter constsyms }
  2781. while (curridx<currparacnt) and
  2782. (tsym(currpd.parast.SymList[curridx]).typ<>paravarsym) do
  2783. inc(curridx);
  2784. while (fwidx<fwparacnt) and
  2785. (tsym(fwpd.parast.SymList[fwidx]).typ<>paravarsym) do
  2786. inc(fwidx);
  2787. { stop when one of the two lists is at the end }
  2788. if (fwidx>=fwparacnt) or (curridx>=currparacnt) then
  2789. break;
  2790. { compare names of parameters, ignore implictly
  2791. renamed parameters }
  2792. currparasym:=tsym(currpd.parast.SymList[curridx]);
  2793. fwparasym:=tsym(fwpd.parast.SymList[fwidx]);
  2794. if not(sp_implicitrename in currparasym.symoptions) and
  2795. not(sp_implicitrename in fwparasym.symoptions) then
  2796. begin
  2797. if (currparasym.name<>fwparasym.name) then
  2798. begin
  2799. MessagePos3(currpd.fileinfo,parser_e_header_different_var_names,
  2800. tprocsym(currpd.procsym).realname,fwparasym.realname,currparasym.realname);
  2801. break;
  2802. end;
  2803. end;
  2804. { next parameter }
  2805. inc(curridx);
  2806. inc(fwidx);
  2807. until false;
  2808. end;
  2809. { Everything is checked, now we can update the forward declaration
  2810. with the new data from the implementation }
  2811. fwpd.forwarddef:=currpd.forwarddef;
  2812. fwpd.hasforward:=true;
  2813. fwpd.procoptions:=fwpd.procoptions+currpd.procoptions;
  2814. { marked as local but exported from unit? }
  2815. if (po_kylixlocal in fwpd.procoptions) and (fwpd.owner.symtabletype=globalsymtable) then
  2816. MessagePos(fwpd.fileinfo,type_e_cant_export_local);
  2817. if fwpd.extnumber=$ffff then
  2818. fwpd.extnumber:=currpd.extnumber;
  2819. while not currpd.aliasnames.empty do
  2820. fwpd.aliasnames.insert(currpd.aliasnames.getfirst);
  2821. { update fileinfo so position references the implementation,
  2822. also update funcretsym if it is already generated }
  2823. fwpd.fileinfo:=currpd.fileinfo;
  2824. if assigned(fwpd.funcretsym) then
  2825. fwpd.funcretsym.fileinfo:=currpd.fileinfo;
  2826. if assigned(currpd.deprecatedmsg) then
  2827. begin
  2828. stringdispose(fwpd.deprecatedmsg);
  2829. fwpd.deprecatedmsg:=stringdup(currpd.deprecatedmsg^);
  2830. end;
  2831. { import names }
  2832. if assigned(currpd.import_dll) then
  2833. begin
  2834. stringdispose(fwpd.import_dll);
  2835. fwpd.import_dll:=stringdup(currpd.import_dll^);
  2836. end;
  2837. if assigned(currpd.import_name) then
  2838. begin
  2839. stringdispose(fwpd.import_name);
  2840. fwpd.import_name:=stringdup(currpd.import_name^);
  2841. end;
  2842. fwpd.import_nr:=currpd.import_nr;
  2843. { for compilerproc defines we need to rename and update the
  2844. symbolname to lowercase }
  2845. if (po_compilerproc in fwpd.procoptions) then
  2846. begin
  2847. { rename to lowercase so users can't access it }
  2848. fwpd.procsym.realname:='$'+lower(fwpd.procsym.name);
  2849. { the mangeled name is already changed by the pd_compilerproc }
  2850. { handler. It must be done immediately because if we have a }
  2851. { call to a compilerproc before it's implementation is }
  2852. { encountered, it must already use the new mangled name (JM) }
  2853. end;
  2854. { Release current procdef }
  2855. currpd.owner.deletedef(currpd);
  2856. currpd:=fwpd;
  2857. end
  2858. else
  2859. begin
  2860. { abstract methods aren't forward defined, but this }
  2861. { needs another error message }
  2862. if (po_abstractmethod in fwpd.procoptions) then
  2863. MessagePos(currpd.fileinfo,parser_e_abstract_no_definition)
  2864. else
  2865. begin
  2866. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  2867. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2868. end;
  2869. end;
  2870. { we found one proc with the same arguments, there are no others
  2871. so we can stop }
  2872. break;
  2873. end;
  2874. { check for allowing overload directive }
  2875. if not(m_fpc in current_settings.modeswitches) then
  2876. begin
  2877. { overload directive turns on overloading }
  2878. if ((po_overload in currpd.procoptions) or
  2879. (po_overload in fwpd.procoptions)) then
  2880. begin
  2881. { check if all procs have overloading, but not if the proc is a method or
  2882. already declared forward, then the check is already done }
  2883. if not(fwpd.hasforward or
  2884. assigned(currpd.struct) or
  2885. (currpd.forwarddef<>fwpd.forwarddef) or
  2886. ((po_overload in currpd.procoptions) and
  2887. (po_overload in fwpd.procoptions))) then
  2888. begin
  2889. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  2890. break;
  2891. end;
  2892. end
  2893. else
  2894. begin
  2895. if not(fwpd.forwarddef) then
  2896. begin
  2897. if (m_tp7 in current_settings.modeswitches) then
  2898. MessagePos(currpd.fileinfo,parser_e_procedure_overloading_is_off)
  2899. else
  2900. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  2901. break;
  2902. end;
  2903. end;
  2904. end; { equal arguments }
  2905. end;
  2906. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  2907. list }
  2908. if not forwardfound then
  2909. begin
  2910. { can happen in Delphi mode }
  2911. if (currpd.proctypeoption = potype_function) and
  2912. is_void(currpd.returndef) then
  2913. MessagePos1(currpd.fileinfo,parser_e_no_funcret_specified,currpd.procsym.realname);
  2914. tprocsym(currpd.procsym).ProcdefList.Add(currpd);
  2915. end;
  2916. proc_add_definition:=forwardfound;
  2917. end;
  2918. end.