Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1802)

Side by Side Diff: pkg/compiler/lib/src/native/behavior.dart

Issue 2595493002: Merge CoreTypes and CoreClasses into CommonElements. (Closed)
Patch Set: Updated cf. comment Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pkg/compiler/lib/src/mirrors_used.dart ('k') | pkg/compiler/lib/src/native/enqueue.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 import '../common.dart'; 5 import '../common.dart';
6 import '../common/backend_api.dart' show ForeignResolver; 6 import '../common/backend_api.dart' show ForeignResolver;
7 import '../common/resolution.dart' show ParsingContext, Resolution; 7 import '../common/resolution.dart' show ParsingContext, Resolution;
8 import '../compiler.dart' show Compiler; 8 import '../compiler.dart' show Compiler;
9 import '../constants/expressions.dart'; 9 import '../constants/expressions.dart';
10 import '../constants/values.dart'; 10 import '../constants/values.dart';
11 import '../core_types.dart' show CoreTypes; 11 import '../core_types.dart' show CommonElements;
12 import '../dart_types.dart'; 12 import '../dart_types.dart';
13 import '../elements/elements.dart'; 13 import '../elements/elements.dart';
14 import '../js/js.dart' as js; 14 import '../js/js.dart' as js;
15 import '../js_backend/js_backend.dart'; 15 import '../js_backend/js_backend.dart';
16 import '../tree/tree.dart'; 16 import '../tree/tree.dart';
17 import '../universe/side_effects.dart' show SideEffects; 17 import '../universe/side_effects.dart' show SideEffects;
18 import '../util/util.dart'; 18 import '../util/util.dart';
19 import 'enqueue.dart'; 19 import 'enqueue.dart';
20 import 'js.dart'; 20 import 'js.dart';
21 21
(...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 return sideEffects; 491 return sideEffects;
492 } 492 }
493 493
494 /// Returns a [TypeLookup] that uses [resolver] to perform lookup and [node] 494 /// Returns a [TypeLookup] that uses [resolver] to perform lookup and [node]
495 /// as position for errors. 495 /// as position for errors.
496 static TypeLookup _typeLookup(Node node, ForeignResolver resolver) { 496 static TypeLookup _typeLookup(Node node, ForeignResolver resolver) {
497 return (String name) => resolver.resolveTypeFromString(node, name); 497 return (String name) => resolver.resolveTypeFromString(node, name);
498 } 498 }
499 499
500 /// Compute the [NativeBehavior] for a [Send] node calling the 'JS' function. 500 /// Compute the [NativeBehavior] for a [Send] node calling the 'JS' function.
501 static NativeBehavior ofJsCallSend(Send jsCall, DiagnosticReporter reporter, 501 static NativeBehavior ofJsCallSend(
502 ParsingContext parsing, CoreTypes coreTypes, ForeignResolver resolver) { 502 Send jsCall,
503 DiagnosticReporter reporter,
504 ParsingContext parsing,
505 CommonElements commonElements,
506 ForeignResolver resolver) {
503 var argNodes = jsCall.arguments; 507 var argNodes = jsCall.arguments;
504 if (argNodes.isEmpty || argNodes.tail.isEmpty) { 508 if (argNodes.isEmpty || argNodes.tail.isEmpty) {
505 reporter.reportErrorMessage(jsCall, MessageKind.WRONG_ARGUMENT_FOR_JS); 509 reporter.reportErrorMessage(jsCall, MessageKind.WRONG_ARGUMENT_FOR_JS);
506 return new NativeBehavior(); 510 return new NativeBehavior();
507 } 511 }
508 512
509 var specArgument = argNodes.head; 513 var specArgument = argNodes.head;
510 if (specArgument is! StringNode || specArgument.isInterpolation) { 514 if (specArgument is! StringNode || specArgument.isInterpolation) {
511 reporter.reportErrorMessage( 515 reporter.reportErrorMessage(
512 specArgument, MessageKind.WRONG_ARGUMENT_FOR_JS_FIRST); 516 specArgument, MessageKind.WRONG_ARGUMENT_FOR_JS_FIRST);
513 return new NativeBehavior(); 517 return new NativeBehavior();
514 } 518 }
515 519
516 var codeArgument = argNodes.tail.head; 520 var codeArgument = argNodes.tail.head;
517 if (codeArgument is! StringNode || codeArgument.isInterpolation) { 521 if (codeArgument is! StringNode || codeArgument.isInterpolation) {
518 reporter.reportErrorMessage( 522 reporter.reportErrorMessage(
519 codeArgument, MessageKind.WRONG_ARGUMENT_FOR_JS_SECOND); 523 codeArgument, MessageKind.WRONG_ARGUMENT_FOR_JS_SECOND);
520 return new NativeBehavior(); 524 return new NativeBehavior();
521 } 525 }
522 526
523 String specString = specArgument.dartString.slowToString(); 527 String specString = specArgument.dartString.slowToString();
524 String codeString = codeArgument.dartString.slowToString(); 528 String codeString = codeArgument.dartString.slowToString();
525 529
526 return ofJsCall(specString, codeString, _typeLookup(specArgument, resolver), 530 return ofJsCall(specString, codeString, _typeLookup(specArgument, resolver),
527 specArgument, reporter, coreTypes); 531 specArgument, reporter, commonElements);
528 } 532 }
529 533
530 /// Compute the [NativeBehavior] for a call to the 'JS' function with the 534 /// Compute the [NativeBehavior] for a call to the 'JS' function with the
531 /// given [specString] and [codeString] (first and second arguments). 535 /// given [specString] and [codeString] (first and second arguments).
532 static NativeBehavior ofJsCall( 536 static NativeBehavior ofJsCall(
533 String specString, 537 String specString,
534 String codeString, 538 String codeString,
535 TypeLookup lookupType, 539 TypeLookup lookupType,
536 Spannable spannable, 540 Spannable spannable,
537 DiagnosticReporter reporter, 541 DiagnosticReporter reporter,
538 CoreTypes coreTypes) { 542 CommonElements commonElements) {
539 // The first argument of a JS-call is a string encoding various attributes 543 // The first argument of a JS-call is a string encoding various attributes
540 // of the code. 544 // of the code.
541 // 545 //
542 // 'Type1|Type2'. A union type. 546 // 'Type1|Type2'. A union type.
543 // '=Object'. A JavaScript Object, no subtype. 547 // '=Object'. A JavaScript Object, no subtype.
544 548
545 NativeBehavior behavior = new NativeBehavior(); 549 NativeBehavior behavior = new NativeBehavior();
546 550
547 behavior.codeTemplateText = codeString; 551 behavior.codeTemplateText = codeString;
548 behavior.codeTemplate = js.js.parseForeignJS(behavior.codeTemplateText); 552 behavior.codeTemplate = js.js.parseForeignJS(behavior.codeTemplateText);
(...skipping 20 matching lines...) Expand all
569 } 573 }
570 574
571 processSpecString(reporter, spannable, specString, 575 processSpecString(reporter, spannable, specString,
572 setSideEffects: setSideEffects, 576 setSideEffects: setSideEffects,
573 setThrows: setThrows, 577 setThrows: setThrows,
574 setIsAllocation: setIsAllocation, 578 setIsAllocation: setIsAllocation,
575 setUseGvn: setUseGvn, 579 setUseGvn: setUseGvn,
576 lookupType: lookupType, 580 lookupType: lookupType,
577 typesReturned: behavior.typesReturned, 581 typesReturned: behavior.typesReturned,
578 typesInstantiated: behavior.typesInstantiated, 582 typesInstantiated: behavior.typesInstantiated,
579 objectType: coreTypes.objectType, 583 objectType: commonElements.objectType,
580 nullType: coreTypes.nullType); 584 nullType: commonElements.nullType);
581 585
582 if (!sideEffectsAreEncodedInSpecString) { 586 if (!sideEffectsAreEncodedInSpecString) {
583 new SideEffectsVisitor(behavior.sideEffects) 587 new SideEffectsVisitor(behavior.sideEffects)
584 .visit(behavior.codeTemplate.ast); 588 .visit(behavior.codeTemplate.ast);
585 } 589 }
586 if (!throwBehaviorFromSpecString) { 590 if (!throwBehaviorFromSpecString) {
587 behavior.throwBehavior = 591 behavior.throwBehavior =
588 new ThrowBehaviorVisitor().analyze(behavior.codeTemplate.ast); 592 new ThrowBehaviorVisitor().analyze(behavior.codeTemplate.ast);
589 } 593 }
590 594
591 return behavior; 595 return behavior;
592 } 596 }
593 597
594 static void _fillNativeBehaviorOfBuiltinOrEmbeddedGlobal( 598 static void _fillNativeBehaviorOfBuiltinOrEmbeddedGlobal(
595 NativeBehavior behavior, 599 NativeBehavior behavior,
596 Spannable spannable, 600 Spannable spannable,
597 String specString, 601 String specString,
598 TypeLookup lookupType, 602 TypeLookup lookupType,
599 DiagnosticReporter reporter, 603 DiagnosticReporter reporter,
600 CoreTypes coreTypes, 604 CommonElements commonElements,
601 {List<String> validTags}) { 605 {List<String> validTags}) {
602 void setSideEffects(SideEffects newEffects) { 606 void setSideEffects(SideEffects newEffects) {
603 behavior.sideEffects.setTo(newEffects); 607 behavior.sideEffects.setTo(newEffects);
604 } 608 }
605 609
606 processSpecString(reporter, spannable, specString, 610 processSpecString(reporter, spannable, specString,
607 validTags: validTags, 611 validTags: validTags,
608 lookupType: lookupType, 612 lookupType: lookupType,
609 setSideEffects: setSideEffects, 613 setSideEffects: setSideEffects,
610 typesReturned: behavior.typesReturned, 614 typesReturned: behavior.typesReturned,
611 typesInstantiated: behavior.typesInstantiated, 615 typesInstantiated: behavior.typesInstantiated,
612 objectType: coreTypes.objectType, 616 objectType: commonElements.objectType,
613 nullType: coreTypes.nullType); 617 nullType: commonElements.nullType);
614 } 618 }
615 619
616 static NativeBehavior ofJsBuiltinCallSend( 620 static NativeBehavior ofJsBuiltinCallSend(
617 Send jsBuiltinCall, 621 Send jsBuiltinCall,
618 DiagnosticReporter reporter, 622 DiagnosticReporter reporter,
619 CoreTypes coreTypes, 623 CommonElements commonElements,
620 ForeignResolver resolver) { 624 ForeignResolver resolver) {
621 NativeBehavior behavior = new NativeBehavior(); 625 NativeBehavior behavior = new NativeBehavior();
622 behavior.sideEffects.setTo(new SideEffects()); 626 behavior.sideEffects.setTo(new SideEffects());
623 627
624 // The first argument of a JS-embedded global call is a string encoding 628 // The first argument of a JS-embedded global call is a string encoding
625 // the type of the code. 629 // the type of the code.
626 // 630 //
627 // 'Type1|Type2'. A union type. 631 // 'Type1|Type2'. A union type.
628 // '=Object'. A JavaScript Object, no subtype. 632 // '=Object'. A JavaScript Object, no subtype.
629 633
(...skipping 13 matching lines...) Expand all
643 LiteralString specLiteral = argNodes.head.asLiteralString(); 647 LiteralString specLiteral = argNodes.head.asLiteralString();
644 if (specLiteral == null) { 648 if (specLiteral == null) {
645 // TODO(sra): We could accept a type identifier? e.g. JS(bool, '1<2'). It 649 // TODO(sra): We could accept a type identifier? e.g. JS(bool, '1<2'). It
646 // is not very satisfactory because it does not work for void, dynamic. 650 // is not very satisfactory because it does not work for void, dynamic.
647 reporter.internalError(argNodes.head, "Unexpected first argument."); 651 reporter.internalError(argNodes.head, "Unexpected first argument.");
648 } 652 }
649 653
650 String specString = specLiteral.dartString.slowToString(); 654 String specString = specLiteral.dartString.slowToString();
651 655
652 return ofJsBuiltinCall(specString, _typeLookup(jsBuiltinCall, resolver), 656 return ofJsBuiltinCall(specString, _typeLookup(jsBuiltinCall, resolver),
653 jsBuiltinCall, reporter, coreTypes); 657 jsBuiltinCall, reporter, commonElements);
654 } 658 }
655 659
656 static NativeBehavior ofJsBuiltinCall( 660 static NativeBehavior ofJsBuiltinCall(
657 String specString, 661 String specString,
658 TypeLookup lookupType, 662 TypeLookup lookupType,
659 Spannable spannable, 663 Spannable spannable,
660 DiagnosticReporter reporter, 664 DiagnosticReporter reporter,
661 CoreTypes coreTypes) { 665 CommonElements commonElements) {
662 NativeBehavior behavior = new NativeBehavior(); 666 NativeBehavior behavior = new NativeBehavior();
663 behavior.sideEffects.setTo(new SideEffects()); 667 behavior.sideEffects.setTo(new SideEffects());
664 _fillNativeBehaviorOfBuiltinOrEmbeddedGlobal( 668 _fillNativeBehaviorOfBuiltinOrEmbeddedGlobal(
665 behavior, spannable, specString, lookupType, reporter, coreTypes); 669 behavior, spannable, specString, lookupType, reporter, commonElements);
666 return behavior; 670 return behavior;
667 } 671 }
668 672
669 static NativeBehavior ofJsEmbeddedGlobalCallSend( 673 static NativeBehavior ofJsEmbeddedGlobalCallSend(
670 Send jsEmbeddedGlobalCall, 674 Send jsEmbeddedGlobalCall,
671 DiagnosticReporter reporter, 675 DiagnosticReporter reporter,
672 CoreTypes coreTypes, 676 CommonElements commonElements,
673 ForeignResolver resolver) { 677 ForeignResolver resolver) {
674 NativeBehavior behavior = new NativeBehavior(); 678 NativeBehavior behavior = new NativeBehavior();
675 // TODO(sra): Allow the use site to override these defaults. 679 // TODO(sra): Allow the use site to override these defaults.
676 // Embedded globals are usually pre-computed data structures or JavaScript 680 // Embedded globals are usually pre-computed data structures or JavaScript
677 // functions that never change. 681 // functions that never change.
678 behavior.sideEffects.setTo(new SideEffects.empty()); 682 behavior.sideEffects.setTo(new SideEffects.empty());
679 behavior.throwBehavior = NativeThrowBehavior.NEVER; 683 behavior.throwBehavior = NativeThrowBehavior.NEVER;
680 684
681 // The first argument of a JS-embedded global call is a string encoding 685 // The first argument of a JS-embedded global call is a string encoding
682 // the type of the code. 686 // the type of the code.
(...skipping 27 matching lines...) Expand all
710 reporter.internalError(argNodes.head, "Unexpected first argument."); 714 reporter.internalError(argNodes.head, "Unexpected first argument.");
711 } 715 }
712 716
713 String specString = specLiteral.dartString.slowToString(); 717 String specString = specLiteral.dartString.slowToString();
714 718
715 return ofJsEmbeddedGlobalCall( 719 return ofJsEmbeddedGlobalCall(
716 specString, 720 specString,
717 _typeLookup(jsEmbeddedGlobalCall, resolver), 721 _typeLookup(jsEmbeddedGlobalCall, resolver),
718 jsEmbeddedGlobalCall, 722 jsEmbeddedGlobalCall,
719 reporter, 723 reporter,
720 coreTypes); 724 commonElements);
721 } 725 }
722 726
723 static NativeBehavior ofJsEmbeddedGlobalCall( 727 static NativeBehavior ofJsEmbeddedGlobalCall(
724 String specString, 728 String specString,
725 TypeLookup lookupType, 729 TypeLookup lookupType,
726 Spannable spannable, 730 Spannable spannable,
727 DiagnosticReporter reporter, 731 DiagnosticReporter reporter,
728 CoreTypes coreTypes) { 732 CommonElements commonElements) {
729 NativeBehavior behavior = new NativeBehavior(); 733 NativeBehavior behavior = new NativeBehavior();
730 // TODO(sra): Allow the use site to override these defaults. 734 // TODO(sra): Allow the use site to override these defaults.
731 // Embedded globals are usually pre-computed data structures or JavaScript 735 // Embedded globals are usually pre-computed data structures or JavaScript
732 // functions that never change. 736 // functions that never change.
733 behavior.sideEffects.setTo(new SideEffects.empty()); 737 behavior.sideEffects.setTo(new SideEffects.empty());
734 behavior.throwBehavior = NativeThrowBehavior.NEVER; 738 behavior.throwBehavior = NativeThrowBehavior.NEVER;
735 _fillNativeBehaviorOfBuiltinOrEmbeddedGlobal( 739 _fillNativeBehaviorOfBuiltinOrEmbeddedGlobal(
736 behavior, spannable, specString, lookupType, reporter, coreTypes, 740 behavior, spannable, specString, lookupType, reporter, commonElements,
737 validTags: ['returns', 'creates']); 741 validTags: ['returns', 'creates']);
738 return behavior; 742 return behavior;
739 } 743 }
740 744
741 static NativeBehavior ofMethodElement( 745 static NativeBehavior ofMethodElement(
742 FunctionElement element, Compiler compiler) { 746 FunctionElement element, Compiler compiler) {
743 FunctionType type = element.computeType(compiler.resolution); 747 FunctionType type = element.computeType(compiler.resolution);
744 List<ConstantExpression> metadata = <ConstantExpression>[]; 748 List<ConstantExpression> metadata = <ConstantExpression>[];
745 for (MetadataAnnotation annotation in element.implementation.metadata) { 749 for (MetadataAnnotation annotation in element.implementation.metadata) {
746 annotation.ensureResolved(compiler.resolution); 750 annotation.ensureResolved(compiler.resolution);
(...skipping 30 matching lines...) Expand all
777 // that interop calls create only interop types (which may be unsound if 781 // that interop calls create only interop types (which may be unsound if
778 // an interop call returns a DOM type and declares a dynamic return type, 782 // an interop call returns a DOM type and declares a dynamic return type,
779 // but otherwise we would include a lot of code by default). 783 // but otherwise we would include a lot of code by default).
780 // TODO(sigmund,sra): consider doing something better for numeric types. 784 // TODO(sigmund,sra): consider doing something better for numeric types.
781 behavior.typesReturned.add( 785 behavior.typesReturned.add(
782 !isJsInterop || compiler.options.trustJSInteropTypeAnnotations 786 !isJsInterop || compiler.options.trustJSInteropTypeAnnotations
783 ? returnType 787 ? returnType
784 : const DynamicType()); 788 : const DynamicType());
785 if (!type.returnType.isVoid) { 789 if (!type.returnType.isVoid) {
786 // Declared types are nullable. 790 // Declared types are nullable.
787 behavior.typesReturned.add(compiler.coreTypes.nullType); 791 behavior.typesReturned.add(compiler.commonElements.nullType);
788 } 792 }
789 behavior._capture(type, compiler.resolution, 793 behavior._capture(type, compiler.resolution,
790 isInterop: isJsInterop, compiler: compiler); 794 isInterop: isJsInterop, compiler: compiler);
791 795
792 for (DartType type in type.optionalParameterTypes) { 796 for (DartType type in type.optionalParameterTypes) {
793 behavior._escape(type, compiler.resolution); 797 behavior._escape(type, compiler.resolution);
794 } 798 }
795 for (DartType type in type.namedParameterTypes) { 799 for (DartType type in type.namedParameterTypes) {
796 behavior._escape(type, compiler.resolution); 800 behavior._escape(type, compiler.resolution);
797 } 801 }
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
832 Compiler compiler, 836 Compiler compiler,
833 {bool isJsInterop}) { 837 {bool isJsInterop}) {
834 Resolution resolution = compiler.resolution; 838 Resolution resolution = compiler.resolution;
835 var behavior = new NativeBehavior(); 839 var behavior = new NativeBehavior();
836 // TODO(sigmund,sra): consider doing something better for numeric types. 840 // TODO(sigmund,sra): consider doing something better for numeric types.
837 behavior.typesReturned.add( 841 behavior.typesReturned.add(
838 !isJsInterop || compiler.options.trustJSInteropTypeAnnotations 842 !isJsInterop || compiler.options.trustJSInteropTypeAnnotations
839 ? type 843 ? type
840 : const DynamicType()); 844 : const DynamicType());
841 // Declared types are nullable. 845 // Declared types are nullable.
842 behavior.typesReturned.add(resolution.coreTypes.nullType); 846 behavior.typesReturned.add(resolution.commonElements.nullType);
843 behavior._capture(type, resolution, 847 behavior._capture(type, resolution,
844 isInterop: isJsInterop, compiler: compiler); 848 isInterop: isJsInterop, compiler: compiler);
845 behavior._overrideWithAnnotations( 849 behavior._overrideWithAnnotations(
846 spannable, metadata, lookupType, compiler); 850 spannable, metadata, lookupType, compiler);
847 return behavior; 851 return behavior;
848 } 852 }
849 853
850 static NativeBehavior ofFieldElementStore( 854 static NativeBehavior ofFieldElementStore(
851 MemberElement field, Resolution resolution) { 855 MemberElement field, Resolution resolution) {
852 DartType type = field.computeType(resolution); 856 DartType type = field.computeType(resolution);
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
1002 type = lookupType(typeString.substring(0, index)); 1006 type = lookupType(typeString.substring(0, index));
1003 if (type != null) { 1007 if (type != null) {
1004 // TODO(sra): Parse type parameters. 1008 // TODO(sra): Parse type parameters.
1005 return type; 1009 return type;
1006 } 1010 }
1007 reporter.reportErrorMessage(spannable, MessageKind.GENERIC, 1011 reporter.reportErrorMessage(spannable, MessageKind.GENERIC,
1008 {'text': "Type '$typeString' not found."}); 1012 {'text': "Type '$typeString' not found."});
1009 return const DynamicType(); 1013 return const DynamicType();
1010 } 1014 }
1011 } 1015 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/mirrors_used.dart ('k') | pkg/compiler/lib/src/native/enqueue.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698