pdecsub.pas 125 KB

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