pdecsub.pas 129 KB

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