pdecsub.pas 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124
  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) or
  1282. (is_javaclass(tprocdef(pd).struct) and
  1283. (po_classmethod in pd.procoptions)) then
  1284. include(pd.procoptions,po_finalmethod)
  1285. else
  1286. Message(parser_e_only_virtual_methods_final);
  1287. end;
  1288. procedure pd_enumerator(pd:tabstractprocdef);
  1289. begin
  1290. if pd.typ<>procdef then
  1291. internalerror(200910250);
  1292. if (token = _ID) then
  1293. begin
  1294. if pattern='MOVENEXT' then
  1295. begin
  1296. if oo_has_enumerator_movenext in tprocdef(pd).struct.objectoptions then
  1297. message(parser_e_only_one_enumerator_movenext);
  1298. pd.calcparas;
  1299. if (pd.proctypeoption = potype_function) and is_boolean(pd.returndef) and
  1300. (pd.minparacount = 0) then
  1301. begin
  1302. include(tprocdef(pd).struct.objectoptions, oo_has_enumerator_movenext);
  1303. include(pd.procoptions,po_enumerator_movenext);
  1304. end
  1305. else
  1306. Message(parser_e_enumerator_movenext_is_not_valid)
  1307. end
  1308. else
  1309. Message1(parser_e_invalid_enumerator_identifier, pattern);
  1310. consume(token);
  1311. end
  1312. else
  1313. Message(parser_e_enumerator_identifier_required);
  1314. end;
  1315. procedure pd_virtual(pd:tabstractprocdef);
  1316. {$ifdef WITHDMT}
  1317. var
  1318. pt : tnode;
  1319. {$endif WITHDMT}
  1320. begin
  1321. if (not assigned(pd.owner.defowner) or
  1322. not is_java_class_or_interface(tdef(pd.owner.defowner))) and
  1323. (po_external in pd.procoptions) then
  1324. Message1(parser_e_proc_dir_conflict,'EXTERNAL');
  1325. if pd.typ<>procdef then
  1326. internalerror(2003042610);
  1327. if (pd.proctypeoption=potype_constructor) and
  1328. is_object(tprocdef(pd).struct) then
  1329. Message(parser_e_constructor_cannot_be_not_virtual);
  1330. if is_objectpascal_helper(tprocdef(pd).struct) and
  1331. (m_objfpc in current_settings.modeswitches) then
  1332. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_VIRTUAL].str);
  1333. {$ifdef WITHDMT}
  1334. if is_object(tprocdef(pd).struct) and
  1335. (token<>_SEMICOLON) then
  1336. begin
  1337. { any type of parameter is allowed here! }
  1338. pt:=comp_expr(true);
  1339. if is_constintnode(pt) then
  1340. begin
  1341. include(pd.procoptions,po_msgint);
  1342. pd.messageinf.i:=pt.value;
  1343. end
  1344. else
  1345. Message(parser_e_ill_msg_expr);
  1346. disposetree(pt);
  1347. end;
  1348. {$endif WITHDMT}
  1349. end;
  1350. procedure pd_dispid(pd:tabstractprocdef);
  1351. var pt:Tnode;
  1352. begin
  1353. if pd.typ<>procdef then
  1354. internalerror(200604301);
  1355. pt:=comp_expr(true,false);
  1356. if is_constintnode(pt) then
  1357. if (Tordconstnode(pt).value<int64(low(longint))) or (Tordconstnode(pt).value>int64(high(longint))) then
  1358. message(parser_e_range_check_error)
  1359. else
  1360. Tprocdef(pd).dispid:=Tordconstnode(pt).value.svalue
  1361. else
  1362. message(parser_e_dispid_must_be_ord_const);
  1363. pt.free;
  1364. end;
  1365. procedure pd_static(pd:tabstractprocdef);
  1366. begin
  1367. if pd.typ=procdef then
  1368. include(tprocdef(pd).procsym.symoptions,sp_static);
  1369. include(pd.procoptions,po_staticmethod);
  1370. end;
  1371. procedure pd_override(pd:tabstractprocdef);
  1372. begin
  1373. if pd.typ<>procdef then
  1374. internalerror(2003042611);
  1375. if is_objectpascal_helper(tprocdef(pd).struct) then
  1376. begin
  1377. if m_objfpc in current_settings.modeswitches then
  1378. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_OVERRIDE].str)
  1379. end
  1380. else if not(is_class_or_interface_or_objc_or_java(tprocdef(pd).struct)) then
  1381. Message(parser_e_no_object_override)
  1382. else if is_objccategory(tprocdef(pd).struct) then
  1383. Message(parser_e_no_category_override)
  1384. else if (po_external in pd.procoptions) and
  1385. not is_objc_class_or_protocol(tprocdef(pd).struct) and
  1386. not is_cppclass(tprocdef(pd).struct) and
  1387. not is_java_class_or_interface(tprocdef(pd).struct) then
  1388. Message1(parser_e_proc_dir_conflict,'OVERRIDE');
  1389. end;
  1390. procedure pd_overload(pd:tabstractprocdef);
  1391. begin
  1392. if pd.typ<>procdef then
  1393. internalerror(2003042612);
  1394. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1395. end;
  1396. procedure pd_message(pd:tabstractprocdef);
  1397. var
  1398. pt : tnode;
  1399. paracnt : longint;
  1400. begin
  1401. if pd.typ<>procdef then
  1402. internalerror(2003042613);
  1403. if is_objectpascal_helper(tprocdef(pd).struct) then
  1404. begin
  1405. if m_objfpc in current_settings.modeswitches then
  1406. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_MESSAGE].str);
  1407. end
  1408. else
  1409. if not is_class(tprocdef(pd).struct) and
  1410. not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1411. Message(parser_e_msg_only_for_classes);
  1412. if ([po_msgstr,po_msgint]*pd.procoptions)<>[] then
  1413. Message(parser_e_multiple_messages);
  1414. { check parameter type }
  1415. if not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1416. begin
  1417. if po_external in pd.procoptions then
  1418. Message1(parser_e_proc_dir_conflict,'MESSAGE');
  1419. paracnt:=0;
  1420. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  1421. if paracnt<>1 then
  1422. Message(parser_e_ill_msg_param);
  1423. end;
  1424. pt:=comp_expr(true,false);
  1425. { message is 1-character long }
  1426. if is_constcharnode(pt) then
  1427. begin
  1428. include(pd.procoptions,po_msgstr);
  1429. tprocdef(pd).messageinf.str:=stringdup(chr(byte(tordconstnode(pt).value.uvalue and $FF)));
  1430. end
  1431. else if pt.nodetype=stringconstn then
  1432. begin
  1433. include(pd.procoptions,po_msgstr);
  1434. if (tstringconstnode(pt).len>255) then
  1435. Message(parser_e_message_string_too_long);
  1436. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).value_str);
  1437. end
  1438. else
  1439. if is_constintnode(pt) and
  1440. (is_class(tprocdef(pd).struct) or
  1441. is_objectpascal_helper(tprocdef(pd).struct)) then
  1442. begin
  1443. include(pd.procoptions,po_msgint);
  1444. if (Tordconstnode(pt).value<int64(low(Tprocdef(pd).messageinf.i))) or
  1445. (Tordconstnode(pt).value>int64(high(Tprocdef(pd).messageinf.i))) then
  1446. message(parser_e_range_check_error)
  1447. else
  1448. Tprocdef(pd).messageinf.i:=tordconstnode(pt).value.svalue;
  1449. end
  1450. else
  1451. Message(parser_e_ill_msg_expr);
  1452. { check whether the selector name is valid in case of Objective-C }
  1453. if (po_msgstr in pd.procoptions) and
  1454. is_objc_class_or_protocol(tprocdef(pd).struct) and
  1455. not objcvalidselectorname(@tprocdef(pd).messageinf.str^[1],length(tprocdef(pd).messageinf.str^)) then
  1456. Message1(type_e_invalid_objc_selector_name,tprocdef(pd).messageinf.str^);
  1457. pt.free;
  1458. end;
  1459. procedure pd_reintroduce(pd:tabstractprocdef);
  1460. begin
  1461. if pd.typ<>procdef then
  1462. internalerror(200401211);
  1463. if is_objectpascal_helper(tprocdef(pd).struct) then
  1464. begin
  1465. if m_objfpc in current_settings.modeswitches then
  1466. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_REINTRODUCE].str);
  1467. end
  1468. else
  1469. if not(is_class_or_interface_or_object(tprocdef(pd).struct)) and
  1470. not(is_objccategory(tprocdef(pd).struct)) and
  1471. not(is_javaclass(tprocdef(pd).struct)) then
  1472. Message(parser_e_no_object_reintroduce);
  1473. end;
  1474. procedure pd_syscall(pd:tabstractprocdef);
  1475. {$if defined(powerpc) or defined(m68k)}
  1476. var
  1477. vs : tparavarsym;
  1478. sym : tsym;
  1479. symtable : TSymtable;
  1480. v: Tconstexprint;
  1481. {$endif defined(powerpc) or defined(m68k)}
  1482. begin
  1483. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  1484. internalerror(2003042614);
  1485. tprocdef(pd).forwarddef:=false;
  1486. {$ifdef m68k}
  1487. if target_info.system in [system_m68k_amiga] then
  1488. begin
  1489. include(pd.procoptions,po_syscall_legacy);
  1490. if consume_sym(sym,symtable) then
  1491. begin
  1492. if (sym.typ=staticvarsym) and
  1493. (
  1494. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1495. is_32bitint(tabstractvarsym(sym).vardef)
  1496. ) then
  1497. begin
  1498. tprocdef(pd).libsym:=sym;
  1499. if po_syscall_legacy in tprocdef(pd).procoptions then
  1500. begin
  1501. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1502. paramanager.parseparaloc(vs,'A6');
  1503. pd.parast.insert(vs);
  1504. end
  1505. end
  1506. else
  1507. Message(parser_e_32bitint_or_pointer_variable_expected);
  1508. end;
  1509. (paramanager as tm68kparamanager).create_funcretloc_info(pd,calleeside);
  1510. (paramanager as tm68kparamanager).create_funcretloc_info(pd,callerside);
  1511. v:=get_intconst;
  1512. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1513. message(parser_e_range_check_error)
  1514. else
  1515. Tprocdef(pd).extnumber:=v.uvalue;
  1516. end;
  1517. {$endif m68k}
  1518. {$ifdef powerpc}
  1519. if target_info.system = system_powerpc_amiga then
  1520. begin
  1521. include(pd.procoptions,po_syscall_basesysv);
  1522. if consume_sym(sym,symtable) then
  1523. begin
  1524. if (sym.typ=staticvarsym) and
  1525. (
  1526. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1527. is_32bitint(tabstractvarsym(sym).vardef)
  1528. ) then
  1529. begin
  1530. tprocdef(pd).libsym:=sym;
  1531. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1532. pd.parast.insert(vs);
  1533. end
  1534. else
  1535. Message(parser_e_32bitint_or_pointer_variable_expected);
  1536. end;
  1537. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1538. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1539. v:=get_intconst;
  1540. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1541. message(parser_e_range_check_error)
  1542. else
  1543. Tprocdef(pd).extnumber:=v.uvalue;
  1544. end else
  1545. if target_info.system = system_powerpc_morphos then
  1546. begin
  1547. if idtoken=_LEGACY then
  1548. begin
  1549. consume(_LEGACY);
  1550. include(pd.procoptions,po_syscall_legacy);
  1551. end
  1552. else if idtoken=_SYSV then
  1553. begin
  1554. consume(_SYSV);
  1555. include(pd.procoptions,po_syscall_sysv);
  1556. end
  1557. else if idtoken=_BASESYSV then
  1558. begin
  1559. consume(_BASESYSV);
  1560. include(pd.procoptions,po_syscall_basesysv);
  1561. end
  1562. else if idtoken=_SYSVBASE then
  1563. begin
  1564. consume(_SYSVBASE);
  1565. include(pd.procoptions,po_syscall_sysvbase);
  1566. end
  1567. else if idtoken=_R12BASE then
  1568. begin
  1569. consume(_R12BASE);
  1570. include(pd.procoptions,po_syscall_r12base);
  1571. end
  1572. else
  1573. if syscall_convention='LEGACY' then
  1574. include(pd.procoptions,po_syscall_legacy)
  1575. else if syscall_convention='SYSV' then
  1576. include(pd.procoptions,po_syscall_sysv)
  1577. else if syscall_convention='BASESYSV' then
  1578. include(pd.procoptions,po_syscall_basesysv)
  1579. else if syscall_convention='SYSVBASE' then
  1580. include(pd.procoptions,po_syscall_sysvbase)
  1581. else if syscall_convention='R12BASE' then
  1582. include(pd.procoptions,po_syscall_r12base)
  1583. else
  1584. internalerror(2005010404);
  1585. if consume_sym(sym,symtable) then
  1586. begin
  1587. if (sym.typ=staticvarsym) and
  1588. (
  1589. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1590. is_32bitint(tabstractvarsym(sym).vardef)
  1591. ) then
  1592. begin
  1593. tprocdef(pd).libsym:=sym;
  1594. if po_syscall_legacy in tprocdef(pd).procoptions then
  1595. begin
  1596. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1597. paramanager.parseparaloc(vs,'A6');
  1598. pd.parast.insert(vs);
  1599. end
  1600. else if po_syscall_sysv in tprocdef(pd).procoptions then
  1601. begin
  1602. { Nothing to be done for sysv here for now, but this might change }
  1603. end
  1604. else if po_syscall_basesysv in tprocdef(pd).procoptions then
  1605. begin
  1606. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1607. pd.parast.insert(vs);
  1608. end
  1609. else if po_syscall_sysvbase in tprocdef(pd).procoptions then
  1610. begin
  1611. vs:=tparavarsym.create('$syscalllib',paranr_syscall_sysvbase,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1612. pd.parast.insert(vs);
  1613. end
  1614. else if po_syscall_r12base in tprocdef(pd).procoptions then
  1615. begin
  1616. vs:=tparavarsym.create('$syscalllib',paranr_syscall_r12base,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1617. paramanager.parseparaloc(vs,'R12');
  1618. pd.parast.insert(vs);
  1619. end
  1620. else
  1621. internalerror(2005010501);
  1622. end
  1623. else
  1624. Message(parser_e_32bitint_or_pointer_variable_expected);
  1625. end;
  1626. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1627. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1628. v:=get_intconst;
  1629. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1630. message(parser_e_range_check_error)
  1631. else
  1632. Tprocdef(pd).extnumber:=v.uvalue;
  1633. end;
  1634. {$endif powerpc}
  1635. end;
  1636. procedure pd_external(pd:tabstractprocdef);
  1637. {
  1638. If import_dll=nil the procedure is assumed to be in another
  1639. object file. In that object file it should have the name to
  1640. which import_name is pointing to. Otherwise, the procedure is
  1641. assumed to be in the DLL to which import_dll is pointing to. In
  1642. that case either import_nr<>0 or import_name<>nil is true, so
  1643. the procedure is either imported by number or by name. (DM)
  1644. }
  1645. var
  1646. hs : string;
  1647. v:Tconstexprint;
  1648. is_java_external: boolean;
  1649. begin
  1650. if pd.typ<>procdef then
  1651. internalerror(2003042615);
  1652. { Allow specifying a separate external name for methods in external Java
  1653. because its identifier naming constraints are laxer than FPC's
  1654. (e.g., case sensitive).
  1655. Limitation: only allows specifying the symbol name and not the package name,
  1656. and only for external classes/interfaces }
  1657. is_java_external:=
  1658. (pd.typ=procdef) and
  1659. is_java_class_or_interface(tdef(pd.owner.defowner)) and
  1660. (oo_is_external in tobjectdef(pd.owner.defowner).objectoptions);
  1661. with tprocdef(pd) do
  1662. begin
  1663. forwarddef:=false;
  1664. { forbid local external procedures }
  1665. if parast.symtablelevel>normal_function_level then
  1666. Message(parser_e_no_local_proc_external);
  1667. { If the procedure should be imported from a DLL, a constant string follows.
  1668. This isn't really correct, an contant string expression follows
  1669. so we check if an semicolon follows, else a string constant have to
  1670. follow (FK) }
  1671. if not is_java_external and
  1672. not(token=_SEMICOLON) and not(idtoken=_NAME) then
  1673. begin
  1674. { Always add library prefix and suffix to create an uniform name }
  1675. hs:=get_stringconst;
  1676. if ExtractFileExt(hs)='' then
  1677. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  1678. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  1679. hs:=target_info.sharedlibprefix+hs;
  1680. { the JVM expects java/lang/Object rather than java.lang.Object }
  1681. if target_info.system=system_jvm_java32 then
  1682. Replace(hs,'.','/');
  1683. import_dll:=stringdup(hs);
  1684. include(procoptions,po_has_importdll);
  1685. if (idtoken=_NAME) then
  1686. begin
  1687. consume(_NAME);
  1688. import_name:=stringdup(get_stringconst);
  1689. include(procoptions,po_has_importname);
  1690. if import_name^='' then
  1691. message(parser_e_empty_import_name);
  1692. end;
  1693. if (idtoken=_INDEX) then
  1694. begin
  1695. {After the word index follows the index number in the DLL.}
  1696. consume(_INDEX);
  1697. v:=get_intconst;
  1698. if (v<int64(low(import_nr))) or (v>int64(high(import_nr))) then
  1699. message(parser_e_range_check_error)
  1700. else
  1701. import_nr:=longint(v.svalue);
  1702. end;
  1703. { default is to used the realname of the procedure }
  1704. if (import_nr=0) and not assigned(import_name) then
  1705. begin
  1706. import_name:=stringdup(procsym.realname);
  1707. include(procoptions,po_has_importname);
  1708. end;
  1709. end
  1710. else
  1711. begin
  1712. if (idtoken=_NAME) or
  1713. is_java_external then
  1714. begin
  1715. consume(_NAME);
  1716. import_name:=stringdup(get_stringconst);
  1717. include(procoptions,po_has_importname);
  1718. if import_name^='' then
  1719. message(parser_e_empty_import_name);
  1720. end;
  1721. end;
  1722. end;
  1723. end;
  1724. procedure pd_weakexternal(pd:tabstractprocdef);
  1725. begin
  1726. if not(target_info.system in systems_weak_linking) then
  1727. message(parser_e_weak_external_not_supported)
  1728. else
  1729. pd_external(pd);
  1730. end;
  1731. type
  1732. pd_handler=procedure(pd:tabstractprocdef);
  1733. proc_dir_rec=record
  1734. idtok : ttoken;
  1735. pd_flags : tpdflags;
  1736. handler : pd_handler;
  1737. pocall : tproccalloption;
  1738. pooption : tprocoptions;
  1739. mutexclpocall : tproccalloptions;
  1740. mutexclpotype : tproctypeoptions;
  1741. mutexclpo : tprocoptions;
  1742. end;
  1743. const
  1744. {Should contain the number of procedure directives we support.}
  1745. num_proc_directives=42;
  1746. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  1747. (
  1748. (
  1749. idtok:_ABSTRACT;
  1750. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  1751. handler : @pd_abstract;
  1752. pocall : pocall_none;
  1753. pooption : [po_abstractmethod];
  1754. mutexclpocall : [pocall_internproc];
  1755. mutexclpotype : [];
  1756. mutexclpo : [po_exports,po_interrupt,po_inline]
  1757. ),(
  1758. idtok:_ALIAS;
  1759. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1760. handler : @pd_alias;
  1761. pocall : pocall_none;
  1762. pooption : [];
  1763. mutexclpocall : [];
  1764. mutexclpotype : [];
  1765. mutexclpo : [po_external,po_inline]
  1766. ),(
  1767. idtok:_ASMNAME;
  1768. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  1769. handler : @pd_asmname;
  1770. pocall : pocall_cdecl;
  1771. pooption : [po_external];
  1772. mutexclpocall : [pocall_internproc];
  1773. mutexclpotype : [];
  1774. mutexclpo : [po_external,po_inline]
  1775. ),(
  1776. idtok:_ASSEMBLER;
  1777. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1778. handler : nil;
  1779. pocall : pocall_none;
  1780. pooption : [po_assembler];
  1781. mutexclpocall : [];
  1782. mutexclpotype : [];
  1783. mutexclpo : [po_external]
  1784. ),(
  1785. idtok:_C; {same as cdecl for mode mac}
  1786. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1787. handler : nil;
  1788. pocall : pocall_cdecl;
  1789. pooption : [];
  1790. mutexclpocall : [];
  1791. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1792. mutexclpo : [po_assembler,po_external]
  1793. ),(
  1794. idtok:_CDECL;
  1795. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1796. handler : nil;
  1797. pocall : pocall_cdecl;
  1798. pooption : [];
  1799. mutexclpocall : [];
  1800. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1801. mutexclpo : [po_assembler,po_external]
  1802. ),(
  1803. idtok:_DISPID;
  1804. pd_flags : [pd_dispinterface];
  1805. handler : @pd_dispid;
  1806. pocall : pocall_none;
  1807. pooption : [po_dispid];
  1808. mutexclpocall : [pocall_internproc];
  1809. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1810. mutexclpo : [po_interrupt,po_external,po_inline]
  1811. ),(
  1812. idtok:_DYNAMIC;
  1813. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord];
  1814. handler : @pd_virtual;
  1815. pocall : pocall_none;
  1816. pooption : [po_virtualmethod];
  1817. mutexclpocall : [pocall_internproc];
  1818. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  1819. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  1820. ),(
  1821. idtok:_EXPORT;
  1822. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf,pd_notrecord,pd_nothelper];
  1823. handler : @pd_export;
  1824. pocall : pocall_none;
  1825. pooption : [po_exports,po_global];
  1826. mutexclpocall : [pocall_internproc];
  1827. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1828. mutexclpo : [po_external,po_interrupt,po_inline]
  1829. ),(
  1830. idtok:_EXTERNAL;
  1831. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper,pd_javaclass,pd_intfjava];
  1832. handler : @pd_external;
  1833. pocall : pocall_none;
  1834. pooption : [po_external];
  1835. mutexclpocall : [pocall_internproc,pocall_syscall];
  1836. { allowed for external cpp classes }
  1837. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  1838. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  1839. ),(
  1840. idtok:_FAR;
  1841. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  1842. handler : @pd_far;
  1843. pocall : pocall_none;
  1844. pooption : [];
  1845. mutexclpocall : [pocall_internproc];
  1846. mutexclpotype : [];
  1847. mutexclpo : [po_inline]
  1848. ),(
  1849. idtok:_FAR16;
  1850. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject,pd_notrecord,pd_nothelper];
  1851. handler : nil;
  1852. pocall : pocall_far16;
  1853. pooption : [];
  1854. mutexclpocall : [];
  1855. mutexclpotype : [];
  1856. mutexclpo : [po_external]
  1857. ),(
  1858. idtok:_FINAL;
  1859. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  1860. handler : @pd_final;
  1861. pocall : pocall_none;
  1862. pooption : [po_finalmethod];
  1863. mutexclpocall : [pocall_internproc];
  1864. mutexclpotype : [];
  1865. mutexclpo : [po_exports,po_interrupt,po_inline]
  1866. ),(
  1867. idtok:_FORWARD;
  1868. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  1869. handler : @pd_forward;
  1870. pocall : pocall_none;
  1871. pooption : [];
  1872. mutexclpocall : [pocall_internproc];
  1873. mutexclpotype : [];
  1874. mutexclpo : [po_external,po_inline]
  1875. ),(
  1876. idtok:_OLDFPCCALL;
  1877. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1878. handler : nil;
  1879. pocall : pocall_oldfpccall;
  1880. pooption : [];
  1881. mutexclpocall : [];
  1882. mutexclpotype : [];
  1883. mutexclpo : []
  1884. ),(
  1885. idtok:_INLINE;
  1886. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1887. handler : nil;
  1888. pocall : pocall_none;
  1889. pooption : [po_inline];
  1890. mutexclpocall : [];
  1891. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1892. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod]
  1893. ),(
  1894. idtok:_INTERNCONST;
  1895. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  1896. handler : @pd_internconst;
  1897. pocall : pocall_none;
  1898. pooption : [po_internconst];
  1899. mutexclpocall : [];
  1900. mutexclpotype : [potype_operator];
  1901. mutexclpo : []
  1902. ),(
  1903. idtok:_INTERNPROC;
  1904. pd_flags : [pd_interface,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  1905. handler : @pd_internproc;
  1906. pocall : pocall_internproc;
  1907. pooption : [];
  1908. mutexclpocall : [];
  1909. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1910. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  1911. ),(
  1912. idtok:_INTERRUPT;
  1913. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  1914. handler : @pd_interrupt;
  1915. pocall : pocall_oldfpccall;
  1916. pooption : [po_interrupt];
  1917. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,
  1918. pocall_pascal,pocall_far16,pocall_oldfpccall];
  1919. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1920. mutexclpo : [po_external,po_inline]
  1921. ),(
  1922. idtok:_IOCHECK;
  1923. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1924. handler : nil;
  1925. pocall : pocall_none;
  1926. pooption : [po_iocheck];
  1927. mutexclpocall : [pocall_internproc];
  1928. mutexclpotype : [];
  1929. mutexclpo : [po_external]
  1930. ),(
  1931. idtok:_LOCAL;
  1932. pd_flags : [pd_implemen,pd_body];
  1933. handler : nil;
  1934. pocall : pocall_none;
  1935. pooption : [po_kylixlocal];
  1936. mutexclpocall : [pocall_internproc,pocall_far16];
  1937. mutexclpotype : [];
  1938. mutexclpo : [po_external,po_exports]
  1939. ),(
  1940. idtok:_MESSAGE;
  1941. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_objcprot,pd_notrecord];
  1942. handler : @pd_message;
  1943. pocall : pocall_none;
  1944. pooption : []; { can be po_msgstr or po_msgint }
  1945. mutexclpocall : [pocall_internproc];
  1946. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1947. mutexclpo : [po_interrupt,po_inline]
  1948. ),(
  1949. idtok:_MWPASCAL;
  1950. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1951. handler : nil;
  1952. pocall : pocall_mwpascal;
  1953. pooption : [];
  1954. mutexclpocall : [];
  1955. mutexclpotype : [];
  1956. mutexclpo : []
  1957. ),(
  1958. idtok:_NEAR;
  1959. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf,pd_notrecord,pd_nothelper];
  1960. handler : @pd_near;
  1961. pocall : pocall_none;
  1962. pooption : [];
  1963. mutexclpocall : [pocall_internproc];
  1964. mutexclpotype : [];
  1965. mutexclpo : []
  1966. ),(
  1967. idtok:_NOSTACKFRAME;
  1968. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1969. handler : nil;
  1970. pocall : pocall_none;
  1971. pooption : [po_nostackframe];
  1972. mutexclpocall : [pocall_internproc];
  1973. mutexclpotype : [];
  1974. mutexclpo : []
  1975. ),(
  1976. idtok:_OVERLOAD;
  1977. pd_flags : [pd_implemen,pd_interface,pd_body,pd_javaclass,pd_intfjava];
  1978. handler : @pd_overload;
  1979. pocall : pocall_none;
  1980. pooption : [po_overload];
  1981. mutexclpocall : [pocall_internproc];
  1982. mutexclpotype : [];
  1983. mutexclpo : []
  1984. ),(
  1985. idtok:_OVERRIDE;
  1986. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_javaclass,pd_intfjava,pd_notrecord];
  1987. handler : @pd_override;
  1988. pocall : pocall_none;
  1989. pooption : [po_overridingmethod,po_virtualmethod];
  1990. mutexclpocall : [pocall_internproc];
  1991. mutexclpotype : [];
  1992. mutexclpo : [po_exports,po_interrupt,po_virtualmethod,po_inline]
  1993. ),(
  1994. idtok:_PASCAL;
  1995. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1996. handler : nil;
  1997. pocall : pocall_pascal;
  1998. pooption : [];
  1999. mutexclpocall : [];
  2000. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2001. mutexclpo : [po_external]
  2002. ),(
  2003. idtok:_PUBLIC;
  2004. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2005. handler : @pd_public;
  2006. pocall : pocall_none;
  2007. pooption : [po_public,po_global];
  2008. mutexclpocall : [pocall_internproc];
  2009. mutexclpotype : [];
  2010. mutexclpo : [po_external,po_inline]
  2011. ),(
  2012. idtok:_REGISTER;
  2013. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2014. handler : nil;
  2015. pocall : pocall_register;
  2016. pooption : [];
  2017. mutexclpocall : [];
  2018. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2019. mutexclpo : [po_external]
  2020. ),(
  2021. idtok:_REINTRODUCE;
  2022. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_notrecord,pd_javaclass];
  2023. handler : @pd_reintroduce;
  2024. pocall : pocall_none;
  2025. pooption : [po_reintroduce];
  2026. mutexclpocall : [pocall_internproc];
  2027. mutexclpotype : [];
  2028. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  2029. ),(
  2030. idtok:_SAFECALL;
  2031. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2032. handler : nil;
  2033. pocall : pocall_safecall;
  2034. pooption : [];
  2035. mutexclpocall : [];
  2036. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2037. mutexclpo : [po_external]
  2038. ),(
  2039. idtok:_SOFTFLOAT;
  2040. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2041. handler : nil;
  2042. pocall : pocall_softfloat;
  2043. pooption : [];
  2044. mutexclpocall : [];
  2045. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2046. { it's available with po_external because the libgcc floating point routines on the arm
  2047. uses this calling convention }
  2048. mutexclpo : []
  2049. ),(
  2050. idtok:_STATIC;
  2051. pd_flags : [pd_interface,pd_implemen,pd_body,pd_object,pd_record,pd_javaclass,pd_notobjintf];
  2052. handler : @pd_static;
  2053. pocall : pocall_none;
  2054. pooption : [po_staticmethod];
  2055. mutexclpocall : [pocall_internproc];
  2056. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2057. mutexclpo : [po_interrupt,po_exports]
  2058. ),(
  2059. idtok:_STDCALL;
  2060. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2061. handler : nil;
  2062. pocall : pocall_stdcall;
  2063. pooption : [];
  2064. mutexclpocall : [];
  2065. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2066. mutexclpo : [po_external]
  2067. ),(
  2068. idtok:_SYSCALL;
  2069. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  2070. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  2071. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  2072. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  2073. handler : @pd_syscall;
  2074. pocall : pocall_syscall;
  2075. pooption : [];
  2076. mutexclpocall : [];
  2077. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2078. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  2079. ),(
  2080. idtok:_VIRTUAL;
  2081. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2082. handler : @pd_virtual;
  2083. pocall : pocall_none;
  2084. pooption : [po_virtualmethod];
  2085. mutexclpocall : [pocall_internproc];
  2086. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2087. mutexclpo : [po_interrupt,po_exports,po_overridingmethod,po_inline]
  2088. ),(
  2089. idtok:_CPPDECL;
  2090. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2091. handler : nil;
  2092. pocall : pocall_cppdecl;
  2093. pooption : [];
  2094. mutexclpocall : [];
  2095. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2096. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  2097. ),(
  2098. idtok:_VARARGS;
  2099. pd_flags : [pd_interface,pd_implemen,pd_procvar,pd_objcclass,pd_objcprot];
  2100. handler : nil;
  2101. pocall : pocall_none;
  2102. pooption : [po_varargs];
  2103. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  2104. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  2105. mutexclpotype : [];
  2106. mutexclpo : [po_assembler,po_interrupt,po_inline]
  2107. ),(
  2108. idtok:_COMPILERPROC;
  2109. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2110. handler : nil;
  2111. pocall : pocall_none;
  2112. pooption : [po_compilerproc];
  2113. mutexclpocall : [];
  2114. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2115. mutexclpo : [po_interrupt]
  2116. ),(
  2117. idtok:_WEAKEXTERNAL;
  2118. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper];
  2119. handler : @pd_weakexternal;
  2120. pocall : pocall_none;
  2121. { mark it both external and weak external, so we don't have to
  2122. adapt all code for external symbols to also check for weak external
  2123. }
  2124. pooption : [po_external,po_weakexternal];
  2125. mutexclpocall : [pocall_internproc,pocall_syscall];
  2126. { allowed for external cpp classes }
  2127. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2128. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2129. ),(
  2130. idtok:_ENUMERATOR;
  2131. pd_flags : [pd_interface,pd_object,pd_record];
  2132. handler : @pd_enumerator;
  2133. pocall : pocall_none;
  2134. pooption : [];
  2135. mutexclpocall : [pocall_internproc];
  2136. mutexclpotype : [];
  2137. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  2138. )
  2139. );
  2140. function check_proc_directive(isprocvar:boolean):boolean;
  2141. var
  2142. i : longint;
  2143. begin
  2144. result:=false;
  2145. for i:=1 to num_proc_directives do
  2146. if proc_direcdata[i].idtok=idtoken then
  2147. begin
  2148. if ((not isprocvar) or
  2149. (pd_procvar in proc_direcdata[i].pd_flags)) and
  2150. { don't eat a public directive in classes }
  2151. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  2152. result:=true;
  2153. exit;
  2154. end;
  2155. end;
  2156. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  2157. {
  2158. Parse the procedure directive, returns true if a correct directive is found
  2159. }
  2160. var
  2161. p : longint;
  2162. found : boolean;
  2163. name : TIDString;
  2164. begin
  2165. parse_proc_direc:=false;
  2166. name:=tokeninfo^[idtoken].str;
  2167. found:=false;
  2168. { Hint directive? Then exit immediatly }
  2169. if (m_hintdirective in current_settings.modeswitches) then
  2170. begin
  2171. case idtoken of
  2172. _LIBRARY,
  2173. _PLATFORM,
  2174. _UNIMPLEMENTED,
  2175. _EXPERIMENTAL,
  2176. _DEPRECATED :
  2177. exit;
  2178. end;
  2179. end;
  2180. { C directive is MacPas only, because it breaks too much existing code
  2181. on other platforms (PFV) }
  2182. if (idtoken=_C) and
  2183. not(m_mac in current_settings.modeswitches) then
  2184. exit;
  2185. { retrieve data for directive if found }
  2186. for p:=1 to num_proc_directives do
  2187. if proc_direcdata[p].idtok=idtoken then
  2188. begin
  2189. found:=true;
  2190. break;
  2191. end;
  2192. { Check if the procedure directive is known }
  2193. if not found then
  2194. begin
  2195. { parsing a procvar type the name can be any
  2196. next variable !! }
  2197. if ((pdflags * [pd_procvar,pd_object,pd_record,pd_objcclass,pd_objcprot])=[]) and
  2198. not(idtoken=_PROPERTY) then
  2199. Message1(parser_w_unknown_proc_directive_ignored,name);
  2200. exit;
  2201. end;
  2202. { check if method and directive not for object, like public.
  2203. This needs to be checked also for procvars }
  2204. if (pd_notobject in proc_direcdata[p].pd_flags) and
  2205. (symtablestack.top.symtabletype=ObjectSymtable) and
  2206. { directive allowed for cpp classes? }
  2207. not((pd_cppobject in proc_direcdata[p].pd_flags) and is_cppclass(tdef(symtablestack.top.defowner))) and
  2208. not((pd_javaclass in proc_direcdata[p].pd_flags) and is_javaclass(tdef(symtablestack.top.defowner))) and
  2209. not((pd_intfjava in proc_direcdata[p].pd_flags) and is_javainterface(tdef(symtablestack.top.defowner))) then
  2210. exit;
  2211. if (pd_notrecord in proc_direcdata[p].pd_flags) and
  2212. (symtablestack.top.symtabletype=recordsymtable) then
  2213. exit;
  2214. { check if method and directive not for java class }
  2215. if not(pd_javaclass in proc_direcdata[p].pd_flags) and
  2216. is_javaclass(tdef(symtablestack.top.defowner)) then
  2217. exit;
  2218. { check if method and directive not for java interface }
  2219. if not(pd_intfjava in proc_direcdata[p].pd_flags) and
  2220. is_javainterface(tdef(symtablestack.top.defowner)) then
  2221. exit;
  2222. { Conflicts between directives ? }
  2223. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  2224. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  2225. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  2226. begin
  2227. Message1(parser_e_proc_dir_conflict,name);
  2228. exit;
  2229. end;
  2230. { set calling convention }
  2231. if proc_direcdata[p].pocall<>pocall_none then
  2232. begin
  2233. if (po_hascallingconvention in pd.procoptions) then
  2234. begin
  2235. Message2(parser_w_proc_overriding_calling,
  2236. proccalloptionStr[pd.proccalloption],
  2237. proccalloptionStr[proc_direcdata[p].pocall]);
  2238. end;
  2239. { check if the target processor supports this calling convention }
  2240. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  2241. begin
  2242. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  2243. { recover }
  2244. proc_direcdata[p].pocall:=pocall_stdcall;
  2245. end;
  2246. pd.proccalloption:=proc_direcdata[p].pocall;
  2247. include(pd.procoptions,po_hascallingconvention);
  2248. end;
  2249. if pd.typ=procdef then
  2250. begin
  2251. { Check if the directive is only for objects }
  2252. if (pd_object in proc_direcdata[p].pd_flags) and
  2253. not assigned(tprocdef(pd).struct) then
  2254. exit;
  2255. { Check if the directive is only for records }
  2256. if (pd_record in proc_direcdata[p].pd_flags) and
  2257. not assigned(tprocdef(pd).struct) then
  2258. exit;
  2259. { check if method and directive not for interface }
  2260. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  2261. is_interface(tprocdef(pd).struct) then
  2262. exit;
  2263. { check if method and directive not for interface }
  2264. if is_dispinterface(tprocdef(pd).struct) and
  2265. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  2266. exit;
  2267. { check if method and directive not for objcclass }
  2268. if is_objcclass(tprocdef(pd).struct) and
  2269. not(pd_objcclass in proc_direcdata[p].pd_flags) then
  2270. exit;
  2271. { check if method and directive not for objcprotocol }
  2272. if is_objcprotocol(tprocdef(pd).struct) and
  2273. not(pd_objcprot in proc_direcdata[p].pd_flags) then
  2274. exit;
  2275. { check if method and directive not for record/class helper }
  2276. if is_objectpascal_helper(tprocdef(pd).struct) and
  2277. (pd_nothelper in proc_direcdata[p].pd_flags) then
  2278. end;
  2279. { consume directive, and turn flag on }
  2280. consume(token);
  2281. parse_proc_direc:=true;
  2282. { Check the pd_flags if the directive should be allowed }
  2283. if (pd_interface in pdflags) and
  2284. not(pd_interface in proc_direcdata[p].pd_flags) then
  2285. begin
  2286. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  2287. exit;
  2288. end;
  2289. if (pd_implemen in pdflags) and
  2290. not(pd_implemen in proc_direcdata[p].pd_flags) then
  2291. begin
  2292. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  2293. exit;
  2294. end;
  2295. if (pd_procvar in pdflags) and
  2296. not(pd_procvar in proc_direcdata[p].pd_flags) then
  2297. begin
  2298. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  2299. exit;
  2300. end;
  2301. { Return the new pd_flags }
  2302. if not(pd_body in proc_direcdata[p].pd_flags) then
  2303. exclude(pdflags,pd_body);
  2304. { Add the correct flag }
  2305. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  2306. { Call the handler }
  2307. if pointer(proc_direcdata[p].handler)<>nil then
  2308. proc_direcdata[p].handler(pd);
  2309. end;
  2310. function proc_get_importname(pd:tprocdef):string;
  2311. var
  2312. dllname, importname : string;
  2313. begin
  2314. result:='';
  2315. if not(po_external in pd.procoptions) then
  2316. internalerror(200412151);
  2317. { external name or number is specified }
  2318. if assigned(pd.import_name) or (pd.import_nr<>0) then
  2319. begin
  2320. if assigned(pd.import_dll) then
  2321. dllname:=pd.import_dll^
  2322. else
  2323. dllname:='';
  2324. if assigned(pd.import_name) then
  2325. importname:=pd.import_name^
  2326. else
  2327. importname:='';
  2328. proc_get_importname:=make_dllmangledname(dllname,
  2329. importname,pd.import_nr,pd.proccalloption);
  2330. end
  2331. else
  2332. begin
  2333. { Default names when importing variables }
  2334. case pd.proccalloption of
  2335. pocall_cdecl :
  2336. begin
  2337. if assigned(pd.struct) then
  2338. result:=target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname
  2339. else
  2340. result:=target_info.Cprefix+pd.procsym.realname;
  2341. end;
  2342. pocall_cppdecl :
  2343. begin
  2344. result:=target_info.Cprefix+pd.cplusplusmangledname;
  2345. end;
  2346. else
  2347. begin
  2348. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  2349. { but according to MacPas mode description
  2350. Cprefix should still be used PM }
  2351. if (m_mac in current_settings.modeswitches) then
  2352. result:=target_info.Cprefix+tprocdef(pd).procsym.realname;
  2353. end;
  2354. end;
  2355. end;
  2356. end;
  2357. procedure proc_set_mangledname(pd:tprocdef);
  2358. var
  2359. s : string;
  2360. begin
  2361. { When the mangledname is already set we aren't allowed to change
  2362. it because it can already be used somewhere (PFV) }
  2363. if not(po_has_mangledname in pd.procoptions) then
  2364. begin
  2365. if (po_external in pd.procoptions) then
  2366. begin
  2367. { External Procedures are only allowed to change the mangledname
  2368. in their first declaration }
  2369. if (pd.forwarddef or (not pd.hasforward)) then
  2370. begin
  2371. s:=proc_get_importname(pd);
  2372. if s<>'' then
  2373. begin
  2374. pd.setmangledname(s);
  2375. end;
  2376. end;
  2377. end
  2378. else
  2379. { Normal procedures }
  2380. begin
  2381. if (po_compilerproc in pd.procoptions) then
  2382. begin
  2383. pd.setmangledname(lower(pd.procsym.name));
  2384. end;
  2385. end;
  2386. end;
  2387. { Public/exported alias names }
  2388. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  2389. not(po_has_public_name in pd.procoptions) then
  2390. begin
  2391. case pd.proccalloption of
  2392. pocall_cdecl :
  2393. begin
  2394. if assigned(pd.struct) then
  2395. pd.aliasnames.insert(target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname)
  2396. else
  2397. begin
  2398. { Export names are not mangled on Windows and OS/2, see also pexports.pas }
  2399. if (target_info.system in (systems_all_windows+[system_i386_emx, system_i386_os2])) and
  2400. (po_exports in pd.procoptions) then
  2401. pd.aliasnames.insert(pd.procsym.realname)
  2402. else
  2403. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  2404. end;
  2405. end;
  2406. pocall_cppdecl :
  2407. begin
  2408. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  2409. end;
  2410. end;
  2411. { prevent adding the alias a second time }
  2412. include(pd.procoptions,po_has_public_name);
  2413. end;
  2414. end;
  2415. procedure handle_calling_convention(pd:tabstractprocdef);
  2416. begin
  2417. { set the default calling convention if none provided }
  2418. if (pd.typ=procdef) and
  2419. (is_objc_class_or_protocol(tprocdef(pd).struct) or
  2420. is_cppclass(tprocdef(pd).struct)) then
  2421. begin
  2422. { none of the explicit calling conventions should be allowed }
  2423. if (po_hascallingconvention in pd.procoptions) then
  2424. internalerror(2009032501);
  2425. if is_cppclass(tprocdef(pd).struct) then
  2426. pd.proccalloption:=pocall_cppdecl
  2427. else
  2428. pd.proccalloption:=pocall_cdecl;
  2429. end
  2430. else if not(po_hascallingconvention in pd.procoptions) then
  2431. pd.proccalloption:=current_settings.defproccall
  2432. else
  2433. begin
  2434. if pd.proccalloption=pocall_none then
  2435. internalerror(200309081);
  2436. end;
  2437. { handle proccall specific settings }
  2438. case pd.proccalloption of
  2439. pocall_cdecl,
  2440. pocall_cppdecl :
  2441. begin
  2442. { check C cdecl para types }
  2443. check_c_para(pd);
  2444. end;
  2445. pocall_far16 :
  2446. begin
  2447. { Temporary stub, must be rewritten to support OS/2 far16 }
  2448. Message1(parser_w_proc_directive_ignored,'FAR16');
  2449. end;
  2450. end;
  2451. { Inlining is enabled and supported? }
  2452. if (po_inline in pd.procoptions) and
  2453. not(cs_do_inline in current_settings.localswitches) then
  2454. begin
  2455. { Give an error if inline is not supported by the compiler mode,
  2456. otherwise only give a warning that this procedure will not be inlined }
  2457. if not(m_default_inline in current_settings.modeswitches) then
  2458. Message(parser_e_proc_inline_not_supported)
  2459. else
  2460. Message(parser_w_inlining_disabled);
  2461. exclude(pd.procoptions,po_inline);
  2462. end;
  2463. { For varargs directive also cdecl and external must be defined }
  2464. if (po_varargs in pd.procoptions) then
  2465. begin
  2466. { check first for external in the interface, if available there
  2467. then the cdecl must also be there since there is no implementation
  2468. available to contain it }
  2469. if parse_only then
  2470. begin
  2471. { if external is available, then cdecl must also be available,
  2472. procvars don't need external }
  2473. if not((po_external in pd.procoptions) or
  2474. (pd.typ=procvardef) or
  2475. { for objcclasses this is checked later, because the entire
  2476. class may be external. }
  2477. is_objc_class_or_protocol(tprocdef(pd).struct)) and
  2478. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal])) then
  2479. Message(parser_e_varargs_need_cdecl_and_external);
  2480. end
  2481. else
  2482. begin
  2483. { both must be defined now }
  2484. if not((po_external in pd.procoptions) or
  2485. (pd.typ=procvardef)) or
  2486. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal])) then
  2487. Message(parser_e_varargs_need_cdecl_and_external);
  2488. end;
  2489. end;
  2490. { insert hidden high parameters }
  2491. pd.parast.SymList.ForEachCall(@insert_hidden_para,pd);
  2492. { insert hidden self parameter }
  2493. insert_self_and_vmt_para(pd);
  2494. { insert funcret parameter if required }
  2495. insert_funcret_para(pd);
  2496. { Make var parameters regable, this must be done after the calling
  2497. convention is set. }
  2498. { this must be done before parentfp is insert, because getting all cases
  2499. where parentfp must be in a memory location isn't catched properly so
  2500. we put parentfp never in a register }
  2501. pd.parast.SymList.ForEachCall(@set_addr_param_regable,pd);
  2502. { insert parentfp parameter if required }
  2503. insert_parentfp_para(pd);
  2504. { Calculate parameter tlist }
  2505. pd.calcparas;
  2506. end;
  2507. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  2508. {
  2509. Parse the procedure directives. It does not matter if procedure directives
  2510. are written using ;procdir; or ['procdir'] syntax.
  2511. }
  2512. var
  2513. res : boolean;
  2514. begin
  2515. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  2516. begin
  2517. tprocdef(pd).aliasnames.insert(target_info.Cprefix+tprocdef(pd).procsym.realname);
  2518. include(pd.procoptions,po_public);
  2519. include(pd.procoptions,po_has_public_name);
  2520. include(pd.procoptions,po_global);
  2521. end;
  2522. { methods from external class definitions are all external themselves }
  2523. if (pd.typ=procdef) and
  2524. assigned(tprocdef(pd).struct) and
  2525. (tprocdef(pd).struct.typ=objectdef) and
  2526. (oo_is_external in tobjectdef(tprocdef(pd).struct).objectoptions) then
  2527. tprocdef(pd).make_external;
  2528. { Class constructors and destructor are static class methods in real. }
  2529. { There are many places in the compiler where either class or static }
  2530. { method flag changes the behavior. It is simplier to add them to }
  2531. { the class constructors/destructors options than to fix all the }
  2532. { occurencies. (Paul) }
  2533. if pd.proctypeoption in [potype_class_constructor,potype_class_destructor] then
  2534. begin
  2535. include(pd.procoptions,po_classmethod);
  2536. include(pd.procoptions,po_staticmethod);
  2537. end;
  2538. while token in [_ID,_LECKKLAMMER] do
  2539. begin
  2540. if try_to_consume(_LECKKLAMMER) then
  2541. begin
  2542. repeat
  2543. parse_proc_direc(pd,pdflags);
  2544. until not try_to_consume(_COMMA);
  2545. consume(_RECKKLAMMER);
  2546. { we always expect at least '[];' }
  2547. res:=true;
  2548. end
  2549. else
  2550. begin
  2551. res:=parse_proc_direc(pd,pdflags);
  2552. end;
  2553. { A procedure directive normally followed by a semicolon, but in
  2554. a const section or reading a type we should stop when _EQ is found,
  2555. because a constant/default value follows }
  2556. if res then
  2557. begin
  2558. if (block_type=bt_const_type) and
  2559. (token=_EQ) then
  2560. break;
  2561. { support procedure proc;stdcall export; }
  2562. if not(check_proc_directive((pd.typ=procvardef))) then
  2563. begin
  2564. { support "record p : procedure stdcall end;" and
  2565. "var p : procedure stdcall = nil;" }
  2566. if (pd_procvar in pdflags) and
  2567. (token in [_END,_RKLAMMER,_EQ]) then
  2568. break
  2569. else
  2570. begin
  2571. if (token=_COLON) then
  2572. begin
  2573. Message(parser_e_field_not_allowed_here);
  2574. consume_all_until(_SEMICOLON);
  2575. end;
  2576. consume(_SEMICOLON)
  2577. end;
  2578. end;
  2579. end
  2580. else
  2581. break;
  2582. end;
  2583. end;
  2584. procedure parse_var_proc_directives(sym:tsym);
  2585. var
  2586. pdflags : tpdflags;
  2587. pd : tabstractprocdef;
  2588. begin
  2589. pdflags:=[pd_procvar];
  2590. pd:=nil;
  2591. case sym.typ of
  2592. fieldvarsym,
  2593. staticvarsym,
  2594. localvarsym,
  2595. paravarsym :
  2596. pd:=tabstractprocdef(tabstractvarsym(sym).vardef);
  2597. typesym :
  2598. pd:=tabstractprocdef(ttypesym(sym).typedef);
  2599. else
  2600. internalerror(2003042617);
  2601. end;
  2602. if pd.typ<>procvardef then
  2603. internalerror(2003042618);
  2604. { names should never be used anyway }
  2605. parse_proc_directives(pd,pdflags);
  2606. end;
  2607. procedure parse_object_proc_directives(pd:tabstractprocdef);
  2608. var
  2609. pdflags : tpdflags;
  2610. begin
  2611. pdflags:=[pd_object];
  2612. parse_proc_directives(pd,pdflags);
  2613. end;
  2614. procedure parse_record_proc_directives(pd:tabstractprocdef);
  2615. var
  2616. pdflags : tpdflags;
  2617. begin
  2618. pdflags:=[pd_record];
  2619. parse_proc_directives(pd,pdflags);
  2620. end;
  2621. function proc_add_definition(var currpd:tprocdef):boolean;
  2622. {
  2623. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  2624. forwarddef is found and reused it returns true
  2625. }
  2626. var
  2627. fwpd : tprocdef;
  2628. currparasym,
  2629. fwparasym : tsym;
  2630. currparacnt,
  2631. fwparacnt,
  2632. curridx,
  2633. fwidx,
  2634. i : longint;
  2635. po_comp : tprocoptions;
  2636. paracompopt: tcompare_paras_options;
  2637. forwardfound : boolean;
  2638. symentry: TSymEntry;
  2639. begin
  2640. forwardfound:=false;
  2641. { check overloaded functions if the same function already exists }
  2642. for i:=0 to tprocsym(currpd.procsym).ProcdefList.Count-1 do
  2643. begin
  2644. fwpd:=tprocdef(tprocsym(currpd.procsym).ProcdefList[i]);
  2645. { can happen for internally generated routines }
  2646. if (fwpd=currpd) then
  2647. begin
  2648. result:=true;
  2649. exit;
  2650. end;
  2651. { Skip overloaded definitions that are declared in other units }
  2652. if fwpd.procsym<>currpd.procsym then
  2653. continue;
  2654. { check the parameters, for delphi/tp it is possible to
  2655. leave the parameters away in the implementation (forwarddef=false).
  2656. But for an overload declared function this is not allowed }
  2657. if { check if empty implementation arguments match is allowed }
  2658. (
  2659. not(m_repeat_forward in current_settings.modeswitches) and
  2660. not(currpd.forwarddef) and
  2661. is_bareprocdef(currpd) and
  2662. not(po_overload in fwpd.procoptions)
  2663. ) or
  2664. { check arguments, we need to check only the user visible parameters. The hidden parameters
  2665. can be in a different location because of the calling convention, eg. L-R vs. R-L order (PFV) }
  2666. (
  2667. (compare_paras(currpd.paras,fwpd.paras,cp_none,[cpo_comparedefaultvalue,cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv])=te_exact) and
  2668. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)=te_exact)
  2669. ) then
  2670. begin
  2671. { Check if we've found the forwarddef, if found then
  2672. we need to update the forward def with the current
  2673. implementation settings }
  2674. if fwpd.forwarddef then
  2675. begin
  2676. forwardfound:=true;
  2677. if not(m_repeat_forward in current_settings.modeswitches) and
  2678. (fwpd.proccalloption<>currpd.proccalloption) then
  2679. paracompopt:=[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv]
  2680. else
  2681. paracompopt:=[cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv];
  2682. { Check calling convention }
  2683. if (fwpd.proccalloption<>currpd.proccalloption) then
  2684. begin
  2685. { In delphi it is possible to specify the calling
  2686. convention in the interface or implementation if
  2687. there was no convention specified in the other
  2688. part }
  2689. if (m_delphi in current_settings.modeswitches) then
  2690. begin
  2691. if not(po_hascallingconvention in currpd.procoptions) then
  2692. currpd.proccalloption:=fwpd.proccalloption
  2693. else
  2694. if not(po_hascallingconvention in fwpd.procoptions) then
  2695. fwpd.proccalloption:=currpd.proccalloption
  2696. else
  2697. begin
  2698. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2699. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2700. { restore interface settings }
  2701. currpd.proccalloption:=fwpd.proccalloption;
  2702. end;
  2703. end
  2704. else
  2705. begin
  2706. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2707. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2708. { restore interface settings }
  2709. currpd.proccalloption:=fwpd.proccalloption;
  2710. end;
  2711. end;
  2712. { Check static }
  2713. if (po_staticmethod in fwpd.procoptions) then
  2714. begin
  2715. if not (po_staticmethod in currpd.procoptions) then
  2716. begin
  2717. include(currpd.procoptions, po_staticmethod);
  2718. if (po_classmethod in currpd.procoptions) then
  2719. begin
  2720. { remove self from the hidden paras }
  2721. symentry:=currpd.parast.Find('self');
  2722. if symentry<>nil then
  2723. begin
  2724. currpd.parast.Delete(symentry);
  2725. currpd.calcparas;
  2726. end;
  2727. end;
  2728. end;
  2729. end;
  2730. { Check if the procedure type and return type are correct,
  2731. also the parameters must match also with the type }
  2732. if ((m_repeat_forward in current_settings.modeswitches) or
  2733. not is_bareprocdef(currpd)) and
  2734. ((compare_paras(currpd.paras,fwpd.paras,cp_all,paracompopt)<>te_exact) or
  2735. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)<>te_exact)) then
  2736. begin
  2737. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2738. fwpd.fullprocname(false));
  2739. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2740. break;
  2741. end;
  2742. { Check if both are declared forward }
  2743. if fwpd.forwarddef and currpd.forwarddef then
  2744. begin
  2745. MessagePos1(currpd.fileinfo,parser_e_function_already_declared_public_forward,
  2746. currpd.fullprocname(false));
  2747. end;
  2748. { internconst or internproc only need to be defined once }
  2749. if (fwpd.proccalloption=pocall_internproc) then
  2750. currpd.proccalloption:=fwpd.proccalloption
  2751. else
  2752. if (currpd.proccalloption=pocall_internproc) then
  2753. fwpd.proccalloption:=currpd.proccalloption;
  2754. { Check procedure options, Delphi requires that class is
  2755. repeated in the implementation for class methods }
  2756. if (m_fpc in current_settings.modeswitches) then
  2757. po_comp:=[po_classmethod,po_varargs,po_methodpointer,po_interrupt]
  2758. else
  2759. po_comp:=[po_classmethod,po_methodpointer];
  2760. if ((po_comp * fwpd.procoptions)<>(po_comp * currpd.procoptions)) or
  2761. (fwpd.proctypeoption <> currpd.proctypeoption) then
  2762. begin
  2763. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2764. fwpd.fullprocname(false));
  2765. tprocsym(fwpd.procsym).write_parameter_lists(fwpd);
  2766. { This error is non-fatal, we can recover }
  2767. end;
  2768. { Forward declaration is external? }
  2769. if (po_external in fwpd.procoptions) then
  2770. MessagePos(currpd.fileinfo,parser_e_proc_already_external);
  2771. { Check parameters }
  2772. if (m_repeat_forward in current_settings.modeswitches) or
  2773. (currpd.minparacount>0) then
  2774. begin
  2775. { If mangled names are equal then they have the same amount of arguments }
  2776. { We can check the names of the arguments }
  2777. { both symtables are in the same order from left to right }
  2778. curridx:=0;
  2779. fwidx:=0;
  2780. currparacnt:=currpd.parast.SymList.Count;
  2781. fwparacnt:=fwpd.parast.SymList.Count;
  2782. repeat
  2783. { skip default parameter constsyms }
  2784. while (curridx<currparacnt) and
  2785. (tsym(currpd.parast.SymList[curridx]).typ<>paravarsym) do
  2786. inc(curridx);
  2787. while (fwidx<fwparacnt) and
  2788. (tsym(fwpd.parast.SymList[fwidx]).typ<>paravarsym) do
  2789. inc(fwidx);
  2790. { stop when one of the two lists is at the end }
  2791. if (fwidx>=fwparacnt) or (curridx>=currparacnt) then
  2792. break;
  2793. { compare names of parameters, ignore implictly
  2794. renamed parameters }
  2795. currparasym:=tsym(currpd.parast.SymList[curridx]);
  2796. fwparasym:=tsym(fwpd.parast.SymList[fwidx]);
  2797. if not(sp_implicitrename in currparasym.symoptions) and
  2798. not(sp_implicitrename in fwparasym.symoptions) then
  2799. begin
  2800. if (currparasym.name<>fwparasym.name) then
  2801. begin
  2802. MessagePos3(currpd.fileinfo,parser_e_header_different_var_names,
  2803. tprocsym(currpd.procsym).realname,fwparasym.realname,currparasym.realname);
  2804. break;
  2805. end;
  2806. end;
  2807. { next parameter }
  2808. inc(curridx);
  2809. inc(fwidx);
  2810. until false;
  2811. end;
  2812. { Everything is checked, now we can update the forward declaration
  2813. with the new data from the implementation }
  2814. fwpd.forwarddef:=currpd.forwarddef;
  2815. fwpd.hasforward:=true;
  2816. fwpd.procoptions:=fwpd.procoptions+currpd.procoptions;
  2817. { marked as local but exported from unit? }
  2818. if (po_kylixlocal in fwpd.procoptions) and (fwpd.owner.symtabletype=globalsymtable) then
  2819. MessagePos(fwpd.fileinfo,type_e_cant_export_local);
  2820. if fwpd.extnumber=$ffff then
  2821. fwpd.extnumber:=currpd.extnumber;
  2822. while not currpd.aliasnames.empty do
  2823. fwpd.aliasnames.insert(currpd.aliasnames.getfirst);
  2824. { update fileinfo so position references the implementation,
  2825. also update funcretsym if it is already generated }
  2826. fwpd.fileinfo:=currpd.fileinfo;
  2827. if assigned(fwpd.funcretsym) then
  2828. fwpd.funcretsym.fileinfo:=currpd.fileinfo;
  2829. if assigned(currpd.deprecatedmsg) then
  2830. begin
  2831. stringdispose(fwpd.deprecatedmsg);
  2832. fwpd.deprecatedmsg:=stringdup(currpd.deprecatedmsg^);
  2833. end;
  2834. { import names }
  2835. if assigned(currpd.import_dll) then
  2836. begin
  2837. stringdispose(fwpd.import_dll);
  2838. fwpd.import_dll:=stringdup(currpd.import_dll^);
  2839. end;
  2840. if assigned(currpd.import_name) then
  2841. begin
  2842. stringdispose(fwpd.import_name);
  2843. fwpd.import_name:=stringdup(currpd.import_name^);
  2844. end;
  2845. fwpd.import_nr:=currpd.import_nr;
  2846. { for compilerproc defines we need to rename and update the
  2847. symbolname to lowercase }
  2848. if (po_compilerproc in fwpd.procoptions) then
  2849. begin
  2850. { rename to lowercase so users can't access it }
  2851. fwpd.procsym.realname:='$'+lower(fwpd.procsym.name);
  2852. { the mangeled name is already changed by the pd_compilerproc }
  2853. { handler. It must be done immediately because if we have a }
  2854. { call to a compilerproc before it's implementation is }
  2855. { encountered, it must already use the new mangled name (JM) }
  2856. end;
  2857. { Release current procdef }
  2858. currpd.owner.deletedef(currpd);
  2859. currpd:=fwpd;
  2860. end
  2861. else
  2862. begin
  2863. { abstract methods aren't forward defined, but this }
  2864. { needs another error message }
  2865. if (po_abstractmethod in fwpd.procoptions) then
  2866. MessagePos(currpd.fileinfo,parser_e_abstract_no_definition)
  2867. else
  2868. begin
  2869. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  2870. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2871. end;
  2872. end;
  2873. { we found one proc with the same arguments, there are no others
  2874. so we can stop }
  2875. break;
  2876. end;
  2877. { check for allowing overload directive }
  2878. if not(m_fpc in current_settings.modeswitches) then
  2879. begin
  2880. { overload directive turns on overloading }
  2881. if ((po_overload in currpd.procoptions) or
  2882. (po_overload in fwpd.procoptions)) then
  2883. begin
  2884. { check if all procs have overloading, but not if the proc is a method or
  2885. already declared forward, then the check is already done }
  2886. if not(fwpd.hasforward or
  2887. assigned(currpd.struct) or
  2888. (currpd.forwarddef<>fwpd.forwarddef) or
  2889. ((po_overload in currpd.procoptions) and
  2890. (po_overload in fwpd.procoptions))) then
  2891. begin
  2892. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  2893. break;
  2894. end;
  2895. end
  2896. else
  2897. begin
  2898. if not(fwpd.forwarddef) then
  2899. begin
  2900. if (m_tp7 in current_settings.modeswitches) then
  2901. MessagePos(currpd.fileinfo,parser_e_procedure_overloading_is_off)
  2902. else
  2903. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  2904. break;
  2905. end;
  2906. end;
  2907. end; { equal arguments }
  2908. end;
  2909. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  2910. list }
  2911. if not forwardfound then
  2912. begin
  2913. { can happen in Delphi mode }
  2914. if (currpd.proctypeoption = potype_function) and
  2915. is_void(currpd.returndef) then
  2916. MessagePos1(currpd.fileinfo,parser_e_no_funcret_specified,currpd.procsym.realname);
  2917. tprocsym(currpd.procsym).ProcdefList.Add(currpd);
  2918. end;
  2919. proc_add_definition:=forwardfound;
  2920. end;
  2921. end.