pdecsub.pas 125 KB

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